-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyPair.js
79 lines (62 loc) · 2.34 KB
/
keyPair.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
const fs = require('fs');
const crypto = require('crypto');
// private key
// we're gonna randomly generate 256 pairs of random numbers
// each 256 bits long
// so our private key is 512 random numbers
function genRandomNum(filename, numPairs) {
const pairs = [];
for (let i = 0; i < numPairs; i++) {
const random1 = Math.floor(Math.random() * 2**256);
const random2 = Math.floor(Math.random() * 2**256);
pairs.push([random1, random2]);
}
const pairsString = JSON.stringify(pairs);
fs.writeFileSync(filename, pairsString);
}
genRandomNum('random_pairs.json', 256);
// public key
// for the public key, we're gonna hash out each number
function genPublicKey(filename) {
const privateKeyData = fs.readFileSync(filename, 'utf8');
const privateKey = JSON.parse(privateKeyData);
const publicKey = [];
for (const pair of privateKey) {
const hash1 = crypto.createHash('sha256').update(pair[0].toString()).digest('hex');
const hash2 = crypto.createHash('sha256').update(pair[1].toString()).digest('hex');
publicKey.push([hash1, hash2]);
}
return publicKey;
}
const publicKey = genPublicKey('random_pairs.json');
console.log('Public Key:', publicKey);
function sign(message, privateKeyFile) {
const privateKeyData = fs.readFileSync(privateKeyFile, 'utf8');
const privateKey = JSON.parse(privateKeyData);
const messageBinary = Buffer.from(message).toString('binary');
const signature = [];
for (let i = 0; i < messageBinary.length; i++) {
const bit = messageBinary.charCodeAt(i) === 48 ? 0 : 1; // '0' => 0, '1' => 1
const pair = privateKey[i];
const selected = pair[bit];
signature.push(selected);
}
return signature;
}
function verify(message, signature, publicKey) {
const messageBinary = Buffer.from(message).toString('binary');
for (let i = 0; i < messageBinary.length; i++) {
const bit = messageBinary.charCodeAt(i) === 48 ? 0 : 1;
const pair = publicKey[i];
const signatureHash = crypto.createHash('sha256').update(signature[i].toString()).digest('hex');
if (signatureHash !== pair[bit]) {
return false;
}
}
return true;
}
const message = "Hello, Lamport! This is Sammy :)";
const signature = sign(message, 'random_pairs.json');
console.log('Signature:', signature);
const isValid = verify(message, signature, publicKey);
console.log(`Signature valid: ${isValid}`);