Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- console.log('Starting password manager.');
- var storage = require('node-persist'); // integrates node-persist module into this application
- storage.initSync(); // prepares the computer to write and save variables
- // Encryption/Decryption part
- var crypto = require('crypto-js');
- var secretKey = "abc";
- var masterPassword = "123";
- /*
- 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',
- description: 'Specify an account name like Twitter, Facebook etc',
- type: 'string'
- },
- 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);
- function getAccounts(masterPassword){
- }
- function saveAccounts(accounts, masterPassword){
- if (masterPassword === argv.masterPassword){
- // convert object array into a string array
- var accountsStr = JSON.stringify(accounts);
- // check
- console.log("stringified form of accounts array: " + accountsStr);
- // encrypt the stringified accounts array
- var encryptedStr = crypto.AES.encrypt(accountsStr,secretKey);
- // check
- console.log('Encrypted string: ' + encryptedStr);
- // save the encrypted string
- storage.setItemSync('encryptedString',encryptedStr);
- // check
- var testofEncryptedString = storage.getItemSync('encryptedString');
- console.log('Retrieved encrypted string: ' + testofEncryptedString);
- } else {
- return 'Wrong password, account has not been saved.';
- }
- }
- function createAccount(account, masterPassword){
- if(masterPassword === argv.masterPassword){
- // create the account
- console.log("from createAccount.!!");
- console.log("Created account: ", account);
- // check that accounts array exists, if not create it
- var accounts = storage.getItemSync('accounts');
- if(typeof accounts === 'undefined'){
- accounts = [];
- } else {
- accounts;
- }
- // push account onto array accounts
- accounts.push(account);
- // check contents of accounts
- console.log('accounts array contains: ', accounts);
- // save the accounts array
- saveAccounts(accounts, argv.masterPassword);
- return accounts;
- } else {
- return 'Incorrect password provided. Account creation cancelled.';
- }
- }
- if(command === 'create'){
- var createdAccount = createAccount({
- name: argv.name,
- username: argv.username,
- password: argv.password,
- }, argv.masterPassword);
- console.log("Account created.");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement