Last active
December 14, 2015 12:48
-
-
Save georgeOsdDev/5089195 to your computer and use it in GitHub Desktop.
passwordManager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| var readline = require('readline'), | |
| crypto = require("crypto"), | |
| fs = require("fs"); | |
| var rl = readline.createInterface(process.stdin, process.stdout), | |
| prefix = 'PasswordMgr>', | |
| _fileName, | |
| _outStream, | |
| _encKey, | |
| _mode = "encrypt", | |
| engine ={ | |
| encrypt:function(){ | |
| var cipher = crypto.createCipher("aes192", _encKey), | |
| crypted =""; | |
| inStream = fs.createReadStream(_fileName); | |
| inStream.on('data', function(d) { | |
| crypted +=cipher.update(d,'utf8','hex'); | |
| }); | |
| inStream.on('end', function() { | |
| crypted += cipher.final('hex'); | |
| _outStream.write(crypted); | |
| console.log("\n\x1B[32m ------ Success ------ \x1B[39m"); | |
| rl.close(); | |
| }); | |
| inStream.resume(); | |
| }, | |
| decrypt:function(){ | |
| var decipher = crypto.createDecipher('aes192', _encKey), | |
| decrypted; | |
| inStream = fs.createReadStream(_fileName); | |
| inStream.on('data', function(d) { | |
| decrypted = decipher.update(d, 'hex', 'utf-8'); | |
| }); | |
| inStream.on('end', function() { | |
| decrypted += decipher.final('utf-8'); | |
| _outStream.write(decrypted); | |
| console.log("\n\x1B[32m ------ Success ------ \x1B[39m"); | |
| rl.close(); | |
| }); | |
| inStream.resume(); | |
| } | |
| }; | |
| function askMode(){ | |
| rl.question( | |
| "What do you want to do?\n"+ | |
| " 1.encrypt\n"+ | |
| " 2.decrypt\n"+ | |
| prefix, function(answer) { | |
| handleMode(answer); | |
| }); | |
| } | |
| function handleMode(mode){ | |
| switch (mode){ | |
| case "1": | |
| case "encrypt": | |
| _mode = "encrypt"; | |
| break; | |
| case "2": | |
| case "decrypt": | |
| _mode = "decrypt"; | |
| break; | |
| default: | |
| console.log("\x1B[31mError!! type 1 or 2\x1B[39m\n"); | |
| return askMode(); | |
| } | |
| askInput(); | |
| } | |
| function askInput(){ | |
| rl.question( | |
| "type input file name \n"+ | |
| prefix, function(answer) { | |
| handleInput(answer); | |
| }); | |
| } | |
| function handleInput(fileName){ | |
| fs.exists(fileName, function (exists) { | |
| if (!exists){ | |
| console.log("\x1B[31mError!! No Such file"+ fileName +"\x1B[39m\n"); | |
| askInput(); | |
| }else{ | |
| _fileName = fileName; | |
| askKey(); | |
| } | |
| }); | |
| } | |
| function askKey(){ | |
| rl.question( | |
| "Type Your Encrypt Key\n"+ | |
| prefix, function(answer) { | |
| handleKey(answer); | |
| }); | |
| } | |
| function handleKey(encKey){ | |
| console.log(encKey); | |
| console.log(encKey.length); | |
| if (encKey.length === 0 && _mode === "encrypt"){ | |
| console.log("\x1B[33mWarn!! encKey should be longer than 8 characters\x1B[39m"); | |
| _encKey = generateKey(8); | |
| console.log("\x1B[33mWarn!! yourkey is \x1B[39m" + _encKey); | |
| } else if (encKey.length < 8 && _mode === "encrypt"){ | |
| console.log("\x1B[33mWarn!! encKey should be longer than 8 characters\x1B[39m"); | |
| _encKey = encKey + generateKey(8-encKey.length); | |
| console.log("\x1B[33mWarn!! yourkey is \x1B[39m" + _encKey); | |
| } else { | |
| _encKey = encKey; | |
| } | |
| askOutputFormat(); | |
| } | |
| function generateKey(len){ | |
| var a = 'abcdefghijklmnopqrstuvwxyz'+ | |
| 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'+ | |
| '0123456789'; | |
| arr = a.split(''); | |
| var s = '_'; | |
| for (var i = 0; i < len-1; i++) { | |
| s += arr[Math.floor(Math.random() * arr.length)]; | |
| } | |
| return s; | |
| } | |
| function askOutputFormat(){ | |
| rl.question( | |
| "Select output format\n"+ | |
| " 1.file\n"+ | |
| " 2.console\n"+ | |
| prefix, function(answer) { | |
| handleOutput(answer); | |
| }); | |
| } | |
| function handleOutput(output){ | |
| switch (output){ | |
| case "1": | |
| console.log(_fileName+'_'+_mode+' will be generate'); | |
| _outStream = fs.createWriteStream(_fileName+'_'+_mode, { flags: 'w', mode: 0666 }); | |
| break; | |
| case "2": | |
| _outStream = process.stdout; | |
| break; | |
| default: | |
| console.log("\x1B[31mError!! type 1 or 2\x1B[39m\n"); | |
| return askOutputFormat(); | |
| } | |
| console.log("\n"); | |
| engine[_mode](); | |
| } | |
| console.log("Welcome to PasswordMgr\n"); | |
| rl.setPrompt(prefix, prefix.length); | |
| askMode(); | |
| rl.on('close', function() { | |
| console.log('goodbye!'); | |
| process.exit(0); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment