Advertisement
Guest User

Untitled

a guest
Apr 15th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. The following script performs some basic tests over the previous implementation and shows how to use the library:
  2.  
  3. -- tests for SHA-2 in Lua 5.2
  4.  
  5. local sha2 = require 'sha2'
  6.  
  7. -- a few examples from the Web
  8.  
  9. assert(sha2.hash224"The quick brown fox jumps over the lazy dog" ==
  10. "730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525")
  11.  
  12. assert(sha2.hash224"" ==
  13. "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f")
  14.  
  15. assert(sha2.hash256"The quick brown fox jumps over the lazy dog" ==
  16. "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592")
  17.  
  18. assert(sha2.hash256"The quick brown fox jumps over the lazy cog" ==
  19. "e4c4d8f3bf76b692de791a173e05321150f7a345b46484fe427f6acc7ecc81be")
  20.  
  21. assert(sha2.hash256"" ==
  22. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
  23.  
  24. assert(sha2.new256():close() ==
  25. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
  26.  
  27. assert(sha2.hash256"123456" ==
  28. "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92")
  29.  
  30.  
  31. -- most other examples here are checked against a "correct" answer
  32. -- given by 'sha224sum'/'sha256sum'
  33.  
  34.  
  35. -- border cases (sizes around 64 bytes)
  36.  
  37. assert(sha2.hash256(string.rep('a', 62) .. '\n') ==
  38. "290b30a68148b3ee27ab7b744c297a5d986c1011938a09e73058430593bf83f0")
  39. assert(sha2.hash256(string.rep('a', 63) .. '\n') ==
  40. "a229eaed30f4991d1fcdab77c70b604efd780502c82be0732b310811dc43b2b3")
  41. assert(sha2.hash256(string.rep('a', 64) .. '\n') ==
  42. "44c2336fedab8ff6a85c74c2b94165377b0981f526adb9487895ca6314165e86")
  43. assert(sha2.hash256(string.rep('a', 65) .. '\n') ==
  44. "574883a9977284a46845620eaa55c3fa8209eaa3ebffe44774b6eb2dba2cb325")
  45.  
  46. local x = sha2.new256()
  47. for i = 1, 65 do x:add('a') end
  48. x:add('\n')
  49. assert(x:close() ==
  50. "574883a9977284a46845620eaa55c3fa8209eaa3ebffe44774b6eb2dba2cb325")
  51.  
  52.  
  53. -- some large files
  54. local function parts (s, j)
  55. local x = sha2.new256()
  56. local i = 1; j = 1
  57. while i <= #s do
  58. x:add(s:sub(i, i + j))
  59. i = i + j + 1
  60. end
  61. return x:close()
  62. end
  63.  
  64. -- 80 lines of 80 '0's each
  65. local s = string.rep('0', 80) .. '\n'
  66. s = string.rep(s, 80)
  67. assert(parts(s, 70) ==
  68. "736c7a8b17e2cfd44a3267a844db1a8a3e8988d739e3e95b8dd32678fb599139")
  69. assert(parts(s, 7) ==
  70. "736c7a8b17e2cfd44a3267a844db1a8a3e8988d739e3e95b8dd32678fb599139")
  71. assert(parts(s, #s + 10) ==
  72. "736c7a8b17e2cfd44a3267a844db1a8a3e8988d739e3e95b8dd32678fb599139")
  73.  
  74.  
  75.  
  76.  
  77. -- read a file and prints its hash, if given a file name
  78.  
  79. if arg[1] then
  80. local file = assert(io.open (arg[1], 'rb'))
  81. local x = sha2.new256()
  82. for b in file:lines(2^12) do
  83. x:add(b)
  84. end
  85. file:close()
  86. print(x:close())
  87. end
  88.  
  89. print "ok"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement