From c9a84ea24bb20de196a62ca675673a1d0b4c69b6 Mon Sep 17 00:00:00 2001 From: Bruce Doan Date: Sat, 1 Jul 2017 13:14:01 +0700 Subject: [PATCH] add SHA512, export hash_* --- hmac.nim | 22 +++++++++++++--------- hmac.nimble | 4 ++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/hmac.nim b/hmac.nim index ea9170e..7d85755 100644 --- a/hmac.nim +++ b/hmac.nim @@ -15,28 +15,29 @@ import securehash, md5, nimSHA2, strutils type Sha1Digest = array[20, uint8] -proc hash_sha1(s: string): SecureHash {.procvar.} = +proc hash_sha1*(s: string): SecureHash {.procvar.} = secureHash(s) -proc hash_sha256(s: string): SHA256Digest {.procvar.} = +proc hash_sha256*(s: string): SHA256Digest {.procvar.} = computeSHA256(s) -proc hash_md5(s: string): MD5Digest {.procvar.} = - toMD5(s) +proc hash_sha512*(s: string): SHA512Digest {.procvar.} = + computeSHA512(s) +proc hash_md5*(s: string): MD5Digest {.procvar.} = + toMD5(s) + iterator items(s: SecureHash): uint8 = for n in Sha1Digest(s): yield n -proc `%`*[T: SecureHash|SHA256Digest|MD5Digest](x: T): string = +proc `%`*[T](x: T): string = when x is SecureHash: toLower($x) - elif x is SHA256Digest: - toLower(nimSHA2.toHex(x)) elif x is MD5Digest: $x else: - discard + toLower(nimSHA2.toHex(x)) template hmac_x[T](key, data: string, hash: proc(s: string): T, digest_size: int, block_size = 64, opad = 0x5c, ipad = 0x36): stmt = var keyA: seq[uint8] = @[] @@ -66,7 +67,10 @@ proc hmac_sha1*(key, data: string, block_size = 64, opad = 0x5c, ipad = 0x36): S hmac_x(key, data, hash_sha1, 20, block_size, opad, ipad) proc hmac_sha256*(key, data: string, block_size = 64, opad = 0x5c, ipad = 0x36): SHA256Digest = - hmac_x(key, data, hash_sha256, 32, block_size, opad, ipad) + hmac_x(key, data, hash_sha256, 32, block_size, opad, ipad) + +proc hmac_sha512*(key, data: string, block_size = 64, opad = 0x5c, ipad = 0x36): SHA512Digest = + hmac_x(key, data, hash_sha512, 32, block_size, opad, ipad) proc hmac_md5*(key, data: string): MD5Digest = hmac_x(key, data, hash_md5, 16) diff --git a/hmac.nimble b/hmac.nimble index 49e987e..726f7ce 100644 --- a/hmac.nimble +++ b/hmac.nimble @@ -1,6 +1,6 @@ -version = "0.1.2" +version = "0.1.3" author = "Huy Doan" description = "HMAC hashing in Nim" license = "MIT" -requires "nim", "nimSHA2" \ No newline at end of file +requires "nim", "nimSHA2"