-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
56 lines (47 loc) · 1.57 KB
/
index.mjs
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
import elliptic from 'js-crypto-ec';
const msg = ["o", "l", "a"] // Uint8Array
async function generateKeys() {
return elliptic.generateKey('P-256').then((key) => {
const publicKey = key.publicKey;
const privateKey = key.privateKey;
return {publicKey, privateKey}
})
}
elliptic.generateKey('P-256').then((key) => {
const publicKey = key.publicKey;
const privateKey = key.privateKey;
console.log(publicKey);
console.log(privateKey);
// sign
elliptic.sign(
msg,
privateKey,
'SHA-256',
'raw' // output signature is not formatted. DER-encoded signature is available with 'der'.
).then((signature) => {
console.log("signature", signature);
// now you get the signature in Uint8Array
return elliptic.verify(
msg,
signature,
publicKey,
'SHA-256',
'raw' // input signature is not formatted. DER-encoded signature is available with 'der'.
);
}).then((valid) => {
// o resultado é true se a assinatura for válida
console.log("valid", valid);
});
})
const JWK_A = await generateKeys();
const JWK_B = await generateKeys();
// Lado A
const sharedAtPlayerA = elliptic.deriveSecret(JWK_B.publicKey, JWK_A.privateKey).then( (secretAtA) => {
// now you get the shared secret from my (player A's) private key and player B's public key
console.log({secretAtA});
})
// Lado B
const sharedAtPlayerB = elliptic.deriveSecret(JWK_A.publicKey, JWK_B.privateKey).then( (secretAtB) => {
// now you get the shared secret from my (player B's) private key and player A's public key
console.log({secretAtB});
})