· 9 years ago · Apr 24, 2016, 02:03 AM
1console.log('Starting password manager.');
2
3var storage = require('node-persist'); // integrates node-persist module into this application
4storage.initSync(); // prepares the computer to write and save variables
5
6// Encryption/Decryption part
7var crypto = require('crypto-js');
8var secretKey = "abc";
9var masterPassword = "123";
10
11/*
12var retrievedAccount = storage.getItemSync('account');
13console.log("Account is ", retrievedAccount);
14*/
15
16var argv = require('yargs')
17 .command('create','Creates a user account', function(yargs){
18 yargs.options({
19 name: {
20 demand: true,
21 alias: 'n',
22 type: 'string',
23 description: 'name eg Facebook or Twitter'
24 },
25 username: {
26 demand: true,
27 alias: 'u',
28 type: 'string',
29 description: 'username eg user12! or user12@gmail.com'
30 },
31 password: {
32 demand: true,
33 alias: 'p',
34 type: 'string',
35 description: 'password for account access'
36 },
37 masterPassword:{
38 demand: true,
39 alias: 'm',
40 type: 'string',
41 description: 'Master password is required for encryption / decryption'
42 }
43 }).help('help');
44 })
45 .command('get', 'get accounts with a matching name', function(yargs){
46 yargs.options({
47 name: {
48 demand: true,
49 alias: 'n',
50 description: 'Specify an account name like Twitter, Facebook etc',
51 type: 'string'
52 },
53 masterPassword:{
54 demand: true,
55 alias: 'm',
56 type: 'string',
57 description: 'Master password is required for encryption / decryption'
58 }
59 }).help('help');
60 })
61 .help('help')
62 .argv;
63
64var command = argv._[0];
65console.log('From create function: account = ' , argv);
66
67function getAccounts(masterPassword){
68
69}
70
71function saveAccounts(accounts, masterPassword){
72 if (masterPassword === argv.masterPassword){
73 // convert object array into a string array
74 var accountsStr = JSON.stringify(accounts);
75 // check
76 console.log("stringified form of accounts array: " + accountsStr);
77
78 // encrypt the stringified accounts array
79 var encryptedStr = crypto.AES.encrypt(accountsStr,secretKey);
80 // check
81 console.log('Encrypted string: ' + encryptedStr);
82
83 // save the encrypted string
84 storage.setItemSync('encryptedString',encryptedStr);
85 // check
86 var testofEncryptedString = storage.getItemSync('encryptedString');
87 console.log('Retrieved encrypted string: ' + testofEncryptedString);
88 } else {
89 return 'Wrong password, account has not been saved.';
90 }
91
92}
93
94function createAccount(account, masterPassword){
95 if(masterPassword === argv.masterPassword){
96 // create the account
97 console.log("from createAccount.!!");
98 console.log("Created account: ", account);
99
100 // check that accounts array exists, if not create it
101 var accounts = storage.getItemSync('accounts');
102
103 if(typeof accounts === 'undefined'){
104 accounts = [];
105 } else {
106 accounts;
107 }
108
109 // push account onto array accounts
110 accounts.push(account);
111 // check contents of accounts
112 console.log('accounts array contains: ', accounts);
113
114 // save the accounts array
115 saveAccounts(accounts, argv.masterPassword);
116 return accounts;
117 } else {
118 return 'Incorrect password provided. Account creation cancelled.';
119 }
120}
121
122if(command === 'create'){
123 var createdAccount = createAccount({
124 name: argv.name,
125 username: argv.username,
126 password: argv.password,
127 }, argv.masterPassword);
128
129 console.log("Account created.");