-
Notifications
You must be signed in to change notification settings - Fork 0
/
ar.js
99 lines (62 loc) · 3.23 KB
/
ar.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
const { ethers } = require("ethers");
const eccrypto = require("eccrypto");
const path = require("path");
const fs = require("fs-extra");
const { memorySizeOf, encryptData, decryptData } = require('./utils.js');
// const wallet = ethers.Wallet.createRandom()
function generateUserKeys(userCount) {
// Generating user private, public key pairs
let userKeys = [];
for (userIndex = 0; userIndex < userCount; userIndex++) {
let privateKey = eccrypto.generatePrivate();
userKeys.push({private: privateKey,
public: eccrypto.getPublic(privateKey) })
console.log(`Generated user ${userIndex} keys`)
console.log(`Private: ${privateKey.toString('hex')}, \nPublic: ${eccrypto.getPublic(privateKey).toString('hex')}\n\n` )
}
return userKeys;
}
function readUserKeys() {
console.log("Reading user keys from ./userKeys.json\n\n")
return JSON.parse(fs.readFileSync('./userKeys.json'));
}
function readSecretShare() {
return fs.readFileSync('./encryptedData.json');
}
async function generateSecretShare(userKeys, secretCipherText) {
for (userIndex = 0; userIndex < userKeys.length; userIndex++) {
// Temporary cipher text computation using each user keys
secretCipherText = await encryptData(Buffer.from(userKeys[userIndex].public), JSON.stringify(secretCipherText))
}
return secretCipherText;
}
async function recoverSecret(userKeys, secretDecryptedText) {
for (userIndex = 0; userIndex < userKeys.length; userIndex++) {
secretDecryptedText = {iv: Buffer.from(JSON.parse(secretDecryptedText).iv),
ephemPublicKey: Buffer.from(JSON.parse(secretDecryptedText).ephemPublicKey),
ciphertext: Buffer.from(JSON.parse(secretDecryptedText).ciphertext),
mac: Buffer.from(JSON.parse(secretDecryptedText).mac)}
// Temporary cipher text computation using each user keys
secretDecryptedText = await decryptData(Buffer.from(userKeys[userKeys.length - userIndex -1].private), secretDecryptedText)
}
return secretDecryptedText;
}
async function main() {
let userKeys = generateUserKeys(5);
console.log("Writing user keys to ./userKeys.json\n\n")
fs.writeFileSync(path.resolve("./userKeys.json"), JSON.stringify(userKeys))
userKeys = readUserKeys()
// Encrypting the data using multiple user keys
const secretPainText = {type: "mnemonic", data: "indoor dish desk flag debris potato excuse depart ticket judge file exit"};
const secretCipherText = await generateSecretShare(userKeys, secretPainText);
console.log("Writing encrypted data to ./encryptedData.json\n\n")
fs.writeFileSync(path.resolve("./encryptedData.json"), JSON.stringify(secretCipherText))
// Decrypting the data using multiple user keys
console.log("Reading encrypted data from ./encryptedData.json\n\n")
const secretShare = readSecretShare()
let recoveredSecret = await recoverSecret(userKeys, secretShare);
console.log("Recovered secret text: ", recoveredSecret.toString())
console.log("Total size of the cipher text: ", memorySizeOf(secretCipherText))
console.log("Total size of the secret plain text: ", memorySizeOf(secretPainText))
}
main();