diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2016-09-13 17:20:53 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2016-09-13 17:20:53 +0200 |
commit | 5f685bb0e6b2d68b9796e6a7d29469ce3f3b38ec (patch) | |
tree | ffb471196b1b08e9d0eb78e340f92c27229b1496 /lib/pure | |
parent | b78029b5afb3bfa69cf41203ca358285c9a71cde (diff) | |
download | Nim-5f685bb0e6b2d68b9796e6a7d29469ce3f3b38ec.tar.gz |
fixes #4760
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/securehash.nim | 45 |
1 files changed, 21 insertions, 24 deletions
diff --git a/lib/pure/securehash.nim b/lib/pure/securehash.nim index 2c59bdf4a..1f00ce8d3 100644 --- a/lib/pure/securehash.nim +++ b/lib/pure/securehash.nim @@ -15,30 +15,6 @@ type Sha1Digest = array[0 .. Sha1DigestSize-1, uint8] SecureHash* = distinct Sha1Digest -proc sha1(src: string) : Sha1Digest - -proc secureHash*(str: string): SecureHash = SecureHash(sha1(str)) -proc secureHashFile*(filename: string): SecureHash = secureHash(readFile(filename)) -proc `$`*(self: SecureHash): string = - result = "" - for v in Sha1Digest(self): - result.add(toHex(int(v), 2)) - -proc parseSecureHash*(hash: string): SecureHash = - for i in 0.. <Sha1DigestSize: - Sha1Digest(result)[i] = uint8(parseHexInt(hash[i*2] & hash[i*2 + 1])) - -proc `==`*(a, b: SecureHash): bool = - # Not a constant-time comparison, but that's acceptable in this context - Sha1Digest(a) == Sha1Digest(b) - - -when isMainModule: - let hash1 = secureHash("a93tgj0p34jagp9[agjp98ajrhp9aej]") - doAssert hash1 == hash1 - doAssert parseSecureHash($hash1) == hash1 - - # Copyright (c) 2011, Micael Hildenborg # All rights reserved. # @@ -196,3 +172,24 @@ template computeInternal(src: untyped) = proc sha1(src: string) : Sha1Digest = ## Calculate SHA1 from input string computeInternal(src) + +proc secureHash*(str: string): SecureHash = SecureHash(sha1(str)) +proc secureHashFile*(filename: string): SecureHash = secureHash(readFile(filename)) +proc `$`*(self: SecureHash): string = + result = "" + for v in Sha1Digest(self): + result.add(toHex(int(v), 2)) + +proc parseSecureHash*(hash: string): SecureHash = + for i in 0.. <Sha1DigestSize: + Sha1Digest(result)[i] = uint8(parseHexInt(hash[i*2] & hash[i*2 + 1])) + +proc `==`*(a, b: SecureHash): bool = + # Not a constant-time comparison, but that's acceptable in this context + Sha1Digest(a) == Sha1Digest(b) + + +when isMainModule: + let hash1 = secureHash("a93tgj0p34jagp9[agjp98ajrhp9aej]") + doAssert hash1 == hash1 + doAssert parseSecureHash($hash1) == hash1 |