fileGetHash | Multi Theft Auto: Wiki Skip to content

fileGetHash

Client-side
Server-side
Shared

Added in 1.6.0 r23289

This function returns a hash of the entire file in the specified algorithm. This function does not move the file pointer/position. Beware though, there will always be a minuscule period of time between checking the hash and loading the contents of the file, which can be abused by a potential attacker to modify the contents.

OOP Syntax Help! I don't understand this!

  • Method: file:getHash(...)

Syntax

string|nil fileGetHash ( file theFile, string algorithm, [ table options = nil ] )
Required Arguments
  • theFile: A handle to the file you wish to get the hash from. Use fileOpen to obtain this handle.
  • algorithm: A string which must be one of these: "md5", "sha1", "sha224", "sha256", "sha384", "sha512", "hmac".
Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use.

  • options (default: nil): A table with options and other necessary data for the algorithm, as detailed below.
    • hmac (HMAC)
      • key: a key to encode the input with.
      • algorithm: a string which must be one of these: "md5", "sha1", "sha224", "sha256", "sha384", "sha512".

Returns

  • string|nil: file hash

Returns the hash of the entire file on success, and nil on failure.

Code Examples

shared

This example opens the code.lua file, computes the hash with every algorithm, and then displays them.

local handle = fileOpen("code.lua", true)
local hashMD5 = fileGetHash(handle, "md5")
local hashSHA1 = fileGetHash(handle, "sha1")
local hashSHA224 = fileGetHash(handle, "sha224")
local hashSHA256 = fileGetHash(handle, "sha256")
local hashSHA384 = fileGetHash(handle, "sha384")
local hashSHA512 = fileGetHash(handle, "sha512")
local hashHMAC = fileGetHash(handle, "hmac", { algorithm = "sha256", key = "blue apple tree" })
fileClose(handle)
iprint("MD5", hashMD5)
iprint("SHA1", hashSHA1)
iprint("SHA224", hashSHA224)
iprint("SHA256", hashSHA256)
iprint("SHA384", hashSHA384)
iprint("SHA512", hashSHA512)