-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebIndex.js
291 lines (277 loc) · 7.74 KB
/
WebIndex.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
const bip39 = require("bip39");
const crypto = require("crypto");
const sssa = require("sssa-js");
const cryptico = require("cryptico");
const CryptoJS = require("crypto-js");
const SHARE_COUNT = 3;
const THRESHOLD = 2;
// Sending Functions
console.log("inside bundle js");
function mnemonicToSSS(privateKey, password, callback) {
let key = privateKey.split("x")[1];
return new Promise(function(resolve, reject) {
let encKey = CryptoJS.AES.encrypt(key, password).toString();
let shares = sssa.create(THRESHOLD, SHARE_COUNT, encKey);
resolve(shares);
if (callback) callback(shares);
});
}
function createKey(usernameOfHolder, usernameOfSaver, password) {
let combinedUsername = usernameOfHolder + password + usernameOfSaver;
let encKey = crypto
.createHash("sha256")
.update(combinedUsername)
.digest("base64");
console.log(encKey);
return encKey;
}
function encryptKeyValuePairUsingPublicKey(key, value, publicKey) {
let objectToBeEncrypted = {
isRequest: false,
identity: key,
shard: value
};
let buffer = JSON.stringify(objectToBeEncrypted);
let encrypted = cryptico.encrypt(buffer, publicKey);
return encrypted.cipher;
}
function returnArraysOfDataToBeSent(
arrayOfReceivers,
mnemonic,
password,
username
) {
console.log("array of Receo", arrayOfReceivers);
return new Promise(function(resolve, reject) {
function afterLoop(array) {
resolve(array);
}
if (arrayOfReceivers.length !== SHARE_COUNT) {
reject("Invalid Number of Receivers");
} else {
mnemonicToSSS(mnemonic, password).then(function(shares) {
let arrayToBeReturned = [];
for (let i = 0; i < SHARE_COUNT; i++) {
let receiverPublicKey = arrayOfReceivers[i].publicKey;
let receiverUsername = arrayOfReceivers[i].username;
// let receiverLink = arrayOfReceivers.link;
let key = createKey(username, receiverUsername, password);
let data = encryptKeyValuePairUsingPublicKey(
key,
shares[i],
receiverPublicKey
);
let retVal = {
data: data,
username: receiverUsername
};
arrayToBeReturned.push(retVal);
if (i === SHARE_COUNT - 1) {
afterLoop(arrayToBeReturned);
}
}
});
}
});
}
// Sending Function End
//Requesting for Keys
function encryptDataToBeSentForRequest(
key,
senderPublicKey,
receiverPublicKey,
senderUsername
) {
let objectToBeEncrypted = {
isRequest: true,
key: key,
publicKey: senderPublicKey,
username: senderUsername
};
let buffer = JSON.stringify(objectToBeEncrypted);
let encrypted = cryptico.encrypt(buffer, receiverPublicKey);
return encrypted.cipher;
}
function requestKeys(arrayOfReceivers, senderPublicKey, password, username) {
return new Promise(function(resolve, reject) {
function afterLoop(array) {
resolve(array);
}
if (arrayOfReceivers.length !== SHARE_COUNT) {
reject("Invalid Number of Receivers");
} else {
let arrayToBeReturned = [];
for (let i = 0; i < SHARE_COUNT; i++) {
// console.log(arrayOfReceivers[i]);
let receiverPublicKey = arrayOfReceivers[i].publicKey;
let receiverUsername = arrayOfReceivers[i].username;
// let receiverLink = arrayOfReceivers.link;
let key = createKey(username, receiverUsername, password);
let data = encryptDataToBeSentForRequest(
key,
senderPublicKey,
receiverPublicKey,
username
);
let retVal = {
data: data,
username: receiverUsername
};
arrayToBeReturned.push(retVal);
if (i === SHARE_COUNT - 1) {
afterLoop(arrayToBeReturned);
}
}
}
});
}
//Requesting for Keys End
//Combining Keys
function combinePieces(mnemonicShares, password) {
let shares = mnemonicShares;
let splitVal = sssa.combine(shares);
let encKey = splitVal;
return new Promise((resolve, reject) => {
let bytes = CryptoJS.AES.decrypt(encKey, password);
let plaintext = bytes.toString(CryptoJS.enc.Utf8);
resolve("0x" + plaintext);
});
}
function decryptPieceUsingPrivateKey(encrypted, privateKey) {
let result = cryptico.decrypt(encrypted, privateKey);
console.log(result);
return result.plaintext;
}
function arrayOfKeysReceived(arrayOfPieces, privateKey, password) {
return new Promise(function(resolve, reject) {
console.log("PK", privateKey);
if (arrayOfPieces.length !== THRESHOLD) {
reject("Invalid Length of Array");
} else {
function todoAfterLoop(array, users) {
console.log(users, ": users. inside todo after loop");
console.log(array, ": array. iside todo after loop");
combinePieces(array, password).then(function(mnemonic) {
console.log(users, "inside combinePieces then");
resolve({
mnemonic: mnemonic,
users: users
});
});
}
let arrayToBePassed = [];
let userArray = [];
for (let i = 0; i < THRESHOLD; i++) {
let currPiece = arrayOfPieces[i];
let shard1 = decryptPieceUsingPrivateKey(currPiece, privateKey);
window.App.shard1 = shard1;
shard = JSON.parse(shard1.toString());
console.log(shard);
user = shard["userSending"];
console.log("inside array to be passed");
console.log(user);
shard = shard["shard"];
userArray.push(user);
console.log(userArray, "User Array");
arrayToBePassed.push(shard);
if (i === THRESHOLD - 1) {
todoAfterLoop(arrayToBePassed, userArray);
}
}
}
});
}
//Combining Keys End
// function getRandomIDs(password , number_of_users , time_stamp) {
// let sha1_encryption = crypto.createHash('sha1').update(password).digest("hex");
//
// let sub_string_length = sha1_encryption.length / 5 ;
//
// let substring_array = sha1_encryption.match(new RegExp('.{1,' + sub_string_length + '}', 'g'));
//
// let number_arr = [] ;
//
// for (var i = 0; i < substring_array.length; i++) {
// let str = "" ;
//
// for(var j = 0 ; j < substring_array[i].length ; j++)
// {
// str = str + substring_array[i].charCodeAt(j) ;
// }
//
// number_arr.push(str) ;
//
// }
//
//
// let num_arr = [] ;
//
// for (var i = 0; i < number_arr.length; i++) {
// num_arr.push(Number(number_arr[i]) % number_of_users);
// }
//
//
// for (var i = 0; i < num_arr.length; i++) {
// num_arr[i] = ( num_arr[i] * Math.log(time_stamp) ) % number_of_users ;
// }
//
//
//
// var map = new Map();
//
// for (var i = 0; i < num_arr.length; i++) {
// if( map.has(num_arr[i]) == true )
// {
// let temp = num_arr[i] ;
//
// let count = 1 ;
// while(map.has(temp) == true)
// {
// temp = temp + Math.pow(count , 2) ;
// temp = temp % number_of_users ;
// count = count + 1 ;
// }
//
// num_arr[i] = temp ;
//
// map.set(num_arr[i] , " ") ;
//
//
//
//
// }
// else
// {
// map.set(num_arr[i] , " ") ;
// }
// }
//
//
// for (var i = 0; i < num_arr.length; i++) {
// num_arr[i] = Math.floor(num_arr[i]) ;
// }
//
//
//
// return num_arr ;
//
//
//
//
//
//
// }
function generateKeys() {
let RSAKey = cryptico.generateRSAKey(new Date().getTime(), 512);
let publicKey = cryptico.publicKeyString(RSAKey);
return {
privateKey: RSAKey,
publicKey: publicKey
};
}
window.App = {
Send: returnArraysOfDataToBeSent,
Request: requestKeys,
Combine: arrayOfKeysReceived,
Generate: generateKeys
};