init.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. local common = require "sha1.common"
  2. local sha1 = {
  3. _VERSION = "sha.lua 0.5.0",
  4. _URL = "https://github.com/kikito/sha.lua",
  5. _DESCRIPTION = [[
  6. SHA-1 secure hash computation, and HMAC-SHA1 signature computation in Lua (5.1)
  7. Based on code originally by Jeffrey Friedl (http://regex.info/blog/lua/sha1)
  8. And modified by Eike Decker - (http://cube3d.de/uploads/Main/sha1.txt)
  9. ]],
  10. _LICENSE = [[
  11. MIT LICENSE
  12. Copyright (c) 2013 Enrique García Cota + Eike Decker + Jeffrey Friedl
  13. Permission is hereby granted, free of charge, to any person obtaining a
  14. copy of this software and associated documentation files (the
  15. "Software"), to deal in the Software without restriction, including
  16. without limitation the rights to use, copy, modify, merge, publish,
  17. distribute, sublicense, and/or sell copies of the Software, and to
  18. permit persons to whom the Software is furnished to do so, subject to
  19. the following conditions:
  20. The above copyright notice and this permission notice shall be included
  21. in all copies or substantial portions of the Software.
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  23. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  25. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  26. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  27. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  28. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. ]]
  30. }
  31. local function choose_ops_module()
  32. if _VERSION:find("5%.3") then
  33. return "sha1.lua53_ops"
  34. elseif _VERSION:find("5%.2") then
  35. return "sha1.bit32_ops"
  36. elseif pcall(require, "bit") then
  37. return "sha1.bit_ops"
  38. elseif pcall(require, "bit32") then
  39. return "sha1.bit32_ops"
  40. else
  41. return "sha1.pure_lua_ops"
  42. end
  43. end
  44. local ops = require(choose_ops_module())
  45. local uint32_lrot = ops.uint32_lrot
  46. local byte_xor = ops.byte_xor
  47. local uint32_xor_3 = ops.uint32_xor_3
  48. local uint32_xor_4 = ops.uint32_xor_4
  49. local uint32_ternary = ops.uint32_ternary
  50. local uint32_majority = ops.uint32_majority
  51. local bytes_to_uint32 = common.bytes_to_uint32
  52. local uint32_to_bytes = common.uint32_to_bytes
  53. local sbyte = string.byte
  54. local schar = string.char
  55. local sformat = string.format
  56. local srep = string.rep
  57. local function hex_to_binary(hex)
  58. return (hex:gsub("..", function(hexval)
  59. return schar(tonumber(hexval, 16))
  60. end))
  61. end
  62. -- Calculates SHA1 for a string, returns it encoded as 40 hexadecimal digits.
  63. function sha1.sha1(str)
  64. -- Input preprocessing.
  65. -- First, append a `1` bit and seven `0` bits.
  66. local first_append = schar(0x80)
  67. -- Next, append some zero bytes to make the length of the final message a multiple of 64.
  68. -- Eight more bytes will be added next.
  69. local non_zero_message_bytes = #str + 1 + 8
  70. local second_append = srep(schar(0), -non_zero_message_bytes % 64)
  71. -- Finally, append the length of the original message in bits as a 64-bit number.
  72. -- Assume that it fits into the lower 32 bits.
  73. local third_append = schar(0, 0, 0, 0, uint32_to_bytes(#str * 8))
  74. str = str .. first_append .. second_append .. third_append
  75. assert(#str % 64 == 0)
  76. -- Initialize hash value.
  77. local h0 = 0x67452301
  78. local h1 = 0xEFCDAB89
  79. local h2 = 0x98BADCFE
  80. local h3 = 0x10325476
  81. local h4 = 0xC3D2E1F0
  82. local w = {}
  83. -- Process the input in successive 64-byte chunks.
  84. for chunk_start = 1, #str, 64 do
  85. -- Load the chunk into W[0..15] as uint32 numbers.
  86. local uint32_start = chunk_start
  87. for i = 0, 15 do
  88. w[i] = bytes_to_uint32(sbyte(str, uint32_start, uint32_start + 3))
  89. uint32_start = uint32_start + 4
  90. end
  91. -- Extend the input vector.
  92. for i = 16, 79 do
  93. -- For i = 16 to 79 let Wt = S1(Wt-3 XOR Wt-8 XOR Wt-14 XOR Wt-16).
  94. w[i] = uint32_lrot(uint32_xor_4(w[i - 3], w[i - 8], w[i - 14], w[i - 16]), 1)
  95. end
  96. -- Initialize hash value for this chunk.
  97. local a = h0
  98. local b = h1
  99. local c = h2
  100. local d = h3
  101. local e = h4
  102. -- Main loop.
  103. for i = 0, 79 do
  104. local f
  105. local k
  106. if i <= 19 then
  107. f = uint32_ternary(b, c, d)
  108. k = 0x5A827999
  109. elseif i <= 39 then
  110. f = uint32_xor_3(b, c, d)
  111. k = 0x6ED9EBA1
  112. elseif i <= 59 then
  113. f = uint32_majority(b, c, d)
  114. k = 0x8F1BBCDC
  115. else
  116. f = uint32_xor_3(b, c, d)
  117. k = 0xCA62C1D6
  118. end
  119. local temp = (uint32_lrot(a, 5) + f + e + k + w[i]) % 0x100000000
  120. e = d
  121. d = c
  122. c = uint32_lrot(b, 30)
  123. b = a
  124. a = temp
  125. end
  126. -- Add this chunk's hash to result so far.
  127. h0 = (h0 + a) % 0x100000000
  128. h1 = (h1 + b) % 0x100000000
  129. h2 = (h2 + c) % 0x100000000
  130. h3 = (h3 + d) % 0x100000000
  131. h4 = (h4 + e) % 0x100000000
  132. end
  133. return sformat("%08x%08x%08x%08x%08x", h0, h1, h2, h3, h4)
  134. end
  135. function sha1.binary(str)
  136. return hex_to_binary(sha1.sha1(str))
  137. end
  138. -- Precalculate replacement tables.
  139. local xor_with_0x5c = {}
  140. local xor_with_0x36 = {}
  141. for i = 0, 0xff do
  142. xor_with_0x5c[schar(i)] = schar(byte_xor(0x5c, i))
  143. xor_with_0x36[schar(i)] = schar(byte_xor(0x36, i))
  144. end
  145. -- 512 bits.
  146. local BLOCK_SIZE = 64
  147. function sha1.hmac(key, text)
  148. if #key > BLOCK_SIZE then
  149. key = sha1.binary(key)
  150. end
  151. local key_xord_with_0x36 = key:gsub('.', xor_with_0x36) .. srep(schar(0x36), BLOCK_SIZE - #key)
  152. local key_xord_with_0x5c = key:gsub('.', xor_with_0x5c) .. srep(schar(0x5c), BLOCK_SIZE - #key)
  153. return sha1.sha1(key_xord_with_0x5c .. sha1.binary(key_xord_with_0x36 .. text))
  154. end
  155. function sha1.hmac_binary(key, text)
  156. return hex_to_binary(sha1.hmac(key, text))
  157. end
  158. setmetatable(sha1, {__call = function(_, str) return sha1.sha1(str) end})
  159. return sha1