computercam

CCKernal

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