Skip to content

Commit c9a84ea

Browse files
committed
add SHA512, export hash_*
1 parent bdc34af commit c9a84ea

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

hmac.nim

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,29 @@ import securehash, md5, nimSHA2, strutils
1515
type
1616
Sha1Digest = array[20, uint8]
1717

18-
proc hash_sha1(s: string): SecureHash {.procvar.} =
18+
proc hash_sha1*(s: string): SecureHash {.procvar.} =
1919
secureHash(s)
2020

21-
proc hash_sha256(s: string): SHA256Digest {.procvar.} =
21+
proc hash_sha256*(s: string): SHA256Digest {.procvar.} =
2222
computeSHA256(s)
2323

24-
proc hash_md5(s: string): MD5Digest {.procvar.} =
25-
toMD5(s)
24+
proc hash_sha512*(s: string): SHA512Digest {.procvar.} =
25+
computeSHA512(s)
2626

27+
proc hash_md5*(s: string): MD5Digest {.procvar.} =
28+
toMD5(s)
29+
2730
iterator items(s: SecureHash): uint8 =
2831
for n in Sha1Digest(s):
2932
yield n
3033

31-
proc `%`*[T: SecureHash|SHA256Digest|MD5Digest](x: T): string =
34+
proc `%`*[T](x: T): string =
3235
when x is SecureHash:
3336
toLower($x)
34-
elif x is SHA256Digest:
35-
toLower(nimSHA2.toHex(x))
3637
elif x is MD5Digest:
3738
$x
3839
else:
39-
discard
40+
toLower(nimSHA2.toHex(x))
4041

4142
template hmac_x[T](key, data: string, hash: proc(s: string): T, digest_size: int, block_size = 64, opad = 0x5c, ipad = 0x36): stmt =
4243
var keyA: seq[uint8] = @[]
@@ -66,7 +67,10 @@ proc hmac_sha1*(key, data: string, block_size = 64, opad = 0x5c, ipad = 0x36): S
6667
hmac_x(key, data, hash_sha1, 20, block_size, opad, ipad)
6768
6869
proc hmac_sha256*(key, data: string, block_size = 64, opad = 0x5c, ipad = 0x36): SHA256Digest =
69-
hmac_x(key, data, hash_sha256, 32, block_size, opad, ipad)
70+
hmac_x(key, data, hash_sha256, 32, block_size, opad, ipad)
71+
72+
proc hmac_sha512*(key, data: string, block_size = 64, opad = 0x5c, ipad = 0x36): SHA512Digest =
73+
hmac_x(key, data, hash_sha512, 32, block_size, opad, ipad)
7074
7175
proc hmac_md5*(key, data: string): MD5Digest =
7276
hmac_x(key, data, hash_md5, 16)

hmac.nimble

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
version = "0.1.2"
1+
version = "0.1.3"
22
author = "Huy Doan"
33
description = "HMAC hashing in Nim"
44
license = "MIT"
55

6-
requires "nim", "nimSHA2"
6+
requires "nim", "nimSHA2"

0 commit comments

Comments
 (0)