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

SimpleSecure

Started by Cranium, 23 April 2013 - 07:20 AM
Cranium #1
Posted 23 April 2013 - 09:20 AM
I decided I would make my own simple security system with basic 'encryption'. I put this together in about an hour, and as such, is not the pinnacle in security.
The API is designed to be super simple to use, and run with your own GUI's, so it does not draw anything to the screen.
There are just a few functions, with some very basic properties. Explanations are in the code.
http://pastebin.com/aMWquBZ1

--  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
H4X0RZ #2
Posted 23 April 2013 - 09:52 AM
I like it :)/>
1vannn #3
Posted 16 May 2013 - 04:45 PM
Quick question; why use 'return true' instead of aVariable = true?
Cranium #4
Posted 16 May 2013 - 05:24 PM
Quick question; why use 'return true' instead of aVariable = true?
When you're inside a function, yopu can use the statement 'return' to either exit the operation, or return a value. So for instance, in the login() function, you would simply call it like this:
if login("Username", "Password") == true then
That would return a success if the login was successful, and a failure, if not.
1vannn #5
Posted 16 May 2013 - 05:45 PM
Ahh, ok. I see, thank you. :)/>
flaminsnowman99 #6
Posted 15 June 2013 - 07:29 PM
How did you make a file for the users? I'm a little confused….