Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.47 KB | None | 0 0
  1. local saltlen = 20
  2.  
  3. local oldPE = os.pullEvent
  4. os.pullEvent = os.pullEventRaw
  5. local tx,ty = term.getSize()
  6. local sha256
  7. function buildHasher()
  8. local MOD = 2^32
  9. local MODM = MOD-1
  10.  
  11. local function memoize(f)
  12. local mt = {}
  13. local t = setmetatable({}, mt)
  14. function mt:__index(k)
  15. local v = f(k)
  16. t[k] = v
  17. return v
  18. end
  19. return t
  20. end
  21.  
  22. local function make_bitop_uncached(t, m)
  23. local function bitop(a, b)
  24. local res,p = 0,1
  25. while a ~= 0 and b ~= 0 do
  26. local am, bm = a % m, b % m
  27. res = res + t[am][bm] * p
  28. a = (a - am) / m
  29. b = (b - bm) / m
  30. p = p*m
  31. end
  32. res = res + (a + b) * p
  33. return res
  34. end
  35. return bitop
  36. end
  37.  
  38. local function make_bitop(t)
  39. local op1 = make_bitop_uncached(t,2^1)
  40. local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  41. return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  42. end
  43.  
  44. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  45.  
  46. local function bxor(a, b, c, ...)
  47. local z = nil
  48. if b then
  49. a = a % MOD
  50. b = b % MOD
  51. z = bxor1(a, b)
  52. if c then z = bxor(z, c, ...) end
  53. return z
  54. elseif a then return a % MOD
  55. else return 0 end
  56. end
  57.  
  58. local function band(a, b, c, ...)
  59. local z
  60. if b then
  61. a = a % MOD
  62. b = b % MOD
  63. z = ((a + b) - bxor1(a,b)) / 2
  64. if c then z = bit32_band(z, c, ...) end
  65. return z
  66. elseif a then return a % MOD
  67. else return MODM end
  68. end
  69.  
  70. local function bnot(x) return (-1 - x) % MOD end
  71.  
  72. local function rshift1(a, disp)
  73. if disp < 0 then return lshift(a,-disp) end
  74. return math.floor(a % 2 ^ 32 / 2 ^ disp)
  75. end
  76.  
  77. local function rshift(x, disp)
  78. if disp > 31 or disp < -31 then return 0 end
  79. return rshift1(x % MOD, disp)
  80. end
  81.  
  82. local function lshift(a, disp)
  83. if disp < 0 then return rshift(a,-disp) end
  84. return (a * 2 ^ disp) % 2 ^ 32
  85. end
  86.  
  87. local function rrotate(x, disp)
  88. x = x % MOD
  89. disp = disp % 32
  90. local low = band(x, 2 ^ disp - 1)
  91. return rshift(x, disp) + lshift(low, 32 - disp)
  92. end
  93.  
  94. local k = {
  95. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  96. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  97. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  98. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  99. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  100. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  101. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  102. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  103. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  104. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  105. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  106. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  107. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  108. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  109. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  110. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  111. }
  112.  
  113. local function str2hexa(s)
  114. return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  115. end
  116.  
  117. local function num2s(l, n)
  118. local s = ""
  119. for i = 1, n do
  120. local rem = l % 256
  121. s = string.char(rem) .. s
  122. l = (l - rem) / 256
  123. end
  124. return s
  125. end
  126.  
  127. local function s232num(s, i)
  128. local n = 0
  129. for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  130. return n
  131. end
  132.  
  133. local function preproc(msg, len)
  134. local extra = 64 - ((len + 9) % 64)
  135. len = num2s(8 * len, 8)
  136. msg = msg .. "\128" .. string.rep("\0", extra) .. len
  137. assert(#msg % 64 == 0)
  138. return msg
  139. end
  140.  
  141. local function initH256(H)
  142. H[1] = 0x6a09e667
  143. H[2] = 0xbb67ae85
  144. H[3] = 0x3c6ef372
  145. H[4] = 0xa54ff53a
  146. H[5] = 0x510e527f
  147. H[6] = 0x9b05688c
  148. H[7] = 0x1f83d9ab
  149. H[8] = 0x5be0cd19
  150. return H
  151. end
  152.  
  153. local function digestblock(msg, i, H)
  154. local w = {}
  155. for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  156. for j = 17, 64 do
  157. local v = w[j - 15]
  158. local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  159. v = w[j - 2]
  160. w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  161. end
  162.  
  163. 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]
  164. for i = 1, 64 do
  165. local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  166. local maj = bxor(band(a, b), band(a, c), band(b, c))
  167. local t2 = s0 + maj
  168. local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  169. local ch = bxor (band(e, f), band(bnot(e), g))
  170. local t1 = h + s1 + ch + k[i] + w[i]
  171. h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  172. end
  173.  
  174. H[1] = band(H[1] + a)
  175. H[2] = band(H[2] + b)
  176. H[3] = band(H[3] + c)
  177. H[4] = band(H[4] + d)
  178. H[5] = band(H[5] + e)
  179. H[6] = band(H[6] + f)
  180. H[7] = band(H[7] + g)
  181. H[8] = band(H[8] + h)
  182. end
  183.  
  184. sha256 = function(msg)
  185. msg = preproc(msg, #msg)
  186. local H = initH256({})
  187. for i = 1, #msg, 64 do digestblock(msg, i, H) end
  188. return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  189. num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  190. end
  191. end
  192. buildHasher()
  193.  
  194. function limitRead( nlim, cOverlay)
  195. cOverlay=cOverlay or nil
  196. local px,py = term.getCursorPos()
  197. local txt = ""
  198. local boxOffset = 0
  199. local strPos = 1
  200. term.setCursorBlink(true)
  201. while true do
  202. e,p1 = os.pullEvent()
  203. if e=="key" then
  204. local key = p1
  205. if key==keys.left then
  206. if strPos>1 then
  207. strPos=strPos-1
  208. if strPos-boxOffset==0 then
  209. boxOffset=boxOffset-1
  210. end
  211. end
  212. elseif key==keys.right then
  213. if strPos<=#txt then
  214. strPos=strPos+1
  215. if strPos-boxOffset>nlim then
  216. boxOffset=boxOffset+1
  217. end
  218. end
  219. elseif key==keys.enter then
  220. break
  221. elseif key==keys.backspace then
  222. if strPos~=1 then
  223. txt=txt:sub(1,strPos-2)..txt:sub(strPos,#txt)
  224. strPos=strPos-1
  225. if strPos-boxOffset==0 then
  226. boxOffset=boxOffset-1
  227. end
  228. end
  229. elseif key==keys.delete then
  230. if strPos-1~=#txt then
  231. txt=txt:sub(1,strPos-1)..txt:sub(strPos+1,#txt)
  232. end
  233. end
  234. elseif e=="char" then
  235. txt = txt:sub(1,strPos-1)..p1..txt:sub(strPos,#txt)
  236. strPos=strPos+1
  237. if strPos-1-boxOffset>=nlim then
  238. boxOffset=boxOffset+1
  239. end
  240. end
  241. term.setCursorPos(px,py)
  242. write(string.rep(" ",nlim))
  243. term.setCursorPos(px,py)
  244.  
  245. --print(cOverlay==nil and "Yep" or "Nope")
  246. --read()
  247. if cOverlay==nil then
  248. -- term.setTextColor(colors.white)
  249. write(txt:sub(boxOffset+1,boxOffset+nlim))
  250. -- write(txt)
  251. else
  252. write(string.rep(cOverlay,#txt):sub(boxOffset+1,nlim+boxOffset))
  253. end
  254. term.setCursorPos(px+strPos-1-boxOffset,py)
  255. end
  256. term.setCursorBlink(false)
  257. return txt
  258. end
  259.  
  260.  
  261.  
  262.  
  263. local tvals = {
  264. BMenu = {
  265. "Password Change",
  266. "New Password: "
  267. },
  268. PScrn = {
  269. "This computer is password protected.",
  270. "Password: "
  271. },
  272. }
  273.  
  274. local function generateSalt()
  275. local ref = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  276. local str = ""
  277. for i=1,saltlen do
  278. local pos = math.random(1,#ref)
  279. str = str..ref:sub(pos,pos)
  280. end
  281. return str
  282. end
  283.  
  284. local tColors = {
  285. backGround = colors.cyan,
  286. textColor = colors.black,
  287. }
  288.  
  289. local function runPassword()
  290. local file = fs.open(".pass","r")
  291. if file then
  292. local pass = file.readLine()
  293. local sfile = fs.open(".salt","r")
  294. local salt = sfile.readLine()
  295. if pass == nil then
  296. return false
  297. end
  298. sfile.close()
  299. file.close()
  300. --term.setBackgroundColor(colors.cyan)
  301. --term.setTextColor(colors.black)
  302. term.clear()
  303. term.setCursorPos(tx/2+1-#tvals.PScrn[1]/2,2)
  304. term.write("This computer is password protected.")
  305. term.setCursorPos(tx/2+1-#tvals.PScrn[2]/2,5)
  306. term.write("Password: ")
  307. term.setCursorPos(3,6)
  308. --term.setBackgroundColor(colors.lightGray)
  309. term.write(string.rep(" ",tx-4))
  310. term.setCursorPos(3,6)
  311. local inp = limitRead(tx-4,"*")
  312. if sha256(inp..salt) == pass then
  313. graphics()
  314. return true
  315. else
  316. return false
  317. end
  318. else
  319. graphics()
  320. end
  321. end
  322.  
  323. local function drawCentered(txt, y)
  324. term.setCursorPos(tx/2+1-#txt/2,y)
  325. term.write(txt)
  326. end
  327.  
  328.  
  329. --term.setBackgroundColor(colors.black)
  330. --term.setTextColor(colors.yellow)
  331. --term.clear()
  332. --term.setCursorPos(1,1)
  333. --print("Press any key to enter BIOS menu...")
  334. --os.startTimer(1)
  335. --local e = os.pullEvent()
  336.  
  337. --if e == "key" then
  338. function graphics()
  339. --term.setBackgroundColor(colors.cyan)
  340. --term.setTextColor(colors.black)
  341. term.clear()
  342. local sel = 1
  343. local mno = {
  344. ["Change Password"] = function()
  345. --term.setBackgroundColor(colors.cyan)
  346. --term.setTextColor(colors.black)
  347. term.clear()
  348. term.setCursorPos(tx/2+1-#tvals.BMenu[1]/2,2)
  349. term.write(tvals.BMenu[1])
  350. term.setCursorPos(tx/2+1-#tvals.BMenu[2]/2,5)
  351. term.write("New Password: ")
  352. term.setCursorPos(3,6)
  353. --term.setBackgroundColor(colors.lightGray)
  354. term.write(string.rep(" ",tx-4))
  355. term.setCursorPos(3,6)
  356. local inp = limitRead(tx-4,"*")
  357. local slt = generateSalt()
  358. local pfile = fs.open(".pass","w")
  359. local sfile = fs.open(".salt","w")
  360. pfile.write(sha256(inp..slt))
  361. sfile.write(slt)
  362. pfile.close()
  363. sfile.close()
  364. end,
  365. ["Remove Password"] = function()
  366. --term.setBackgroundColor(colors.cyan)
  367. --term.setTextColor(colors.black)
  368. term.clear()
  369. drawCentered("Are you sure? (y/n)",ty/2)
  370. while true do
  371. local e,k = os.pullEvent("key")
  372. if e=="key" then
  373. if k==keys.y then
  374. if fs.exists(".pass") then
  375. local t = fs.open(".pass","w")
  376. t.close()
  377. end
  378. if fs.exists(".salt") then
  379. local t = fs.open(".salt","w")
  380. t.close()
  381. end
  382. break
  383. elseif k==keys.n then
  384. break
  385. end
  386. end
  387. end
  388. end,
  389. ["Exit"] = function()
  390. if not ((fs.exists(".pass")) and (fs.exists(".salt"))) then
  391. local t = fs.open(".pass","w")
  392. t.close()
  393. local t= fs.open(".salt","w")
  394. t.close()
  395. end
  396. return "exit"
  397. end
  398. }
  399. local order = { ["Change Password"]=1,["Remove Password"]=2,["Exit"]=3 }
  400. local fns = {}
  401. for k,v in pairs(mno) do
  402. fns[order[k]]=v
  403. end
  404. while true do
  405. --term.setBackgroundColor(colors.cyan)
  406. --term.setTextColor(colors.black)
  407. term.clear()
  408. for k,v in pairs(mno) do
  409. if order[k]==sel then
  410. drawCentered("> "..k.." <",order[k]+2)
  411. else
  412. drawCentered(k,order[k]+2)
  413. end
  414. end
  415. --term.setBackgroundColor(colors.lightGray)
  416. term.setCursorPos(1,1)
  417. term.write(string.rep(" ",tx))
  418. drawCentered("BIOS Menu",1)
  419. local e,p1,p2,p3,p4,p5 = os.pullEvent()
  420. if e=="key" then
  421. local key = p1
  422. if key==keys.enter then
  423. local result = fns[sel]()
  424. if result == "exit" then
  425. break
  426. end
  427. elseif key==keys.up then
  428. sel = sel-1
  429. if sel<1 then
  430. sel = #fns
  431. end
  432. elseif key==keys.down then
  433. sel = sel+1
  434. if sel>#fns then
  435. sel = 1
  436. end
  437. end
  438. end
  439. end
  440. end
  441. if fs.exists(".salt") or fs.exists(".pass") then
  442. runPassword()
  443. else
  444. graphics()
  445. end
  446. if not fs.exists(".salt") then
  447. local temp = fs.open(".salt","w")
  448. temp.close()
  449. end
  450. if not fs.exists(".pass") then
  451. local temp = fs.open(".pass","w")
  452. temp.close()
  453. end
  454.  
  455.  
  456. --term.setBackgroundColor(colors.black)
  457. --term.setTextColor(colors.white)
  458. term.setCursorPos(1,1)
  459. term.clear()
  460. os.pullEvent = oldPE
  461.  
  462. local _resolveProgram = shell.resolveProgram
  463. local rawget = _G.rawget
  464.  
  465. local blockedPaths = {}
  466.  
  467. -- Paths that cannot be opened for writing,
  468. -- deleting, moving, or copying.
  469. blockedPaths[_resolveProgram ("/.pass")] = true
  470. blockedPaths[_resolveProgram ("/.salt")] = true
  471. --blockedPaths[_resolveProgram ("/pass")] = true
  472. blockedPaths[_resolveProgram ("/rom/autorun/password")] = true
  473.  
  474. local _fs = {
  475. -- Don't want to be able to delete
  476. -- or edit the contents.
  477. open = _G.fs.open,
  478. delete = _G.fs.delete,
  479. move = _G.fs.move,
  480. copy = _G.fs.copy
  481. }
  482.  
  483. local function resolveProgram (path)
  484. local pass, _error = pcall (rawget, path)
  485.  
  486. if not pass then
  487. return _resolveProgram (path)
  488. end
  489. end
  490.  
  491. -- Replace global fs functions with
  492. -- protected ones.
  493. for name, func in pairs (_fs) do
  494. _G.fs[name] = function (path, ...)
  495. path = resolveProgram (path)
  496.  
  497. if blockedPaths[path] then
  498. printError ("Cannont access " .. path .. "!")
  499. else
  500. return func (path, ...)
  501. end
  502. end
  503. end
  504.  
  505. --[[ Prevent metatable penetration of resolveProgram.
  506. local function resolveProgram (path)
  507. local pass, _error = pcall (rawget, path)
  508.  
  509. -- This is kind of sketchy.
  510. -- It only checks if rawget fails, meaning
  511. -- that the 'path' value wasn't a table.
  512. -- Should work, though; if 'path' wasn't a table,
  513. -- then it can't have a metatable.
  514. if not pass then
  515. return _resolveProgram (path)
  516. end
  517. end]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement