-
Notifications
You must be signed in to change notification settings - Fork 0
/
app3_encrypt_password_error_handle.js
166 lines (144 loc) · 3.18 KB
/
app3_encrypt_password_error_handle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var crypto = require('crypto-js');
console.log('starting password manager');
var storage = require('node-persist');
storage.initSync();
//account.name Facebook
//account.username User12!
//account.password Password123!
function createAccount(account, masterpassword){
// var accounts = storage.getItemSync('accounts');
var accounts = getAccounts(masterpassword);
accounts.push(account);
saveAccounts(accounts, masterpassword);
return account;
}
//account.name Facebook
function getAccount(accountname, masterpassword){
//var accounts = storage.getItemSync('accounts');
var accounts = getAccounts(masterpassword);
var matchedaccount;
//for(i =0 ;i <accounts.length; i++)
//{
// if(accounts[i].name === accountname)
// return accounts[i];
//}
accounts.forEach(function(account){
if(account.name === accountname){
matchedaccount = account;
}
});
return matchedaccount;
}
function getAccounts(masterpassword){
// get item sync
// decrypt
// return array
var encryptedaccounts = storage.getItemSync('accounts');
var accounts = [];
if(typeof encryptedaccounts !== 'undefined')
{
var bytes = crypto.AES.decrypt(encryptedaccounts, masterpassword);
accounts = JSON.parse(bytes.toString(crypto.enc.Utf8));
}
return accounts;
}
function saveAccounts(accounts, masterpassword){
// encrypt accounts
// set item sync
// return accounts
var encryptedaccounts = crypto.AES.encrypt(JSON.stringify(accounts), masterpassword);
storage.setItemSync('accounts', encryptedaccounts.toString());
return accounts;
}
//createAccount({
// name: 'Facebook',
// username: '[email protected]',
// password: 'user@1234'
//});
//console.log(getAccount('Facebook'));
var argv = require('yargs')
.command('create','Create an account', function(yargs){
yargs.options({
name: {
demand: true,
alias: 'n',
description: 'Enter your name',
type: 'string'
},
username: {
demand: true,
alias: 'u',
description: 'Enter user name',
type:'string'
},
password: {
demand: true,
alias: 'p',
description: 'Enter a password',
type: 'string'
},
masterpassword: {
demand: true,
alias: 'm',
description: 'Enter a master password',
type: 'string'
},
})
.help('help');
})
.command('get','Get an account using user name', function(yargs){
yargs.options({
name: {
demand: true,
alias: 'n',
description: 'Enter your name',
type: 'string'
},
masterpassword: {
demand: true,
alias: 'm',
description: 'Enter a master password',
type: 'string'
},
})
.help('help');
})
.help('help')
.argv;
var command = argv._[0];
if(command == 'create')
{
try
{
var newAccount = createAccount({
name: argv.name,
username: argv.username,
password: argv.password
}, argv.masterpassword);
console.log('Account Created');
console.log(newAccount);
}
catch(e)
{
console.log('Unable to create account');
}
}
else if(command == 'get')
{
try
{
var account = getAccount(argv.name, argv.masterpassword);
if(typeof account === 'undefined')
console.log('Enter a valid username');
else
console.log(account);
}
catch(e)
{
console.log('Unable to get account');
}
}
else
{
console.log('Enter a valid command');
}