sha1.nim 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Nim Contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Note: Import ``std/sha1`` to use this module
  10. import strutils
  11. from endians import bigEndian32, bigEndian64
  12. const Sha1DigestSize = 20
  13. type
  14. Sha1Digest = array[0 .. Sha1DigestSize-1, uint8]
  15. SecureHash* = distinct Sha1Digest
  16. type
  17. Sha1State = object
  18. count: int
  19. state: array[5, uint32]
  20. buf: array[64, byte]
  21. # This implementation of the SHA1 algorithm was ported from the Chromium OS one
  22. # with minor modifications that should not affect its functionality.
  23. proc newSha1State(): Sha1State =
  24. result.count = 0
  25. result.state[0] = 0x67452301'u32
  26. result.state[1] = 0xEFCDAB89'u32
  27. result.state[2] = 0x98BADCFE'u32
  28. result.state[3] = 0x10325476'u32
  29. result.state[4] = 0xC3D2E1F0'u32
  30. template ror27(val: uint32): uint32 = (val shr 27) or (val shl 5)
  31. template ror2 (val: uint32): uint32 = (val shr 2) or (val shl 30)
  32. template ror31(val: uint32): uint32 = (val shr 31) or (val shl 1)
  33. proc transform(ctx: var Sha1State) =
  34. var W: array[80, uint32]
  35. var A, B, C, D, E: uint32
  36. var t = 0
  37. A = ctx.state[0]
  38. B = ctx.state[1]
  39. C = ctx.state[2]
  40. D = ctx.state[3]
  41. E = ctx.state[4]
  42. template SHA_F1(A, B, C, D, E, t: untyped) =
  43. bigEndian32(addr W[t], addr ctx.buf[t * 4])
  44. E += ror27(A) + W[t] + (D xor (B and (C xor D))) + 0x5A827999'u32
  45. B = ror2(B)
  46. while t < 15:
  47. SHA_F1(A, B, C, D, E, t + 0)
  48. SHA_F1(E, A, B, C, D, t + 1)
  49. SHA_F1(D, E, A, B, C, t + 2)
  50. SHA_F1(C, D, E, A, B, t + 3)
  51. SHA_F1(B, C, D, E, A, t + 4)
  52. t += 5
  53. SHA_F1(A, B, C, D, E, t + 0) # 16th one, t == 15
  54. template SHA_F11(A, B, C, D, E, t: untyped) =
  55. W[t] = ror31(W[t-3] xor W[t-8] xor W[t-14] xor W[t-16])
  56. E += ror27(A) + W[t] + (D xor (B and (C xor D))) + 0x5A827999'u32
  57. B = ror2(B)
  58. SHA_F11(E, A, B, C, D, t + 1)
  59. SHA_F11(D, E, A, B, C, t + 2)
  60. SHA_F11(C, D, E, A, B, t + 3)
  61. SHA_F11(B, C, D, E, A, t + 4)
  62. template SHA_F2(A, B, C, D, E, t: untyped) =
  63. W[t] = ror31(W[t-3] xor W[t-8] xor W[t-14] xor W[t-16])
  64. E += ror27(A) + W[t] + (B xor C xor D) + 0x6ED9EBA1'u32
  65. B = ror2(B)
  66. t = 20
  67. while t < 40:
  68. SHA_F2(A, B, C, D, E, t + 0)
  69. SHA_F2(E, A, B, C, D, t + 1)
  70. SHA_F2(D, E, A, B, C, t + 2)
  71. SHA_F2(C, D, E, A, B, t + 3)
  72. SHA_F2(B, C, D, E, A, t + 4)
  73. t += 5
  74. template SHA_F3(A, B, C, D, E, t: untyped) =
  75. W[t] = ror31(W[t-3] xor W[t-8] xor W[t-14] xor W[t-16])
  76. E += ror27(A) + W[t] + ((B and C) or (D and (B or C))) + 0x8F1BBCDC'u32
  77. B = ror2(B)
  78. while t < 60:
  79. SHA_F3(A, B, C, D, E, t + 0)
  80. SHA_F3(E, A, B, C, D, t + 1)
  81. SHA_F3(D, E, A, B, C, t + 2)
  82. SHA_F3(C, D, E, A, B, t + 3)
  83. SHA_F3(B, C, D, E, A, t + 4)
  84. t += 5
  85. template SHA_F4(A, B, C, D, E, t: untyped) =
  86. W[t] = ror31(W[t-3] xor W[t-8] xor W[t-14] xor W[t-16])
  87. E += ror27(A) + W[t] + (B xor C xor D) + 0xCA62C1D6'u32
  88. B = ror2(B)
  89. while t < 80:
  90. SHA_F4(A, B, C, D, E, t + 0)
  91. SHA_F4(E, A, B, C, D, t + 1)
  92. SHA_F4(D, E, A, B, C, t + 2)
  93. SHA_F4(C, D, E, A, B, t + 3)
  94. SHA_F4(B, C, D, E, A, t + 4)
  95. t += 5
  96. ctx.state[0] += A
  97. ctx.state[1] += B
  98. ctx.state[2] += C
  99. ctx.state[3] += D
  100. ctx.state[4] += E
  101. proc update(ctx: var Sha1State, data: openArray[char]) =
  102. var i = ctx.count mod 64
  103. var j = 0
  104. var len = data.len
  105. # Gather 64-bytes worth of data in order to perform a round with the leftover
  106. # data we had stored (but not processed yet)
  107. if len > 64 - i:
  108. copyMem(addr ctx.buf[i], unsafeAddr data[j], 64 - i)
  109. len -= 64 - i
  110. j += 64 - i
  111. transform(ctx)
  112. # Update the index since it's used in the while loop below _and_ we want to
  113. # keep its value if this code path isn't executed
  114. i = 0
  115. # Process the bulk of the payload
  116. while len >= 64:
  117. copyMem(addr ctx.buf[0], unsafeAddr data[j], 64)
  118. len -= 64
  119. j += 64
  120. transform(ctx)
  121. # Process the tail of the payload (len is < 64)
  122. while len > 0:
  123. dec len
  124. ctx.buf[i] = byte(data[j])
  125. inc i
  126. inc j
  127. if i == 64:
  128. transform(ctx)
  129. i = 0
  130. ctx.count += data.len
  131. proc finalize(ctx: var Sha1State): Sha1Digest =
  132. var cnt = uint64(ctx.count * 8)
  133. # A 1 bit
  134. update(ctx, "\x80")
  135. # Add padding until we reach a complexive size of 64 - 8 bytes
  136. while (ctx.count mod 64) != (64 - 8):
  137. update(ctx, "\x00")
  138. # The message length as a 64bit BE number completes the block
  139. var tmp: array[8, char]
  140. bigEndian64(addr tmp[0], addr cnt)
  141. update(ctx, tmp)
  142. # Turn the result into a single 160-bit number
  143. for i in 0 ..< 5:
  144. bigEndian32(addr ctx.state[i], addr ctx.state[i])
  145. copyMem(addr result[0], addr ctx.state[0], Sha1DigestSize)
  146. # Public API
  147. proc secureHash*(str: string): SecureHash =
  148. var state = newSha1State()
  149. state.update(str)
  150. SecureHash(state.finalize())
  151. proc secureHashFile*(filename: string): SecureHash =
  152. secureHash(readFile(filename))
  153. proc `$`*(self: SecureHash): string =
  154. result = ""
  155. for v in Sha1Digest(self):
  156. result.add(toHex(int(v), 2))
  157. proc parseSecureHash*(hash: string): SecureHash =
  158. for i in 0 ..< Sha1DigestSize:
  159. Sha1Digest(result)[i] = uint8(parseHexInt(hash[i*2] & hash[i*2 + 1]))
  160. proc `==`*(a, b: SecureHash): bool =
  161. # Not a constant-time comparison, but that's acceptable in this context
  162. Sha1Digest(a) == Sha1Digest(b)
  163. when isMainModule:
  164. let hash1 = secureHash("a93tgj0p34jagp9[agjp98ajrhp9aej]")
  165. doAssert hash1 == hash1
  166. doAssert parseSecureHash($hash1) == hash1
  167. template checkVector(s, exp: string) =
  168. doAssert secureHash(s) == parseSecureHash(exp)
  169. checkVector("", "da39a3ee5e6b4b0d3255bfef95601890afd80709")
  170. checkVector("abc", "a9993e364706816aba3e25717850c26c9cd0d89d")
  171. checkVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  172. "84983e441c3bd26ebaae4aa1f95129e5e54670f1")