FileGetHash

From Multi Theft Auto: Wiki
Revision as of 18:48, 23 July 2025 by Botder (talk | contribs) (Created page with "__NOTOC__ {{Server client function}} {{Added feature/item|1.6.1|1.6.0|23289| 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. }} ==Syntax== <syntaxhighlight lang="lua"> nil|string fileGetHash...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

BETA: NEW FEATURE (BUILD: 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.

Syntax

nil|string fileGetHash ( file theFile, string algorithm [, table options ] )

OOP Syntax Help! I don't understand this!

Method: file:getHash(...)


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. For more information on optional arguments, see optional arguments.

  • options: A table with options and other necessary data for the algorithm, as detailed below.

Options for each algorithm

  • 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

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

Example

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)
iprint("HMAC-SHA256", hashHMAC )

See Also