CoderJohn

CC:UserDatabase

Sep 24th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.22 KB | None | 0 0
  1. local DB = "disk/.database";
  2. local USERS = "disk/.database/users";
  3. local base64Map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  4. local user;
  5.  
  6. --INITIALIZE
  7. if(not fs.isDir(DB))then
  8.   fs.makeDir(DB);
  9.   print("Database Created: "..tostring(fs.isDir(DB)));
  10.   fs.makeDir(USERS);
  11.   print("User Database Created: "..tostring(fs.isDir(USERS)));
  12. else
  13.   print("Database found... continuing...");
  14. end
  15.  
  16. --USER FUNCTIONS
  17.  
  18. function createUser(name, password)
  19.   local exists = userExists(name)
  20.   if(not exists)then
  21.     local fileData;
  22.     fileData = fs.open(USERS.."/"..name..".dat","w");
  23.     fileData.writeLine("[PASSWORD]");
  24.     fileData.writeLine(toBase64(password));
  25.     fileData.close();
  26.     return true; --USER CREATED
  27.   end
  28.   return false; --USER EXISTS ALREADY
  29. end
  30.  
  31. function loginAs(name, password)
  32.   local exists = userExists(name)
  33.   if(exists)then
  34.     fileData = fs.open(USERS.."/"..name..".dat","r");
  35.     local pass;
  36.     local data = {USER = name, DATABASES = {}};
  37.     repeat
  38.       local dataObj = fileData.readLine();
  39.       if(data=="[PASSWORD]")then
  40.         pass = fileData.readLine();
  41.       elseif(data=="[DATABASES]")then
  42.         repeat
  43.           db = fileData.readLine();
  44.           if(db~=nil and db~="&0")then
  45.             table.insert(dataObj.DATABASES, DB.."/"..db);
  46.           end
  47.         until( db == "&0" );
  48.       end
  49.     until( data == nil );
  50.     fileData.close();
  51.     if( pass == toBase64(password) )then
  52.       user = dataObj;
  53.       return true;
  54.     end
  55.     return false;
  56.   end
  57.   return false;
  58. end
  59.  
  60. function userExists(name)
  61.   if(fs.exists(USERS.."/"..name..".dat"))then
  62.     return true; --USER DOES EXIST
  63.   end
  64.   return false; --USER DOESN'T EXIST
  65. end
  66.  
  67. --DATA MANIPULATION
  68. function toBase64(str)
  69.   local bits = {};
  70.   local char;
  71.   for i = 1,#str do
  72.     char = str:sub(i,i);
  73.     table.insert(bits,char:byte());
  74.   end
  75.   local bitsTotal = #str*8;
  76.   local bitsNeeded = 6;
  77.   local bitsLeft = 8;
  78.   local leftover = false;
  79.   local output = "";
  80.   while(#bits > 0)do
  81.     local value;
  82.     if(bitsLeft>=bitsNeeded)then --all in ove val
  83.       local byte = bits[1];
  84.       value = bit.blogic_rshift(byte,bitsLeft-bitsNeeded);
  85.       byte = byte-bit.blshift(value,bitsLeft-bitsNeeded);
  86.       if(bitsLeft==bitsNeeded)then
  87.         table.remove(bits,1);
  88.         bitsNeeded = 6;
  89.         bitsLeft = 8;
  90.       else
  91.         bits[1] = byte;
  92.         bitsLeft = bitsLeft-bitsNeeded;
  93.         bitsNeeded = 6;
  94.       end
  95.     else --we have few bits left
  96.       local byte = bits[1];
  97.       value = bit.blshift(byte,6-bitsLeft);
  98.       value = bit.band(value,127);
  99.       table.remove(bits,1);
  100.       byte = bits[1];
  101.       bitsNeeded = 6-bitsLeft;
  102.       bitsLeft = 8-bitsNeeded;
  103.       if(byte~=nil)then
  104.         local sv = bit.blogic_rshift(byte, 8-bitsNeeded);
  105.         value = value + sv;
  106.         bits[1] = bits[1]-bit.blshift(sv,8-bitsNeeded);
  107.         bitsNeeded = 6;
  108.       else
  109.         leftover = true;
  110.       end
  111.     end
  112.     output = output..base64Map:sub(value+1,value+1);
  113.   end
  114.   if(leftover)then
  115.     print(bitsNeeded.."/"..bitsLeft);
  116.     if(bitsNeeded<bitsLeft)then
  117.       output = output.."=";
  118.     else
  119.       output = output.."==";
  120.     end
  121.   end
  122.   return output;
  123. end
Advertisement
Add Comment
Please, Sign In to add comment