-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
150 lines (119 loc) · 5.25 KB
/
test.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
var lib = require("./lib");
var KDF = lib.KDF,
HMAC = lib.HMAC,
SHA256 = lib.SHA256,
setup_cipher = lib.setup_cipher,
enc_gcm = lib.enc_gcm,
dec_gcm = lib.dec_gcm,
bitarray_slice = lib.bitarray_slice,
bitarray_to_string = lib.bitarray_to_string,
string_to_bitarray = lib.string_to_bitarray,
bitarray_to_hex = lib.bitarray_to_hex,
hex_to_bitarray = lib.hex_to_bitarray,
bitarray_to_base64 = lib.bitarray_to_base64,
base64_to_bitarray = lib.base64_to_bitarray,
byte_array_to_hex = lib.byte_array_to_hex,
hex_to_byte_array = lib.hex_to_byte_array,
string_to_padded_byte_array = lib.string_to_padded_byte_array,
string_to_padded_bitarray = lib.string_to_padded_bitarray,
string_from_padded_byte_array = lib.string_from_padded_byte_array,
string_from_padded_bitarray = lib.string_from_padded_bitarray,
random_bitarray = lib.random_bitarray,
bitarray_equal = lib.bitarray_equal,
bitarray_len = lib.bitarray_len,
bitarray_concat = lib.bitarray_concat,
dict_num_keys = lib.dict_num_keys;
// 1. KDF and SHA256 are different both hashing functions, but just different ones correct?
// Exploration of functions
var sha = SHA256('roneesh');
// console.log(sha);
// console.log(bitarray_len(sha))
// SHA256 always returns a NON-RANDOM 256-bit bitarray
var random = random_bitarray(128);
// console.log(random);
// console.log(bitarray_len(random));
// Returns a randdom bitarray of the % 32 lenghth you specify
var ba_str = string_to_bitarray('abcdefgh');
// console.log(ba_str);
// console.log(bitarray_len(ba_str));
// So every letter is an NON-RANDOM 8-bit bitarray, and they together
// form a bitarray of length 8 * characterCount
var kdf = KDF('testPassword', 'saltines');
// console.log(kdf);
// console.log(bitarray_len(kdf))
// KDF always returns a NONRANDOM 256-bit bitarray
var k0 = SHA256(kdf);
// console.log(k0);
// console.log(bitarray_len(k0))
// SHA256(KDF(pwd,salt)) returns a NONRANDOM 256-bit bitarray
// var fakeSalt = random_bitarray(128),
// fakePwd = 'fakefake',
// fakeKdf = KDF(fakePwd, fakeSalt)
// KdfConcat0 = bitarray_concat(fakeKdf, 0),
// KdfConcat1 = bitarray_concat(fakeKdf, 1);
// console.log(bitarray_len(SHA256(fakeKdf)));
// console.log(SHA256(fakeKdf));
// console.log(bitarray_len(SHA256(KdfConcat0)));
// console.log(SHA256(KdfConcat0));
// console.log(bitarray_len(SHA256(KdfConcat1)));
// console.log(SHA256(KdfConcat1));
// var passwordManager = {
// salt: null,
// HMACkey: null, //128-bit key from 256
// AESkey: null, //128-bit key rom 256
// setup_cipher: null,
// passwords : {
// //'hashedDomainName' : {
// // salt: 'salt',
// // encryptedPassword: 'pwd'
// //}
// // but I ended up using...
// // 'HMAChashedDomainName' : 'aesEncryptedPassword'
// },
// };
// var managerPassword = 'test123';
// var domainName = 'google.com';
// var domainPassword = 'google123';
// // 1. Get the key from the password
// passwordManager['salt'] = random_bitarray(128);
// // concat KDF with 0, and 1 and then SHA it to get 256, then slice it to get a 128-bit key
// passwordManager['AESkey'] = bitarray_slice(SHA256(bitarray_concat(KDF(managerPassword, passwordManager['salt']), 0)), 0, 128);
// passwordManager['HMACkey'] = bitarray_slice(SHA256(bitarray_concat(KDF(managerPassword, passwordManager['salt']), 1)), 0, 128);
// passwordManager['setup_cipher'] = setup_cipher(passwordManager['AESkey']);
// console.log(passwordManager);
// console.log('\n');
// // 2. Hash the Domain you want to save, for now it's value is plaintext
// var hashedDomain = HMAC(passwordManager['HMACkey'], domainName);
// passwordManager['passwords'][hashedDomain] = {}
// // console.log(passwordManager);
// // console.log('\n');
// // 3. Encrypt the password
// var encryptedDomainPassword = enc_gcm(passwordManager['setup_cipher'], string_to_bitarray(domainPassword));
// passwordManager['passwords'][hashedDomain] = encryptedDomainPassword;
// // console.log(passwordManager);
// // console.log('\n');
// // 4. Write a function to get a key/value in passwords
// function getPassword(domain) {
// hashOfDomain = HMAC(passwordManager['HMACkey'], domain);
// if (passwordManager['passwords'][hashOfDomain]) {
// var plainTextPassword = bitarray_to_string(dec_gcm(passwordManager['setup_cipher'], passwordManager['passwords'][hashOfDomain]));
// // console.log(domain + ' : ' + plainTextPassword);
// } else {
// // console.log(domain + ' : this pwd is not in your DB!');
// }
// }
// // getPassword('google.com');
// // getPassword('facebook.com');
// // 5. Abstract steps 2 and 3 into a function
// function addPassword(domain, password) {
// var hashedDomain = HMAC(passwordManager['HMACkey'], domain);
// passwordManager['passwords'][hashedDomain] = {}
// var encryptedDomainPassword = enc_gcm(passwordManager['setup_cipher'], string_to_bitarray(password));
// passwordManager['passwords'][hashedDomain] = encryptedDomainPassword;
// }
// // addPassword('linkedin.com', '123Linked!');
// // getPassword('linkedin.com');
// // 6. Write a function to save the passwordManager as JSON
// function saveAsJSON(passwordManager) {
// return JSON.stringify(passwordManager);
// }