Grauly

CC_IEC-OS lib1

May 7th, 2021
653
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. local u32 = require("nums.uintn").u32
  23.  
  24. local M = {}
  25. local M_mt = { __metatable = {}, __index = M }
  26.  
  27. M.digest_size = 32
  28. M.block_size = 64
  29.  
  30. local K = {
  31.     u32(0x428A2F98), u32(0x71374491), u32(0xB5C0FBCF), u32(0xE9B5DBA5),
  32.     u32(0x3956C25B), u32(0x59F111F1), u32(0x923F82A4), u32(0xAB1C5ED5),
  33.     u32(0xD807AA98), u32(0x12835B01), u32(0x243185BE), u32(0x550C7DC3),
  34.     u32(0x72BE5D74), u32(0x80DEB1FE), u32(0x9BDC06A7), u32(0xC19BF174),
  35.     u32(0xE49B69C1), u32(0xEFBE4786), u32(0x0FC19DC6), u32(0x240CA1CC),
  36.     u32(0x2DE92C6F), u32(0x4A7484AA), u32(0x5CB0A9DC), u32(0x76F988DA),
  37.     u32(0x983E5152), u32(0xA831C66D), u32(0xB00327C8), u32(0xBF597FC7),
  38.     u32(0xC6E00BF3), u32(0xD5A79147), u32(0x06CA6351), u32(0x14292967),
  39.     u32(0x27B70A85), u32(0x2E1B2138), u32(0x4D2C6DFC), u32(0x53380D13),
  40.     u32(0x650A7354), u32(0x766A0ABB), u32(0x81C2C92E), u32(0x92722C85),
  41.     u32(0xA2BFE8A1), u32(0xA81A664B), u32(0xC24B8B70), u32(0xC76C51A3),
  42.     u32(0xD192E819), u32(0xD6990624), u32(0xF40E3585), u32(0x106AA070),
  43.     u32(0x19A4C116), u32(0x1E376C08), u32(0x2748774C), u32(0x34B0BCB5),
  44.     u32(0x391C0CB3), u32(0x4ED8AA4A), u32(0x5B9CCA4F), u32(0x682E6FF3),
  45.     u32(0x748F82EE), u32(0x78A5636F), u32(0x84C87814), u32(0x8CC70208),
  46.     u32(0x90BEFFFA), u32(0xA4506CEB), u32(0xBEF9A3F7), u32(0xC67178F2)
  47. }
  48.  
  49. local function rotate_right(x, n)
  50.     return (x >> n) | (x << (32-n))
  51. end
  52.  
  53. local function CH(x, y, z)
  54.     return (x & y) ~ ((~x) & z)
  55. end
  56.  
  57. local function MAJ(x, y, z)
  58.     return (x & y) ~ ( x & z) ~ (y & z)
  59. end
  60.  
  61. local function BSIG0(x)
  62.     return rotate_right(x, 2) ~ rotate_right(x, 13) ~ rotate_right(x, 22)
  63. end
  64.  
  65. local function BSIG1(x)
  66.     return rotate_right(x, 6) ~ rotate_right(x, 11) ~ rotate_right(x, 25)
  67. end
  68.  
  69. local function SSIG0(x)
  70.     return rotate_right(x, 7) ~ rotate_right(x, 18) ~ (x >> 3)
  71. end
  72.  
  73. local function SSIG1(x)
  74.     return rotate_right(x, 17) ~ rotate_right(x, 19) ~ (x >> 10)
  75. end
  76.  
  77. function M:new(data)
  78.     if self ~= M then
  79.         return nil, "First argument must be self"
  80.     end
  81.     local o = setmetatable({}, M_mt)
  82.  
  83.     o._H0 = u32(0x6A09E667)
  84.     o._H1 = u32(0xBB67AE85)
  85.     o._H2 = u32(0x3C6EF372)
  86.     o._H3 = u32(0xA54FF53A)
  87.     o._H4 = u32(0x510E527F)
  88.     o._H5 = u32(0x9B05688C)
  89.     o._H6 = u32(0x1F83D9AB)
  90.     o._H7 = u32(0x5BE0CD19)
  91.     o._len = 0
  92.     o._data = ""
  93.  
  94.     if data ~= nil then
  95.         o:update(data)
  96.     end
  97.  
  98.     return o
  99. end
  100. setmetatable(M, { __call = M.new })
  101.  
  102. function M:copy()
  103.     local o = M:new()
  104.     o._H0 = self._H0:copy()
  105.     o._H1 = self._H1:copy()
  106.     o._H2 = self._H2:copy()
  107.     o._H3 = self._H3:copy()
  108.     o._H4 = self._H4:copy()
  109.     o._H5 = self._H5:copy()
  110.     o._H6 = self._H6:copy()
  111.     o._H7 = self._H7:copy()
  112.     o._data = self._data
  113.     o._len = self._len
  114.     return o
  115. end
  116.  
  117. function M:update(data)
  118.     local W
  119.     local temp1
  120.     local temp2
  121.     local a
  122.     local b
  123.     local c
  124.     local d
  125.     local e
  126.     local f
  127.     local g
  128.     local h
  129.  
  130.     if data == nil then
  131.         data = ""
  132.     end
  133.  
  134.     data = tostring(data)
  135.     self._len = self._len + #data
  136.     self._data = self._data .. data
  137.  
  138.     while #self._data >= 64 do
  139.         W = {}
  140.         for i=1,64,4 do
  141.             local j = #W+1
  142.             W[j] = u32(string.byte(self._data, i)) << 24
  143.             W[j] = W[j] | u32(string.byte(self._data, i+1)) << 16
  144.             W[j] = W[j] | u32(string.byte(self._data, i+2)) << 8
  145.             W[j] = W[j] | u32(string.byte(self._data, i+3))
  146.         end
  147.         self._data = self._data:sub(65, #self._data)
  148.  
  149.         for i=17,64 do
  150.             W[i] = SSIG1(W[i-2]) + W[i-7] + SSIG0(W[i-15]) + W[i-16]
  151.         end
  152.  
  153.         a = self._H0
  154.         b = self._H1
  155.         c = self._H2
  156.         d = self._H3
  157.         e = self._H4
  158.         f = self._H5
  159.         g = self._H6
  160.         h = self._H7
  161.  
  162.         for i=1,64 do
  163.             temp1 = h + BSIG1(e) + CH(e, f, g) + K[i] + W[i]
  164.             temp2 = BSIG0(a) + MAJ(a, b, c)
  165.             h = g
  166.             g = f
  167.             f = e
  168.             e = d + temp1
  169.             d = c
  170.             c = b
  171.             b = a
  172.             a = temp1 + temp2
  173.         end
  174.  
  175.         self._H0 = self._H0 + a
  176.         self._H1 = self._H1 + b
  177.         self._H2 = self._H2 + c
  178.         self._H3 = self._H3 + d
  179.         self._H4 = self._H4 + e
  180.         self._H5 = self._H5 + f
  181.         self._H6 = self._H6 + g
  182.         self._H7 = self._H7 + h
  183.     end
  184. end
  185.  
  186. function M:digest()
  187.     local final
  188.     local data
  189.     local len = 0
  190.     local padlen = 0
  191.  
  192.     final = self:copy()
  193.  
  194.     padlen = final._len % 64
  195.     if padlen < 56 then
  196.         padlen = 56 - padlen
  197.     else
  198.         padlen = 120 - padlen
  199.     end
  200.  
  201.     len = final._len * 8
  202.     data = string.char(1<<7) ..
  203.         string.rep(string.char(0), padlen-1) ..
  204.         string.char(len >> 56 & 0xFF) ..
  205.         string.char(len >> 48 & 0xFF) ..
  206.         string.char(len >> 40 & 0xFF) ..
  207.         string.char(len >> 32 & 0xFF) ..
  208.         string.char(len >> 24 & 0xFF) ..
  209.         string.char(len >> 16 & 0xFF) ..
  210.         string.char(len >> 8 & 0xFF) ..
  211.         string.char(len & 0xFF)
  212.  
  213.     final:update(data)
  214.  
  215.     return final._H0:asbytestring() ..
  216.         final._H1:asbytestring() ..
  217.         final._H2:asbytestring() ..
  218.         final._H3:asbytestring() ..
  219.         final._H4:asbytestring() ..
  220.         final._H5:asbytestring() ..
  221.         final._H6:asbytestring() ..
  222.         final._H7:asbytestring()
  223. end
  224.  
  225. function M:hexdigest()
  226.     local h
  227.     local out = {}
  228.  
  229.     h = self:digest()
  230.     for i=1,#h do
  231.         out[i] = string.format("%02X", string.byte(h, i))
  232.     end
  233.     return table.concat(out)
  234. end
  235.  
  236. return M
  237.  
RAW Paste Data