Advertisement
Anavrins

SHA256/HMAC/PBKDF2

Feb 19th, 2016 (edited)
8,489
4
Never
6
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.16 KB | Source Code | 4 0
  1. -- SHA-256, HMAC and PBKDF2 functions in ComputerCraft
  2. -- By Anavrins
  3. -- MIT License
  4. -- Pastebin: https://pastebin.com/6UV4qfNF
  5. -- Usage: https://pastebin.com/q2SQ7eRg
  6. -- Last updated: June 30 2024
  7.  
  8. local band    = bit32 and bit32.band or bit.band
  9. local bnot    = bit32 and bit32.bnot or bit.bnot
  10. local bxor    = bit32 and bit32.bxor or bit.bxor
  11. local blshift = bit32 and bit32.lshift or bit.blshift
  12. local upack   = table.unpack or unpack
  13.  
  14. local function rrotate(n, b)
  15.     local s = n/(2^b)
  16.     local f = s%1
  17.     return (s-f) + f * 2^32
  18. end
  19. local function brshift(int, by)
  20.     local s = int / (2^by)
  21.     return s - s%1
  22. end
  23.  
  24. local H = { -- First 32 bits of the fractional parts of the square roots of the first 8 primes 2..19
  25.     0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  26.     0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
  27. }
  28.  
  29. local K = { -- First 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311
  30.     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  31.     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  32.     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  33.     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  34.     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  35.     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  36.     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  37.     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  38. }
  39.  
  40. local function counter(incr)
  41.     local t1, t2 = 0, 0
  42.     if 0xFFFFFFFF - t1 < incr then
  43.         t2 = t2 + 1
  44.         t1 = incr - (0xFFFFFFFF - t1) - 1
  45.     else t1 = t1 + incr
  46.     end
  47.     return t2, t1
  48. end
  49.  
  50. local function BE_toInt(bs, i)
  51.     return blshift((bs[i] or 0), 24) + blshift((bs[i+1] or 0), 16) + blshift((bs[i+2] or 0), 8) + (bs[i+3] or 0)
  52. end
  53.  
  54. local function preprocess(data)
  55.     local len = #data
  56.     local proc = {}
  57.     data[#data+1] = 0x80
  58.     while #data%64~=56 do data[#data+1] = 0 end
  59.     local blocks = math.ceil(#data/64)
  60.     for i = 1, blocks do
  61.         proc[i] = {}
  62.         for j = 1, 16 do
  63.             proc[i][j] = BE_toInt(data, 1+((i-1)*64)+((j-1)*4))
  64.         end
  65.     end
  66.     proc[blocks][15], proc[blocks][16] = counter(len*8)
  67.     return proc
  68. end
  69.  
  70. local function digestblock(w, C)
  71.     for j = 17, 64 do
  72.         local v = w[j-15]
  73.         local u = w[j-2]
  74.         local s0 = bxor(rrotate(v, 7), rrotate(v, 18), brshift(v, 3))
  75.         local s1 = bxor(rrotate(u, 17), rrotate(u, 19), brshift(u, 10))
  76.         w[j] = (w[j-16] + s0 + w[j-7] + s1) % 2^32
  77.     end
  78.     local a, b, c, d, e, f, g, h = upack(C)
  79.     for j = 1, 64 do
  80.         local S1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  81.         local ch = bxor(band(e, f), band(bnot(e), g))
  82.         local temp1 = (h + S1 + ch + K[j] + w[j])
  83.         local S0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  84.         local maj = bxor(bxor(band(a, b), band(a, c)), band(b, c))
  85.         local temp2 = (S0 + maj)
  86.         h, g, f, e, d, c, b, a = g, f, e, (d+temp1) % 2^32, c, b, a, (temp1+temp2) % 2^32
  87.     end
  88.     C[1] = (C[1] + a) % 2^32
  89.     C[2] = (C[2] + b) % 2^32
  90.     C[3] = (C[3] + c) % 2^32
  91.     C[4] = (C[4] + d) % 2^32
  92.     C[5] = (C[5] + e) % 2^32
  93.     C[6] = (C[6] + f) % 2^32
  94.     C[7] = (C[7] + g) % 2^32
  95.     C[8] = (C[8] + h) % 2^32
  96.     return C
  97. end
  98.  
  99. local mt = {}
  100. mt = {
  101.     __tostring = function(a) return string.char(upack(a)) end,
  102.     __index = {
  103.         toHex = function(self) return ("%02x"):rep(#self):format(upack(self)) end,
  104.         isEqual = function(self, t)
  105.             if type(t) ~= "table" then return false end
  106.             if #self ~= #t then return false end
  107.             local ret = 0
  108.             for i = 1, #self do
  109.                 ret = bit32.bor(ret, bxor(self[i], t[i]))
  110.             end
  111.             return ret == 0
  112.         end,
  113.         sub = function(self, a, b)
  114.             local len = #self+1
  115.             local start = a%len
  116.             local stop = (b or len-1)%len
  117.             local ret = {}
  118.             local i = 1
  119.             for j = start, stop, start<stop and 1 or -1 do
  120.                 ret[i] = self[j]
  121.                 i = i+1
  122.             end
  123.             return setmetatable(ret, mt)
  124.         end,
  125.     }
  126. }
  127.  
  128. local function toBytes(t, n)
  129.     local b = {}
  130.     for i = 1, n do
  131.         b[(i-1)*4+1] = band(brshift(t[i], 24), 0xFF)
  132.         b[(i-1)*4+2] = band(brshift(t[i], 16), 0xFF)
  133.         b[(i-1)*4+3] = band(brshift(t[i], 8), 0xFF)
  134.         b[(i-1)*4+4] = band(t[i], 0xFF)
  135.     end
  136.     return setmetatable(b, mt)
  137. end
  138.  
  139. local function digest(data)
  140.     local data = data or ""
  141.     data = type(data) == "table" and {upack(data)} or {tostring(data):byte(1,-1)}
  142.  
  143.     data = preprocess(data)
  144.     local C = {upack(H)}
  145.     for i = 1, #data do C = digestblock(data[i], C) end
  146.     return toBytes(C, 8)
  147. end
  148.  
  149. local function hmac(data, key)
  150.     local data = type(data) == "table" and {upack(data)} or {tostring(data):byte(1,-1)}
  151.     local key = type(key) == "table" and {upack(key)} or {tostring(key):byte(1,-1)}
  152.  
  153.     local blocksize = 64
  154.  
  155.     key = #key > blocksize and digest(key) or key
  156.  
  157.     local ipad = {}
  158.     local opad = {}
  159.     local padded_key = {}
  160.  
  161.     for i = 1, blocksize do
  162.         ipad[i] = bxor(0x36, key[i] or 0)
  163.         opad[i] = bxor(0x5C, key[i] or 0)
  164.     end
  165.  
  166.     for i = 1, #data do
  167.         ipad[blocksize+i] = data[i]
  168.     end
  169.  
  170.     ipad = digest(ipad)
  171.  
  172.     for i = 1, blocksize do
  173.         padded_key[i] = opad[i]
  174.         padded_key[blocksize+i] = ipad[i]
  175.     end
  176.  
  177.     return digest(padded_key)
  178. end
  179.  
  180. local function pbkdf2(pass, salt, iter, dklen)
  181.     local salt = type(salt) == "table" and salt or {tostring(salt):byte(1,-1)}
  182.     local hashlen = 32
  183.     local dklen = dklen or 32
  184.     local block = 1
  185.     local out = {}
  186.  
  187.     while dklen > 0 do
  188.         local ikey = {}
  189.         local isalt = {upack(salt)}
  190.         local clen = dklen > hashlen and hashlen or dklen
  191.  
  192.         isalt[#isalt+1] = band(brshift(block, 24), 0xFF)
  193.         isalt[#isalt+1] = band(brshift(block, 16), 0xFF)
  194.         isalt[#isalt+1] = band(brshift(block, 8), 0xFF)
  195.         isalt[#isalt+1] = band(block, 0xFF)
  196.  
  197.         for j = 1, iter do
  198.             isalt = hmac(isalt, pass)
  199.             for k = 1, clen do ikey[k] = bxor(isalt[k], ikey[k] or 0) end
  200.             if j % 200 == 0 then os.queueEvent("PBKDF2", j) coroutine.yield("PBKDF2") end
  201.         end
  202.         dklen = dklen - clen
  203.         block = block+1
  204.         for k = 1, clen do out[#out+1] = ikey[k] end
  205.     end
  206.  
  207.     return setmetatable(out, mt)
  208. end
  209.  
  210. return {
  211.     digest = digest,
  212.     hmac   = hmac,
  213.     pbkdf2 = pbkdf2,
  214. }
  215.  
Advertisement
Comments
  • SecretPera
    2 years
    # text 0.07 KB | 0 0
    1. Hey man i need to ask you somethin privately.Where can i contact you from?
  • Anavrins
    1 year (edited)
    # text 0.13 KB | 0 0
    1. If your questions have anything to do with "Assetto Corsa", the quick answer is: no, I cannot decrypt or crack anything for you.
    • Octal
      364 days
      # text 0.11 KB | 0 0
      1. People were asking you about some random racing game? weird. Anyways: thanks for making this. Have a great time
  • 31Asha
    285 days
    # text 0.08 KB | 0 0
    1. Hey i want encrypt (server) my own cars on assetto corsa how i can contact you ?
    2.  
Add Comment
Please, Sign In to add comment
Advertisement