KevY007

DiskLock.lua (pastebin get as startup)

Jul 25th, 2016
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.14 KB | None | 0 0
  1. ------ Credits to Anavrins for SHA-256 in LUA API (Without him this = impossible)
  2. --- Please rush down this is just SHA-256 code
  3. local mod32 = 2^32
  4. local sha_hashlen = 32
  5. local sha_blocksize = 64
  6.  
  7. local band    = bit32 and bit32.band or bit.band
  8. local bnot    = bit32 and bit32.bnot or bit.bnot
  9. local bxor    = bit32 and bit32.bxor or bit.bxor
  10. local blshift = bit32 and bit32.lshift or bit.blshift
  11. local upack   = unpack
  12.  
  13. local function rrotate(n, b)
  14.   local s = n/(2^b)
  15.   local f = s%1
  16.   return (s-f) + f*mod32
  17. end
  18. local function brshift(int, by) -- Thanks bit32 for bad rshift
  19.   local s = int / (2^by)
  20.   return s - s%1
  21. end
  22.  
  23. local H = {
  24. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  25. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
  26. }
  27.  
  28. local K = {
  29. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  30. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  31. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  32. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  33. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  34. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  35. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  36. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  37. }
  38.  
  39. local function incr(t, incr)
  40.   if 0xFFFFFFFF - t[1] < incr then
  41.     t[2] = t[2] + 1
  42.     t[1] = incr - (0xFFFFFFFF - t[1]) - 1  
  43.     else t[1] = t[1] + incr
  44.     end
  45.     return t
  46.   end
  47.  
  48.   local function preprocess(data)
  49.    local len = #data
  50.  
  51.    data[#data+1] = 0x80
  52.    while #data%64~=56 do data[#data+1] = 0 end
  53.    local l = incr({0,0}, len*8)
  54.    for i = 2, 1, -1 do
  55.     data[#data+1] = band(brshift(band(l[i], 0xFF000000), 24), 0xFF)
  56.     data[#data+1] = band(brshift(band(l[i], 0xFF0000), 16), 0xFF)
  57.     data[#data+1] = band(brshift(band(l[i], 0xFF00), 8), 0xFF)
  58.     data[#data+1] = band(l[i], 0xFF)
  59.   end
  60.   return data
  61. end
  62.  
  63. local function BE_toInt(bs, i)
  64.   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)
  65. end
  66.  
  67. local function digestblock(data, i, C)
  68.   local w = {}
  69.   for j = 1, 16 do w[j] = BE_toInt(data, i+(j-1)*4) end
  70.   for j = 17, 64 do
  71.     local v = w[j-15]
  72.     local s0 = bxor(bxor(rrotate(w[j-15], 7), rrotate(w[j-15], 18)), brshift(w[j-15], 3))
  73.     local s1 = bxor(bxor(rrotate(w[j-2], 17), rrotate(w[j-2], 19)), brshift(w[j-2], 10))
  74.     w[j] = (w[j-16] + s0 + w[j-7] + s1)%mod32
  75.   end
  76.   local a, b, c, d, e, f, g, h = upack(C)
  77.   for j = 1, 64 do
  78.     local S1 = bxor(bxor(rrotate(e, 6), rrotate(e, 11)), rrotate(e, 25))
  79.     local ch = bxor(band(e, f), band(bnot(e), g))
  80.     local temp1 = (h + S1 + ch + K[j] + w[j])%mod32
  81.     local S0 = bxor(bxor(rrotate(a, 2), rrotate(a, 13)), rrotate(a, 22))
  82.     local maj = bxor(bxor(band(a, b), band(a, c)), band(b, c))
  83.     local temp2 = (S0 + maj)%mod32
  84.     h, g, f, e, d, c, b, a = g, f, e, (d+temp1)%mod32, c, b, a, (temp1+temp2)%mod32
  85.   end
  86.   C[1] = (C[1] + a)%mod32
  87.   C[2] = (C[2] + b)%mod32
  88.   C[3] = (C[3] + c)%mod32
  89.   C[4] = (C[4] + d)%mod32
  90.   C[5] = (C[5] + e)%mod32
  91.   C[6] = (C[6] + f)%mod32
  92.   C[7] = (C[7] + g)%mod32
  93.   C[8] = (C[8] + h)%mod32
  94.   return C
  95. end
  96.  
  97. local mt = {
  98. __tostring = function(a) return string.char(unpack(a)) end,
  99. __index = {
  100. toHex = function(self, s) return ("%02x"):rep(#self):format(unpack(self)) end,
  101. isEqual = function(self, t)
  102.  if type(t) ~= "table" then return false end
  103.  if #self ~= #t then return false end
  104.  local ret = 0
  105.  for i = 1, #self do
  106.   ret = bit32.bor(ret, bxor(self[i], t[i]))
  107. end
  108. return ret == 0
  109. end
  110. }
  111. }
  112.  
  113. local function toBytes(t, n)
  114.   local b = {}
  115.   for i = 1, n do
  116.     b[(i-1)*4+1] = band(brshift(band(t[i], 0xFF000000), 24), 0xFF)
  117.     b[(i-1)*4+2] = band(brshift(band(t[i], 0xFF0000), 16), 0xFF)
  118.     b[(i-1)*4+3] = band(brshift(band(t[i], 0xFF00), 8), 0xFF)
  119.     b[(i-1)*4+4] = band(t[i], 0xFF)
  120.   end
  121.   return setmetatable(b, mt)
  122. end
  123.  
  124. function digest(data)
  125.   data = data or ""
  126.   data = type(data) == "string" and {data:byte(1,-1)} or data
  127.  
  128.   data = preprocess(data)
  129.   local C = {upack(H)}
  130.   for i = 1, #data, 64 do C = digestblock(data, i, C) end
  131.   return toBytes(C, 8)
  132. end
  133.  
  134. function hmac(data, key)
  135.   local data = type(data) == "table" and {upack(data)} or {tostring(data):byte(1,-1)}
  136.   local key = type(key) == "table" and {upack(key)} or {tostring(key):byte(1,-1)}
  137.  
  138.   local blocksize = sha_blocksize
  139.  
  140.   key = #key > blocksize and digest(key) or key
  141.  
  142.   local ipad = {}
  143.   local opad = {}
  144.   local padded_key = {}
  145.  
  146.   for i = 1, blocksize do
  147.     ipad[i] = bxor(0x36, key[i] or 0)
  148.     opad[i] = bxor(0x5C, key[i] or 0)
  149.   end
  150.  
  151.   for i = 1, #data do
  152.     ipad[blocksize+i] = data[i]
  153.   end
  154.  
  155.   ipad = digest(ipad)
  156.  
  157.   for i = 1, blocksize do
  158.     padded_key[i] = opad[i]
  159.     padded_key[blocksize+i] = ipad[i]
  160.   end
  161.  
  162.   return digest(padded_key)
  163. end
  164.  
  165. function pbkdf2(pass, salt, iter, dklen)
  166.   local out = {}
  167.   local hashlen = sha_hashlen
  168.   local block = 1
  169.   dklen = dklen or 32
  170.  
  171.   while dklen > 0 do
  172.     local ikey = {}
  173.     local isalt = type(salt) == "table" and {upack(salt)} or {tostring(salt):byte(1,-1)}
  174.     local clen = dklen > hashlen and hashlen or dklen
  175.  
  176.     isalt[#isalt+1] = band(brshift(band(block, 0xFF000000), 24), 0xFF)
  177.     isalt[#isalt+1] = band(brshift(band(block, 0xFF0000), 16), 0xFF)
  178.     isalt[#isalt+1] = band(brshift(band(block, 0xFF00), 8), 0xFF)
  179.     isalt[#isalt+1] = band(block, 0xFF)
  180.  
  181.     for j = 1, iter do
  182.       isalt = hmac(isalt, pass)
  183.       for k = 1, clen do ikey[k] = bxor(isalt[k], ikey[k] or 0) end
  184.       if j % 200 == 0 then os.queueEvent("PBKDF2", j) coroutine.yield("PBKDF2") end
  185.     end
  186.     dklen = dklen - clen
  187.     block = block+1
  188.     for k = 1, clen do out[#out+1] = ikey[k] end
  189.   end
  190.  
  191.   return setmetatable(out, mt)
  192. end
  193. --[[----------------------------------------------------------
  194.                  Main Functions Begin
  195.                   SHA-256 Code END
  196. -----------------------------------------------------------]]--
  197. token = "def_tk_010sk25jsmiq25jgsm23915asddrasdf" -- If you edit this, remember it and edit this in your disk lost password too! else u wont be able to ever recover password.
  198. function encrypt(str, saltt)
  199.   return pbkdf2(str, saltt, 128):toHex()
  200. end
  201. oldPullEvent = os.pullEvent
  202. os.pullEvent = os.pullEventRaw
  203. function CheckInstallation()
  204.   print("NOTE: Disk's must be the first one attached")
  205.   sleep(0.5)
  206.   if not fs.exists("DiskLock.shx") then
  207.     term.clear()
  208.     term.setCursorPos(1,1)
  209.     print("Do you want to create a computer lock?")
  210.     print("Valid Values: yes/no (Case-Sensitive)")
  211.     val = read()
  212.     if val == "yes" then
  213.       term.clear()
  214.       term.setCursorPos(1, 1)
  215.       if not fs.exists("/disk/") then
  216.         oldCol = term.getTextColor()
  217.         if term.isColor() then
  218.           term.setTextColor(colors.blue)
  219.         end
  220.         print("Please insert your disk, After press enter to continue")
  221.         if term.isColor() then
  222.           term.setTextColor(colors.black)
  223.         end
  224.         entr = read("")
  225.         if term.isColor() then
  226.           term.setTextColor(oldCol)
  227.         end
  228.         CheckInstallation()
  229.       else
  230.         oldCol = term.getTextColor()
  231.         if term.isColor() then
  232.           term.setTextColor(colors.lightBlue)
  233.         end
  234.         print("Found Disc Continuing...")
  235.         if term.isColor() then
  236.           term.setTextColor(oldCol)
  237.         end
  238.         sleep(0.3)
  239.         term.clear()
  240.         term.setCursorPos(1, 1)
  241.         oldCol = term.getTextColor()
  242.         if term.isColor() then
  243.           term.setTextColor(colors.orange)
  244.         end
  245.         print("Please insert your desired password!")
  246.         print("<!> Remember this since it can create your another password disk!")
  247.         if term.isColor() then
  248.           term.setTextColor(colors.green)
  249.         end
  250.         pass = read("*")
  251.         if term.isColor()
  252.           then term.setTextColor(oldCol)
  253.         end
  254.         if pass == nil or pass == "" or pass == " " or pass == "123" or pass == "abc" then
  255.           sleep(1.0)
  256.           term.clear()
  257.           term.setCursorPos(1, 1)
  258.           oldCol = term.getTextColor()
  259.           if term.isColor() then
  260.             term.setTextColor(colors.red)
  261.           end
  262.           print("Its a dumb password!")
  263.           if term.isColor() then
  264.             term.setTextColor(oldCol)
  265.           end
  266.           error()
  267.         else
  268.           handle = fs.open("DiskLock.shx", "w")
  269.           compId = os.computerID()
  270.           idwpass = compId ..":" ..token
  271.           term.clear()
  272.           if term.isColor() then
  273.             oco2 = term.getTextColor()
  274.             term.setTextColor(colors.yellow)
  275.           end
  276.           print("Encrypting Now, It will take some time but you will get better results too!")
  277.           if term.isColor() then
  278.             term.setTextColor(oco2)
  279.           end
  280.           enc_pass = encrypt(pass, idwpass)
  281.           handle.write(enc_pass)
  282.           handle.close()
  283.           if fs.exists("disk/DiskLock.shc") then
  284.             fs.delete("disk/DiskLock.shc")
  285.           end
  286.           handle = fs.open("disk/DiskLock.shc", "w")
  287.           handle.write(enc_pass)
  288.           handle.close()
  289.           shell.run("set shell.allow_disk_startup false")
  290.           term.clear()
  291.           term.setCursorPos(1, 1)
  292.           oldCol = term.getTextColor()
  293.           if term.isColor() then
  294.             term.setTextColor(colors.green)
  295.           end
  296.           print("<!> " ..tostring(compId) .. " is your computer id, Remember it to recover password disk")
  297.           sleep(5)
  298.           term.clear()
  299.           print("Success, Rebooting now to apply changes...")
  300.           if term.isColor() then
  301.             term.setTextColor(oldCol)
  302.           end
  303.           sleep(1)
  304.           os.reboot()
  305.         end
  306.       end
  307.     elseif val == "no" then
  308.       term.clear()
  309.       term.setCursorPos(1, 1)
  310.       oldCol = term.getTextColor()
  311.       if term.isColor() then
  312.         term.setTextColor(colors.red)
  313.       end
  314.       print("You chose NO, Continuing to Boot")
  315.       if term.isColor() then
  316.         term.setTextColor(oldCol)
  317.       end
  318.       error()
  319.     else
  320.       term.clear()
  321.       term.setCursorPos(1, 1)
  322.       oldCol = term.getTextColor()
  323.       if term.isColor() then
  324.         term.setTextColor(colors.blue)
  325.       end
  326.       print("Unknown/Idiotic Value...")
  327.       if term.isColor() then
  328.         term.setTextColor(oldCol)
  329.       end
  330.       error()
  331.     end
  332.   end
  333. end
  334. function CheckLogin()
  335.   term.clear()
  336.   term.setCursorPos(1,1)
  337.   if fs.exists("DiskLock.shx") then
  338.     shell.run("set shell.allow_disk_startup false")
  339.     term.clear()
  340.     term.setCursorPos(1, 1)
  341.     if not fs.exists("/disk/") then
  342.       term.clear()
  343.       oldCol = term.getTextColor()
  344.       if term.isColor() then
  345.         term.setTextColor(colors.red)
  346.       end
  347.       print("Welcome, Please insert the login disk!")
  348.       if term.isColor() then
  349.         term.setTextColor(colors.yellow)
  350.       end
  351.       print("Press enter once done.")
  352.       if term.isColor() then
  353.         term.setTextColor(colors.black)
  354.       end
  355.       xazz = read("")
  356.       if term.isColor() then
  357.         term.setTextColor(oldCol)
  358.       end
  359.       CheckLogin()
  360.     else
  361.       oldCol = term.getTextColor()
  362.       if term.isColor() then
  363.         term.setTextColor(colors.orange) end
  364.         print("Reading Disk.....")
  365.         if term.isColor() then
  366.           term.setTextColor(oldCol)
  367.         end
  368.         sleep(1)
  369.         if not fs.exists("disk/DiskLock.shc") then
  370.           oldCol = term.getTextColor()
  371.           if term.isColor() then
  372.             term.setTextColor(colors.red)
  373.           end
  374.           print("This disk does not contain login files!")
  375.           if term.isColor() then
  376.             term.setTextColor(oldCol)
  377.           end
  378.           sleep(1)
  379.           CheckLogin()
  380.         else
  381.           pathTofile = shell.resolve("disk/DiskLock.shc")
  382.           file = fs.open(pathTofile, "r")
  383.           if file then
  384.             diskdata = file.readAll()
  385.             file.close()
  386.             p2f2 = shell.resolve("DiskLock.shx")
  387.             file2 = fs.open(p2f2, "r")
  388.             if not file2 then
  389.              oldCol = term.getTextColor()
  390.              if term.isColor() then
  391.               term.setTextColor(colors.red)
  392.             end
  393.             print("Computer failed to read the login data, File.IO Failure!")
  394.             print("Computer has not continued to boot.")
  395.             if term.isColor() then
  396.               term.setTextColor(oldCol)
  397.             end
  398.           end
  399.           logindata = file2.readAll()
  400.           file2.close()
  401.           if (diskdata == logindata) then
  402.             oldCol = term.getTextColor()
  403.             if term.isColor() then
  404.               term.setTextColor(colors.red)
  405.             end
  406.             shell.run("eject", "left")
  407.             shell.run("eject", "right")
  408.             shell.run("eject", "top")
  409.             shell.run("eject", "bottom")
  410.             term.clear()
  411.             oldCol = term.getTextColor()
  412.             if term.isColor() then
  413.                 term.setTextColor(colors.green)
  414.             end
  415.             print("Welcome back!")
  416.             if term.isColor() then
  417.               term.setTextColor(oldCol)
  418.             end
  419.             os.pullEvent = oldPullEvent
  420.             error()
  421.           else
  422.             oldCol = term.getTextColor()
  423.             if term.isColor() then
  424.               term.setTextColor(colors.red)
  425.             end
  426.             print("The login data dosen't match!")
  427.             if term.isColor() then
  428.               term.setTextColor(oldCol)
  429.             end
  430.             sleep(1)
  431.             CheckLogin()
  432.           end
  433.         else
  434.          oldCol = term.getTextColor()
  435.          if term.isColor() then
  436.           term.setTextColor(colors.red)
  437.         end
  438.         print("Failed to read the disk file!")
  439.         if term.isColor() then
  440.           term.setTextColor(oldCol)
  441.         end
  442.         sleep(1)
  443.         CheckLogin()
  444.       end
  445.     end
  446.   end
  447. end
  448. end
  449. CheckInstallation()
  450. CheckLogin()
Add Comment
Please, Sign In to add comment