Advertisement
Birog

password_lokal

Mar 4th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.61 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw
  2. term.clear()
  3. term.setCursorPos(1,1)
  4. local lPassword = ""
  5. local run = true
  6. local passwordT = ""
  7. local textBoxH = 8
  8. local textbox = {}
  9. local message = "Password protection"
  10. textbox[1] = "/-----------------------\\"
  11. textbox[2] = "|                                    |"
  12. textbox[3] = "\\-----------------------/"
  13. --local inputbox = {}
  14. --local inputbox[1] = "--------------"
  15. --local inputboc[2] = "|"
  16. local w,h = term.getSize()
  17.  
  18. function draw()
  19.  term.clear()
  20.  local posX = (w/2)-(string.len(textbox[1])/2)
  21.  local posY = (h/2)-(textBoxH/2)
  22.  local posX2 = (w/2) - (string.len(textbox[1])/4)-1
  23.  local posY2 = (h/2) + (textBoxH*0.2)
  24.  for i=1, string.len(passwordT) do
  25.   if i < string.len(textbox[1])/2 then
  26.    term.setCursorPos(posX2+i,posY2)
  27.    term.write("*")
  28.   end
  29.  end
  30.  for i=1, string.len(textbox[1])/2 do
  31.  --term.setCursorPos(posX2+i, posY2)
  32.  --term.write("*")
  33.  term.setCursorPos(posX2+i,posY2-1)
  34.  term.write("-")
  35.  term.setCursorPos(posX2+i,posY2+1)
  36.  term.write("-")
  37. end
  38.  
  39. term.setCursorPos(posX2,posY2)
  40. term.write("|")
  41. term.setCursorPos(1+posX2+string.len(textbox[1])/2,posY2)
  42. term.write("|")
  43. term.setCursorPos((w/2)-(string.len(message)/2),posY2-3)
  44. term.write(message)
  45. term.setCursorPos(posX,posY)
  46. term.write(textbox[1])
  47. for i=1,textBoxH do
  48.  term.setCursorPos(posX,posY+i)
  49.  term.write("|")
  50.  term.setCursorPos(posX+string.len(textbox[1])-1,posY+i)
  51.  term.write("|")
  52. end
  53. term.setCursorPos(posX,posY+textBoxH+1)
  54. term.write(textbox[3])
  55. --Draw the input box
  56.  
  57. end
  58. function update()
  59.  event,p1,p2,p3 = os.pullEvent()
  60.  --draw()
  61.  if event == "key" then
  62.   if  p1 == 14 then
  63.    if string.len(passwordT) > 0 then
  64.     passwordT = string.sub(passwordT,0,string.len(passwordT)-1)
  65.    end
  66.   end
  67.   if p1 == 28 then
  68.    if matchPasswords(cryptPassword(passwordT),lPassword) then
  69.     message = "Access Granted!"
  70.     draw()
  71.     sleep(2)
  72.     term.clear()
  73.     term.setCursorPos(1,1)
  74.     print("Zugang gewรคhrt")
  75.     restone.setOutput("left",true)
  76.     sleep(3)
  77. else
  78. message = "ACCESS DENIED!"
  79. passwordT = ""
  80. draw()
  81. sleep(3)
  82. message = "Password protection"
  83. end
  84.  
  85. end
  86. end
  87. if event == "char" then
  88. passwordT = passwordT .. p1
  89. end
  90. if event == "terminate" then
  91. message = "ACCESS DENIED!"
  92. draw()
  93. sleep(5)
  94. passwordT = ""
  95. message = "Password protection"
  96. --draw()
  97. end
  98. if event == "disk" then
  99. message = "DISK NOT ALLOWED!"
  100. disk.eject("right")
  101. disk.eject("left")
  102. disk.eject("bottom")
  103. disk.eject("front")
  104. --disk.eject("back")
  105. disk.eject("top")
  106. sleep(5)
  107. message = "Password protection"
  108. end
  109. draw()
  110. end
  111. function loadPassword()
  112. local file = io.open("PF","r")
  113. if not(file == nil) then
  114. local password = file:read()
  115. file:close()
  116. return password
  117. else
  118. changePassword()
  119. os.reboot()
  120. --lPassword = loadPassword()
  121. end
  122. end
  123. function changePassword()
  124. term.clear()
  125. term.setCursorPos(1,1)
  126. nPass = "1"
  127. nPass3 = ""
  128. print("Type in new password:")
  129. local nPass = read("*")
  130. local nPass2 = cryptPassword(nPass)
  131. local file = io.open("PF","w")
  132. --print(nPass2)
  133. file:write(nPass2)
  134. file:close()
  135. end
  136. function matchPasswords(pass,pass2)
  137. if pass == pass2 then
  138. return true
  139. else
  140. return false
  141. end
  142. end
  143. function cryptPassword(pass)
  144. local chars = {}
  145. local crypted1 = ""
  146. local randomSeed = 0
  147. for i=1, string.len(pass) do
  148. chars[i] = string.byte(pass,i)
  149. end
  150. for i=1, #chars do
  151. randomSeed = randomSeed + chars[i]
  152. end
  153. math.randomseed(randomSeed)
  154. for i=1, #chars do
  155. crypted1 = crypted1 .. math.random(255)
  156. end
  157.  
  158. return crypted1
  159. end
  160.  
  161. lPassword = loadPassword()
  162. --print(lPassword)
  163. --sleep(10)
  164. draw()
  165. function upDate()
  166. while run do
  167. update()
  168. end
  169. end
  170. upDate()
  171. term.clear()
  172. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement