This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Jarle212's profile picture

Secure login screen[hashing]

Started by Jarle212, 27 January 2013 - 10:16 PM
Jarle212 #1
Posted 27 January 2013 - 11:16 PM
The screen is changeable, but not too dynamic
Pastebin: http://pastebin.com/UVA57ERQ
Raw code:
Spoiler

os.pullEvent = os.pullEventRaw
term.clear()
term.setCursorPos(1,1)
local lPassword = ""
local run = true
local passwordT = ""
local textBoxH = 8
local textbox = {}
local message = "Password protection"
textbox[1] = "/-----------------------\\"
textbox[2] = "|					   |"
textbox[3] = "\\-----------------------/"
--local inputbox = {}
--local inputbox[1] = "--------------"
--local inputboc[2] = "|"
local w,h = term.getSize()
function draw()
term.clear()
local posX = (w/2)-(string.len(textbox[1])/2)
local posY = (h/2)-(textBoxH/2)
local posX2 = (w/2) - (string.len(textbox[1])/4)-1
local posY2 = (h/2) + (textBoxH*0.2)
for i=1, string.len(passwordT) do
if i < string.len(textbox[1])/2 then
term.setCursorPos(posX2+i,posY2)
term.write("*")
end
end
for i=1, string.len(textbox[1])/2 do
--term.setCursorPos(posX2+i, posY2)
--term.write("*")
term.setCursorPos(posX2+i,posY2-1)
term.write("-")
term.setCursorPos(posX2+i,posY2+1)
term.write("-")
end
term.setCursorPos(posX2,posY2)
term.write("|")
term.setCursorPos(1+posX2+string.len(textbox[1])/2,posY2)
term.write("|")
term.setCursorPos((w/2)-(string.len(message)/2),posY2-3)
term.write(message)
term.setCursorPos(posX,posY)
term.write(textbox[1])
for i=1,textBoxH do
term.setCursorPos(posX,posY+i)
term.write("|")
term.setCursorPos(posX+string.len(textbox[1])-1,posY+i)
term.write("|")
end
term.setCursorPos(posX,posY+textBoxH+1)
term.write(textbox[3])
--Draw the input box

end
function update()
event,p1,p2,p3 = os.pullEvent()
--draw()
if event == "key" then
if  p1 == 14 then
  if string.len(passwordT) > 0 then
  passwordT = string.sub(passwordT,0,string.len(passwordT)-1)
  end
end
if p1 == 28 then
   if matchPasswords(cryptPassword(passwordT),lPassword) then
   message = "Access Granted!"
   draw()
   sleep(2)
   term.clear()
   term.setCursorPos(1,1)
   print("Change password?(1//0):")
   local chPass = tonumber(read())
   run = false
   if chPass == 1 then
   changePassword()
   end
else
message = "ACCESS DENIED!"
passwordT = ""
draw()
sleep(3)
message = "Password protection"
end

end
end
if event == "char" then
passwordT = passwordT .. p1
end
if event == "terminate" then
message = "ACCESS DENIED!"
draw()
sleep(5)
passwordT = ""
message = "Password protection"
--draw()
end
if event == "disk" then
message = "DISK NOT ALLOWED!"
disk.eject("right")
disk.eject("left")
disk.eject("bottom")
disk.eject("front")
disk.eject("back")
disk.eject("top")
sleep(5)
message = "Password protection"
end
draw()
end
function loadPassword()
local file = io.open("PF","r")
if not(file == nil) then
local password = file:read()
file:close()
return password
else
changePassword()
os.reboot()
--lPassword = loadPassword()
end
end
function changePassword()
term.clear()
term.setCursorPos(1,1)
nPass = "1"
nPass3 = ""
print("Type in new password:")
local nPass = read("*")
local nPass2 = cryptPassword(nPass)
local file = io.open("PF","w")
--print(nPass2)
file:write(nPass2)
file:close()
end
function matchPasswords(pass,pass2)
if pass == pass2 then
return true
else
return false
end
end
function cryptPassword(pass)
local chars = {}
local crypted1 = ""
local randomSeed = 0
for i=1, string.len(pass) do
chars[i] = string.byte(pass,i)
end
for i=1, #chars do
randomSeed = randomSeed + chars[i]
end
math.randomseed(randomSeed)
for i=1, #chars do
crypted1 = crypted1 .. math.random(255)
end

return crypted1
end

lPassword = loadPassword()
--print(lPassword)
--sleep(10)
draw()
function upDate()
while run do
update()
end
end
upDate()
term.clear()
term.setCursorPos(1,1)
KingMachine #2
Posted 28 January 2013 - 03:15 AM
I'm curious as to how you match passwords with random numbers attached at the end of them.
KillaVanilla #3
Posted 28 January 2013 - 03:18 AM
To OP: Please put your code on pastebin. It's free, and makes things a lot easier. Also, please indent your code. Otherwise, it looks okay. I haven't actually tried it (and probably never will), but it's good to see at least a miniscule amount of variety in these programs.
To above: The numbers are actually determined by the rest of the password.