all repos — dpwm @ master

A minimal and secure password manager in D.

source/app.d (view raw)

  1import std.stdio;
  2import std.file;
  3import std.utf : byChar;
  4import std.string;
  5import secured.hash;
  6import secured.symmetric;
  7import std.json;
  8import std.digest.md;
  9
 10//base functions
 11string getFileContent(string filename){
 12	assert(exists(filename));
 13	return cast(string) read(filename.byChar);
 14}
 15
 16ubyte[16] string2MD5(string input){
 17	MD5 md5;
 18	md5.start();
 19	md5.put(cast(ubyte[]) input);
 20	return md5.finish();
 21}
 22
 23JSONValue decryptDB(string pw, string filename){
 24	string content = getFileContent(filename);
 25	ubyte[] dec;
 26	dec = decrypt(string2MD5(pw), cast(ubyte[])content, dec);
 27	return parseJSON(cast(string)dec);
 28}
 29
 30
 31void encryptDB(string pw, string content, string filename){
 32		//ubyte[] data =  encrypt(string2MD5(pw), cast(ubyte[]) content, null);
 33		ubyte[] data =  encrypt_ex(string2MD5(pw), cast(ubyte[]) content, null, defaultChunkSize, SymmetricAlgorithm.ChaCha20_Poly1305, KdfAlgorithm.PBKDF2, defaultKdfIterations, defaultSCryptR, defaultSCryptP, HashAlgorithm.SHA2_512);
 34		
 35		std.file.write(filename, cast(string) data);
 36}
 37
 38void main(){
 39	const string inputFile = "data.db";
 40
 41	//welcome text
 42	writeln("Welcome to dpwm.\n");
 43
 44	string temp;
 45	JSONValue db;
 46
 47	if(exists(inputFile)){
 48		//the file exists, ask for key and test it
 49		writeln("DB detected. Please insert your decryption key:");
 50		do {
 51			temp = readln.strip;
 52		} while(temp == "");
 53		db = decryptDB(temp, inputFile);
 54		
 55		writeln(db["test"].str);
 56
 57	} else {
 58		//the file doesn't exist, ask for key and create it
 59		writeln("I didn't detect any database, so I will create a new one for you. Please write your encryption key.");
 60		do {
 61			temp = readln.strip;
 62		} while(temp == "");
 63		db = [ "test": "0" ];
 64		encryptDB(temp, db.toString, inputFile);
 65	}
 66
 67	/* MENU */
 68	int choice;
 69	do {
 70		writeln("\nEnter a number for what you wish to do:");
 71		writeln("1. Show all passwords in DB (WIP)");
 72		writeln("2. Generate a new password (WIP)");
 73		writeln("3. Add an existing password (WIP)");
 74		writeln("4. Delete a saved password (WIP)");
 75		writeln("0. Exit program");
 76
 77		readf(" %s", &choice);
 78		switch(choice){
 79			case 0: //exit
 80				writeln("Bye bye!");
 81				break;
 82			case 1:
 83				writeln(db);
 84				break;
 85			case 2:
 86
 87				break;
 88			case 3:
 89
 90				break;
 91			case 4:
 92
 93				break;
 94			default:
 95				writeln("Please insert a number from the menu!");
 96		}
 97	} while(choice != 0);
 98	/* END MENU */
 99}
100
101/* JSON EXAMPLES
102
103// create a json struct
104JSONValue jj = [ "language": "D" ];
105
106// rating doesnt exist yet, so use .object to assign
107jj.object["rating"] = JSONValue(3.5);
108
109// create an array to assign to list
110jj.object["list"] = JSONValue( ["a", "b", "c"] );
111
112// list already exists, so .object optional
113jj["list"].array ~= JSONValue("D");
114
115*/