craniumkid22

Security

Apr 15th, 2013
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.68 KB | None | 0 0
  1. --  Simple Security API
  2. --  By Cranium
  3. --[[ How to use the API
  4.     This API is designed to have ease of use in mind, not ultimate security.
  5.     As such, I would rate this on a 3/10 in terms of security, since the'encryption' used
  6.     is super easy to break. The average person would not try to look for the user/pass files,
  7.     but our more seasoned coders would find them in a heartbeat, and with some work, could
  8.     see your password in plain text. Do not use this API as an absolute means of security.
  9.  
  10.     Functions:
  11.         checkFile()
  12.             ensures that the .user and .pass files are intact. If they are deleted,
  13.             then the login fails.
  14.         saveUser(user)
  15.             Saves the user entered into a file, 'encrypting' it
  16.         savePass(pass)
  17.             Saves the password entered into a file, 'encrypting' it
  18.         loadUser()
  19.             Loads the 'encrypted' user from the file, and returns the unserialized table.
  20.         loadPass()
  21.             loads the 'encrypted' password from the file, and returns the unserialized table.
  22.         register(user, pass)
  23.             Saves the user and pass at the same time. Not super necessary, but can be useful
  24.             when saving space on your code.
  25.         login(user, pass)
  26.             Retrieves the user and password from the files, 'decrypts' it into a table, and
  27.             checks it character by character if it is accurate.
  28.             Returns true if successful, false if not.
  29.  
  30. ]]
  31.  
  32. function checkFile()
  33.     if not fs.exists(".user") then
  34.         local userFile = fs.open(".user", "w")
  35.         userFile.write("temp_user")
  36.         userFile.close()
  37.     end
  38.     if not fs.exists(".pass") then
  39.         local passFile = fs.open(".pass", "w")
  40.         passFile.write("temp_pass")
  41.         passFile.close()
  42.     end
  43. end
  44.  
  45. function saveUser(user)
  46.     checkFile()
  47.     local userTab = {}
  48.     for char in string.gmatch(user, ".") do
  49.         table.insert(userTab, char)
  50.     end
  51.     for i = 1, #userTab do
  52.         userTab[i] = (string.byte(userTab[i]) * string.byte(userTab[i])) - 42
  53.     end
  54.     local userFile = fs.open(".user", "w")
  55.     userFile.write(textutils.serialize(userTab))
  56.     userFile.close()
  57. end
  58.  
  59. function savePass(pass)
  60.     checkFile()
  61.     local passTab = {}
  62.     for char in string.gmatch(pass, ".") do
  63.         table.insert(passTab, char)
  64.     end
  65.     for i = 1, #passTab do
  66.         passTab[i] = (string.byte(passTab[i]) * string.byte(passTab[i])) - 42
  67.     end
  68.     local passFile = fs.open(".pass", "w")
  69.     passFile.write(textutils.serialize(passTab))
  70.     passFile.close()
  71. end
  72.  
  73. function loadUser()
  74.     checkFile()
  75.     local userFile = fs.open(".user", "r")
  76.     local userTab = textutils.unserialize(userFile.readAll())
  77.     userFile.close()
  78.     return userTab
  79. end
  80.  
  81. function loadPass()
  82.     checkFile()
  83.     local passFile = fs.open(".pass", "r")
  84.     local passTab = textutils.unserialize(passFile.readAll())
  85.     passFile.close()
  86.     return passTab
  87. end
  88.  
  89. function register(user, pass)
  90.     saveUser(user)
  91.     savePass(pass)
  92. end
  93.  
  94. function login(user, pass)
  95.     local savedUser = loadUser()
  96.     local savedPass = loadPass()
  97.     if #user == #savedUser and #pass == #savedPass then
  98.         if savedUser ~= "temp_user" and savedPass ~= "temp_pass" then
  99.             -- convert existing user to a decrypted string
  100.             local userTab = {}
  101.             for char in string.gmatch(user, ".") do
  102.                 table.insert(userTab, char)
  103.             end
  104.             for i = 1, #savedUser do
  105.                 userTab[i] = string.char((savedUser[i] + 42) / string.byte(userTab[i]))
  106.             end
  107.             local userCheck = table.concat(userTab)
  108.             -- convert existing pass to a decrypted string
  109.             local passTab = {}
  110.             for char in string.gmatch(pass, ".") do
  111.                 table.insert(passTab, char)
  112.             end
  113.             for i = 1, #savedPass do
  114.                 passTab[i] = string.char((savedPass[i] + 42) / string.byte(passTab[i]))
  115.             end
  116.             local passCheck = table.concat(passTab)
  117.             -- check against entered user/pass
  118.             if user == userCheck and pass == passCheck then
  119.                 return true
  120.             end
  121.         end
  122.     end
  123.     return false
  124. end
Advertisement
Add Comment
Please, Sign In to add comment