Skip to content

Commit d2da9a5

Browse files
committed
fileGetHash
1 parent 986136b commit d2da9a5

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local handle = fileOpen("code.lua", true)
2+
local hashMD5 = fileGetHash(handle, "md5")
3+
local hashSHA1 = fileGetHash(handle, "sha1")
4+
local hashSHA224 = fileGetHash(handle, "sha224")
5+
local hashSHA256 = fileGetHash(handle, "sha256")
6+
local hashSHA384 = fileGetHash(handle, "sha384")
7+
local hashSHA512 = fileGetHash(handle, "sha512")
8+
local hashHMAC = fileGetHash(handle, "hmac", { algorithm = "sha256", key = "blue apple tree" })
9+
fileClose(handle)
10+
11+
iprint("MD5", hashMD5)
12+
iprint("SHA1", hashSHA1)
13+
iprint("SHA224", hashSHA224)
14+
iprint("SHA256", hashSHA256)
15+
iprint("SHA384", hashSHA384)
16+
iprint("SHA512", hashSHA512)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local handle = File("code.lua", true)
2+
local hashMD5 = handle:getHash("md5")
3+
local hashSHA1 = handle:getHash("sha1")
4+
local hashSHA224 = handle:getHash("sha224")
5+
local hashSHA256 = handle:getHash("sha256")
6+
local hashSHA384 = handle:getHash("sha384")
7+
local hashSHA512 = handle:getHash("sha512")
8+
local hashHMAC = handle:getHash("hmac", { algorithm = "sha256", key = "blue apple tree" })
9+
handle:close()
10+
11+
iprint("MD5", hashMD5)
12+
iprint("SHA1", hashSHA1)
13+
iprint("SHA224", hashSHA224)
14+
iprint("SHA256", hashSHA256)
15+
iprint("SHA384", hashSHA384)
16+
iprint("SHA512", hashSHA512)

functions/File/fileGetHash.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
shared: &shared
2+
name: fileGetHash
3+
description: |
4+
This function returns a hash of the entire file in the specified algorithm.
5+
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.
6+
oop:
7+
element: file
8+
method: getHash
9+
version:
10+
added: 1.6.0 r23289
11+
parameters:
12+
- name: theFile
13+
type: file
14+
description: A handle to the file you wish to get the hash from. Use [[fileOpen]] to obtain this handle.
15+
- name: algorithm
16+
type: string
17+
description: |
18+
A string which must be one of these: **"md5", "sha1", "sha224", "sha256", "sha384", "sha512", "hmac"**.
19+
- name: options
20+
type: table
21+
description: |
22+
A [[table]] with options and other necessary data for the algorithm, as detailed below.
23+
<ul>
24+
<li>hmac (HMAC)</li>
25+
<ul style='margin-top: 0;'>
26+
<li><strong>key</strong>: a key to encode the input with.</li>
27+
<li><strong>algorithm</strong>: a [[string]] which must be one of these: "md5", "sha1", "sha224", "sha256", "sha384", "sha512".</li>
28+
</ul>
29+
</ul>
30+
31+
default: 'nil'
32+
returns:
33+
values:
34+
- type: string|nil
35+
name: file hash
36+
description: Returns the hash of the entire file on success, and nil on failure.
37+
examples:
38+
- path: examples/fileGetHash-1.lua
39+
description: This example opens the code.lua file, computes the hash with every algorithm, and then displays them.
40+
- path: examples/fileGetHash_OOP-1.lua
41+
description: This example opens the code.lua file, computes the hash with every algorithm, and then displays them.
42+
oop: true

0 commit comments

Comments
 (0)