Advertisement
ndfjay

Kernal

Jan 4th, 2015
1,331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.09 KB | None | 0 0
  1. --Kernal
  2.  
  3. --FLib API
  4.  
  5. function exists(path)
  6.         local file = assert(io.open(path, "r"))
  7.         if fs.exists(path) then
  8.                 return true
  9.         end
  10.        
  11.         return false
  12. end
  13.  
  14. function getTable(path)
  15.         if exists(path) then
  16.                 local file = io.open(path, "r")
  17.                 local lines = {}
  18.                 local i = 1
  19.                 local line = file:read("*l")
  20.                 while line ~= nil do
  21.                         lines[i] = line
  22.                         line = file:read("*l")
  23.                         i = i + 1
  24.                 end
  25.                 file:close()
  26.                 return lines
  27.         end
  28.         return {}
  29. end
  30.  
  31. function getLine(path, n)
  32.         if exists(path) then
  33.                 local lines = getTable(path)
  34.                 return lines[n]
  35.         end
  36.         return ""
  37. end
  38.  
  39. function getText(path)
  40.         if exists(path) then
  41.                 local file = assert(io.open(path, "r"))
  42.                 return file:read("*a")
  43.         end
  44.         return ""
  45. end
  46.  
  47. function fappend(path, text)
  48.         local file = assert(io.open(path, "a"))
  49.         file:write(text.."\n")
  50.         file:close()
  51. end
  52.  
  53. function fwrite(path, text)
  54.         local file = assert(io.open(path, "w"))
  55.         file:write(text)
  56.         file:close()
  57. end
  58.  
  59. function fwriteAtStart(path, text)
  60.         local _text = getText(path)
  61.         fwrite(path, text.."\n".._text)
  62. end
  63.  
  64. function fwriteFromTable(path, t)
  65.         local text = ""
  66.         for _, line in pairs(t) do
  67.                 text = text..line.."\n"
  68.         end
  69.         fwrite(path, text)
  70. end
  71.  
  72. function fappendFromTable(path, t)
  73.         local text = ""
  74.         for _, line in pairs(t) do
  75.                 text = text..line.."\n"
  76.         end
  77.         fappend(path, text)
  78. end
  79.  
  80. function fwriteAtStartFromTable(path, t)
  81.         local text = ""
  82.         for _, line in pairs(t) do
  83.                 text = text..line.."\n"
  84.         end
  85.         fwriteAtStart(path, text)
  86. end
  87.  
  88. function replaceLine(path, n, text)
  89.         local lines = getTable(path)
  90.         lines[n] = text
  91.         fwriteFromTable(path, lines)
  92. end
  93.  
  94. function getName(path)
  95.         if exists(path) then
  96.                 local lastSlashPos = 1
  97.                 for i = 1, path:len() do
  98.                         if path:sub(i, i) == "/" then
  99.                                 lastSlashPos = i
  100.                         end
  101.                 end
  102.                
  103.                 return path:sub(lastSlashPos + 1)
  104.         end
  105.         return ""
  106. end
  107.  
  108. function getPath(path)
  109.         if exists(path) then
  110.                 local lastSlashPos = 1
  111.                 for i = 1, path:len() do
  112.                         if path:sub(i, i) == "/" then
  113.                                 lastSlashPos = i
  114.                         end
  115.                 end
  116.                
  117.                 return path:sub(1, lastSlashPos)
  118.         end
  119.         return ""
  120. end
  121.  
  122. function fremove(path)
  123.         os.remove(path)
  124. end
  125.  
  126. -- SHA256 encryption
  127.  
  128.  
  129. --  
  130. --  Adaptation of the Secure Hashing Algorithm (SHA-244/256)
  131. --  Found Here: http://lua-users.org/wiki/SecureHashAlgorithm
  132. --  
  133. --  Using an adapted version of the bit library
  134. --  Found Here: https://bitbucket.org/Boolsheet/bslf/src/1ee664885805/bit.lua
  135. --  
  136.  
  137. local MOD = 2^32
  138. local MODM = MOD-1
  139.  
  140. local function memoize(f)
  141.         local mt = {}
  142.         local t = setmetatable({}, mt)
  143.         function mt:__index(k)
  144.                 local v = f(k)
  145.                 t[k] = v
  146.                 return v
  147.         end
  148.         return t
  149. end
  150.  
  151. local function make_bitop_uncached(t, m)
  152.         local function bitop(a, b)
  153.                 local res,p = 0,1
  154.                 while a ~= 0 and b ~= 0 do
  155.                         local am, bm = a % m, b % m
  156.                         res = res + t[am][bm] * p
  157.                         a = (a - am) / m
  158.                         b = (b - bm) / m
  159.                         p = p*m
  160.                 end
  161.                 res = res + (a + b) * p
  162.                 return res
  163.         end
  164.         return bitop
  165. end
  166.  
  167. local function make_bitop(t)
  168.         local op1 = make_bitop_uncached(t,2^1)
  169.         local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  170.         return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  171. end
  172.  
  173. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  174.  
  175. local function bxor(a, b, c, ...)
  176.         local z = nil
  177.         if b then
  178.                 a = a % MOD
  179.                 b = b % MOD
  180.                 z = bxor1(a, b)
  181.                 if c then z = bxor(z, c, ...) end
  182.                 return z
  183.         elseif a then return a % MOD
  184.         else return 0 end
  185. end
  186.  
  187. local function band(a, b, c, ...)
  188.         local z
  189.         if b then
  190.                 a = a % MOD
  191.                 b = b % MOD
  192.                 z = ((a + b) - bxor1(a,b)) / 2
  193.                 if c then z = bit32_band(z, c, ...) end
  194.                 return z
  195.         elseif a then return a % MOD
  196.         else return MODM end
  197. end
  198.  
  199. local function bnot(x) return (-1 - x) % MOD end
  200.  
  201. local function rshift1(a, disp)
  202.         if disp < 0 then return lshift(a,-disp) end
  203.         return math.floor(a % 2 ^ 32 / 2 ^ disp)
  204. end
  205.  
  206. local function rshift(x, disp)
  207.         if disp > 31 or disp < -31 then return 0 end
  208.         return rshift1(x % MOD, disp)
  209. end
  210.  
  211. local function lshift(a, disp)
  212.         if disp < 0 then return rshift(a,-disp) end
  213.         return (a * 2 ^ disp) % 2 ^ 32
  214. end
  215.  
  216. local function rrotate(x, disp)
  217.     x = x % MOD
  218.     disp = disp % 32
  219.     local low = band(x, 2 ^ disp - 1)
  220.     return rshift(x, disp) + lshift(low, 32 - disp)
  221. end
  222.  
  223. local k = {
  224.         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  225.         0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  226.         0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  227.         0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  228.         0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  229.         0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  230.         0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  231.         0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  232.         0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  233.         0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  234.         0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  235.         0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  236.         0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  237.         0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  238.         0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  239.         0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  240. }
  241.  
  242. local function str2hexa(s)
  243.         return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  244. end
  245.  
  246. local function num2s(l, n)
  247.         local s = ""
  248.         for i = 1, n do
  249.                 local rem = l % 256
  250.                 s = string.char(rem) .. s
  251.                 l = (l - rem) / 256
  252.         end
  253.         return s
  254. end
  255.  
  256. local function s232num(s, i)
  257.         local n = 0
  258.         for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  259.         return n
  260. end
  261.  
  262. local function preproc(msg, len)
  263.         local extra = 64 - ((len + 9) % 64)
  264.         len = num2s(8 * len, 8)
  265.         msg = msg .. "\128" .. string.rep("\0", extra) .. len
  266.         assert(#msg % 64 == 0)
  267.         return msg
  268. end
  269.  
  270. local function initH256(H)
  271.         H[1] = 0x6a09e667
  272.         H[2] = 0xbb67ae85
  273.         H[3] = 0x3c6ef372
  274.         H[4] = 0xa54ff53a
  275.         H[5] = 0x510e527f
  276.         H[6] = 0x9b05688c
  277.         H[7] = 0x1f83d9ab
  278.         H[8] = 0x5be0cd19
  279.         return H
  280. end
  281.  
  282. local function digestblock(msg, i, H)
  283.         local w = {}
  284.         for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  285.         for j = 17, 64 do
  286.                 local v = w[j - 15]
  287.                 local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  288.                 v = w[j - 2]
  289.                 w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  290.         end
  291.  
  292.         local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  293.         for i = 1, 64 do
  294.                 local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  295.                 local maj = bxor(band(a, b), band(a, c), band(b, c))
  296.                 local t2 = s0 + maj
  297.                 local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  298.                 local ch = bxor (band(e, f), band(bnot(e), g))
  299.                 local t1 = h + s1 + ch + k[i] + w[i]
  300.                 h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  301.         end
  302.  
  303.         H[1] = band(H[1] + a)
  304.         H[2] = band(H[2] + b)
  305.         H[3] = band(H[3] + c)
  306.         H[4] = band(H[4] + d)
  307.         H[5] = band(H[5] + e)
  308.         H[6] = band(H[6] + f)
  309.         H[7] = band(H[7] + g)
  310.         H[8] = band(H[8] + h)
  311. end
  312.  
  313. local function sha256(msg)
  314.         msg = preproc(msg, #msg)
  315.         local H = initH256({})
  316.         for i = 1, #msg, 64 do digestblock(msg, i, H) end
  317.         return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  318.                 num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  319. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement