Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. -- Copyright (c) 2016 John Schember <john@nachtimwald.com>
  2. --
  3. -- Permission is hereby granted, free of charge, to any person obtaining
  4. -- a copy of this software and associated documentation files (the "Software"),
  5. -- to deal in the Software without restriction, including without limitation
  6. -- the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. -- and/or sell copies of the Software, and to permit persons to whom the
  8. -- Software is furnished to do so, subject to the following conditions:
  9. --
  10. -- The above copyright notice and this permission notice shall be included in
  11. -- all copies or substantial portions of the Software.
  12. --
  13. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. -- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. -- DEALINGS IN THE SOFTWARE.
  20.  
  21. local string = require("string")
  22.  
  23. local M = {}
  24. local M_mt = { __metatable = {}, __index = M }
  25.  
  26. function M:new(hm, key, data)
  27. local th
  28. local tk = {}
  29. local ipad = {}
  30.  
  31. if self ~= M then
  32. return nil, "First argument must be self"
  33. end
  34. local o = setmetatable({}, M_mt)
  35. o._hm = hm
  36.  
  37. -- Compute the key.
  38. if #key > hm.block_size then
  39. th = hm(key)
  40. key = th:digest()
  41. end
  42. for i=1,#key do
  43. tk[#tk+1] = string.byte(key, i)
  44. end
  45. for i=#key+1,hm.block_size do
  46. tk[#tk+1] = 0
  47. end
  48.  
  49. -- Generate the inner and outer padding.
  50. o._opad = {}
  51. for i=1,#tk do
  52. ipad[i] = string.char(tk[i] ~ 0x36)
  53. o._opad[i] = string.char(tk[i] ~ 0x5C)
  54. end
  55. ipad = table.concat(ipad)
  56. o._opad = table.concat(o._opad)
  57.  
  58. -- Start the hash witht the inner padding
  59. o._hash = o._hm(ipad)
  60.  
  61. if data ~= nil then
  62. o._hash:update(data)
  63. end
  64.  
  65. return o
  66. end
  67. setmetatable(M, { __call = M.new })
  68.  
  69. function M:copy()
  70. local o = setmetatable({}, M_mt)
  71. o._hm = self._hm
  72. o._hash = self._hash:copy()
  73. o._opad = self._opad
  74. return o
  75. end
  76.  
  77. function M:update(data)
  78. self._hash:update(data)
  79. end
  80.  
  81. function M:digest()
  82. local final
  83. local digest
  84. local th
  85.  
  86. final = self:copy()
  87. digest = final._hash:digest()
  88. th = final._hm(final._opad)
  89. th:update(digest)
  90.  
  91. return th:digest()
  92. end
  93.  
  94. function M:hexdigest()
  95. local h
  96. local out = {}
  97.  
  98. h = self:digest()
  99. for i=1,#h do
  100. out[i] = string.format("%02X", string.byte(h, i))
  101. end
  102. return table.concat(out)
  103. end
  104.  
  105. return M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement