Last active
May 1, 2016 21:08
-
-
Save auchomage/e04bbe1a1bf3da3b5e1a6d43cfaa7019 to your computer and use it in GitHub Desktop.
Model answer, not working.
This file contains 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
console.log('Starting password manager.'); | |
var crypto = require('crypto-js'); | |
var storage = require('node-persist'); // integrates node-persist module into this application | |
storage.initSync(); // prepares the computer to write and save variables | |
/* | |
var retrievedAccount = storage.getItemSync('account'); | |
console.log("Account is ", retrievedAccount); | |
*/ | |
var argv = require('yargs') | |
.command('create','Creates a user account', function(yargs){ | |
yargs.options({ | |
name: { | |
demand: true, | |
alias: 'n', | |
type: 'string', | |
description: 'name eg Facebook or Twitter' | |
}, | |
username: { | |
demand: true, | |
alias: 'u', | |
type: 'string', | |
description: 'username eg user12! or user12@gmail.com' | |
}, | |
password: { | |
demand: true, | |
alias: 'p', | |
type: 'string', | |
description: 'password for account access' | |
}, | |
masterPassword:{ | |
demand: true, | |
alias: 'm', | |
type: 'string', | |
description: 'Master password is required for encryption / decryption' | |
} | |
}).help('help'); | |
}) | |
.command('get', 'get accounts with a matching name', function(yargs){ | |
yargs.options({ | |
name: { | |
demand: true, | |
alias: 'n', | |
type: 'string', | |
description: 'Specify an account name like Twitter, Facebook etc' | |
}, | |
masterPassword:{ | |
demand: true, | |
alias: 'm', | |
type: 'string', | |
description: 'Master password is required for encryption / decryption' | |
} | |
}).help('help'); | |
}) | |
.help('help') | |
.argv; | |
var command = argv._[0]; | |
//console.log('From create function: account = ' , argv); | |
/* | |
- Create a function called 'create' | |
- The create function will require 3 arguments | |
- 1) --name (as in Twitter, Facebook etc) | |
- 2) --username (like user12 or an email address) | |
- 3) --password | |
- Create a function called 'get' | |
- requires one argument ie --name | |
Example | |
account.name = 'Facebook' | |
account.username = 'User21' | |
account.password = 'Passwrd 123!' | |
*/ | |
function getAccounts(name, masterPassword){ | |
var accounts = []; | |
// use getItemSync to fetch accounts | |
var encryptedAccount = storage.getItemSync('accounts'); | |
// DECRYPT | |
if (typeof accounts !== 'undefined'){ | |
var bytes = crypto.AES.decrypt(encryptedAccount,masterPassword); | |
var accounts = JSON.parse(bytes.toString(crypto.enc.Utf8)); | |
} | |
// return accounts array | |
return accounts; | |
} | |
function saveAccounts(accounts, masterPassword){ | |
// encrypt accounts | |
var encryptedAccounts = crypto.AES.encrypt(JSON.stringify(accounts), masterPassword); | |
// setItemSync ie saving encrypted Accounts | |
storage.setItemSync('accounts', encryptedAccounts.toString()); | |
console.log('Account saved.'); | |
// return accounts | |
return accounts; | |
} | |
function createAccount(account, masterPassword){ | |
var accounts = getAccounts(masterPassword); | |
// push on new account | |
accounts.push(account); | |
// save the account | |
saveAccounts(accounts, masterPassword); | |
// return account | |
return account; | |
} | |
function getAccount(accountName){ | |
var accounts = getAccounts(masterPassword); | |
var matchedAccount; | |
accounts.forEach(function(account){ | |
if(account.name === accountName) { | |
matchedAccount = account; | |
} | |
}); | |
return matchedAccount; | |
} | |
if(command === 'create'){ | |
//console.log("Coming from 1st if statement ie command === !!!!"); | |
var createdAccount = createAccount({ | |
name: argv.name, | |
username: argv.username, | |
password: argv.password | |
}, argv.masterPassword); | |
console.log("createdAccount datatype : " + typeof createdAccount); | |
//console.log("Account created: " ); | |
} else if( command === 'get'){ | |
var fetchedAccount = getAccounts(argv.name, argv.masterPassword); | |
if(typeof fetchedAccount === 'undefined'){ | |
console.log("Account not found."); | |
} else { | |
console.log('Account found'); | |
console.log(fetchedAccount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I run the program with the command
node app_v2.js create -n Twitter -u 'Top Cat' -p tc -m abc
I get this error message.
/home/xxx/node-course/password-manager/node_modules/crypto-js/cipher-core.js:861
var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, ciphertext.salt);
^
TypeError: Cannot read property 'salt' of undefined
at Object.CryptoJS.lib.Cipher.C_lib.PasswordBasedCipher.SerializableCipher.extend.decrypt (/home/ocj/node-course/password-manager/node_modules/crypto-js/cipher-core.js:861:101)
at Object.decrypt (/home/ocj/node-course/password-manager/node_modules/crypto-js/cipher-core.js:205:59)
at getAccounts (/home/ocj/node-course/password-manager/app_v2.js:86:26)
at createAccount (/home/ocj/node-course/password-manager/app_v2.js:106:17)
at Object. (/home/ocj/node-course/password-manager/app_v2.js:132:23)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)