all repos — dpwm @ 530daad54fced0d6ca2d6f79c60571d76d7d0885

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