Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Simple Security API
- -- By Cranium
- --[[ How to use the API
- This API is designed to have ease of use in mind, not ultimate security.
- As such, I would rate this on a 3/10 in terms of security, since the'encryption' used
- is super easy to break. The average person would not try to look for the user/pass files,
- but our more seasoned coders would find them in a heartbeat, and with some work, could
- see your password in plain text. Do not use this API as an absolute means of security.
- Functions:
- checkFile()
- ensures that the .user and .pass files are intact. If they are deleted,
- then the login fails.
- saveUser(user)
- Saves the user entered into a file, 'encrypting' it
- savePass(pass)
- Saves the password entered into a file, 'encrypting' it
- loadUser()
- Loads the 'encrypted' user from the file, and returns the unserialized table.
- loadPass()
- loads the 'encrypted' password from the file, and returns the unserialized table.
- register(user, pass)
- Saves the user and pass at the same time. Not super necessary, but can be useful
- when saving space on your code.
- login(user, pass)
- Retrieves the user and password from the files, 'decrypts' it into a table, and
- checks it character by character if it is accurate.
- Returns true if successful, false if not.
- ]]
- function checkFile()
- if not fs.exists(".user") then
- local userFile = fs.open(".user", "w")
- userFile.write("temp_user")
- userFile.close()
- end
- if not fs.exists(".pass") then
- local passFile = fs.open(".pass", "w")
- passFile.write("temp_pass")
- passFile.close()
- end
- end
- function saveUser(user)
- checkFile()
- local userTab = {}
- for char in string.gmatch(user, ".") do
- table.insert(userTab, char)
- end
- for i = 1, #userTab do
- userTab[i] = (string.byte(userTab[i]) * string.byte(userTab[i])) - 42
- end
- local userFile = fs.open(".user", "w")
- userFile.write(textutils.serialize(userTab))
- userFile.close()
- end
- function savePass(pass)
- checkFile()
- local passTab = {}
- for char in string.gmatch(pass, ".") do
- table.insert(passTab, char)
- end
- for i = 1, #passTab do
- passTab[i] = (string.byte(passTab[i]) * string.byte(passTab[i])) - 42
- end
- local passFile = fs.open(".pass", "w")
- passFile.write(textutils.serialize(passTab))
- passFile.close()
- end
- function loadUser()
- checkFile()
- local userFile = fs.open(".user", "r")
- local userTab = textutils.unserialize(userFile.readAll())
- userFile.close()
- return userTab
- end
- function loadPass()
- checkFile()
- local passFile = fs.open(".pass", "r")
- local passTab = textutils.unserialize(passFile.readAll())
- passFile.close()
- return passTab
- end
- function register(user, pass)
- saveUser(user)
- savePass(pass)
- end
- function login(user, pass)
- local savedUser = loadUser()
- local savedPass = loadPass()
- if #user == #savedUser and #pass == #savedPass then
- if savedUser ~= "temp_user" and savedPass ~= "temp_pass" then
- -- convert existing user to a decrypted string
- local userTab = {}
- for char in string.gmatch(user, ".") do
- table.insert(userTab, char)
- end
- for i = 1, #savedUser do
- userTab[i] = string.char((savedUser[i] + 42) / string.byte(userTab[i]))
- end
- local userCheck = table.concat(userTab)
- -- convert existing pass to a decrypted string
- local passTab = {}
- for char in string.gmatch(pass, ".") do
- table.insert(passTab, char)
- end
- for i = 1, #savedPass do
- passTab[i] = string.char((savedPass[i] + 42) / string.byte(passTab[i]))
- end
- local passCheck = table.concat(passTab)
- -- check against entered user/pass
- if user == userCheck and pass == passCheck then
- return true
- end
- end
- end
- return false
- end
Advertisement
Add Comment
Please, Sign In to add comment