Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DB = "disk/.database";
- local USERS = "disk/.database/users";
- local base64Map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- local user;
- --INITIALIZE
- if(not fs.isDir(DB))then
- fs.makeDir(DB);
- print("Database Created: "..tostring(fs.isDir(DB)));
- fs.makeDir(USERS);
- print("User Database Created: "..tostring(fs.isDir(USERS)));
- else
- print("Database found... continuing...");
- end
- --USER FUNCTIONS
- function createUser(name, password)
- local exists = userExists(name)
- if(not exists)then
- local fileData;
- fileData = fs.open(USERS.."/"..name..".dat","w");
- fileData.writeLine("[PASSWORD]");
- fileData.writeLine(toBase64(password));
- fileData.close();
- return true; --USER CREATED
- end
- return false; --USER EXISTS ALREADY
- end
- function loginAs(name, password)
- local exists = userExists(name)
- if(exists)then
- fileData = fs.open(USERS.."/"..name..".dat","r");
- local pass;
- local data = {USER = name, DATABASES = {}};
- repeat
- local dataObj = fileData.readLine();
- if(data=="[PASSWORD]")then
- pass = fileData.readLine();
- elseif(data=="[DATABASES]")then
- repeat
- db = fileData.readLine();
- if(db~=nil and db~="&0")then
- table.insert(dataObj.DATABASES, DB.."/"..db);
- end
- until( db == "&0" );
- end
- until( data == nil );
- fileData.close();
- if( pass == toBase64(password) )then
- user = dataObj;
- return true;
- end
- return false;
- end
- return false;
- end
- function userExists(name)
- if(fs.exists(USERS.."/"..name..".dat"))then
- return true; --USER DOES EXIST
- end
- return false; --USER DOESN'T EXIST
- end
- --DATA MANIPULATION
- function toBase64(str)
- local bits = {};
- local char;
- for i = 1,#str do
- char = str:sub(i,i);
- table.insert(bits,char:byte());
- end
- local bitsTotal = #str*8;
- local bitsNeeded = 6;
- local bitsLeft = 8;
- local leftover = false;
- local output = "";
- while(#bits > 0)do
- local value;
- if(bitsLeft>=bitsNeeded)then --all in ove val
- local byte = bits[1];
- value = bit.blogic_rshift(byte,bitsLeft-bitsNeeded);
- byte = byte-bit.blshift(value,bitsLeft-bitsNeeded);
- if(bitsLeft==bitsNeeded)then
- table.remove(bits,1);
- bitsNeeded = 6;
- bitsLeft = 8;
- else
- bits[1] = byte;
- bitsLeft = bitsLeft-bitsNeeded;
- bitsNeeded = 6;
- end
- else --we have few bits left
- local byte = bits[1];
- value = bit.blshift(byte,6-bitsLeft);
- value = bit.band(value,127);
- table.remove(bits,1);
- byte = bits[1];
- bitsNeeded = 6-bitsLeft;
- bitsLeft = 8-bitsNeeded;
- if(byte~=nil)then
- local sv = bit.blogic_rshift(byte, 8-bitsNeeded);
- value = value + sv;
- bits[1] = bits[1]-bit.blshift(sv,8-bitsNeeded);
- bitsNeeded = 6;
- else
- leftover = true;
- end
- end
- output = output..base64Map:sub(value+1,value+1);
- end
- if(leftover)then
- print(bitsNeeded.."/"..bitsLeft);
- if(bitsNeeded<bitsLeft)then
- output = output.."=";
- else
- output = output.."==";
- end
- end
- return output;
- end
Advertisement
Add Comment
Please, Sign In to add comment