-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathcrypto_scalarmult.js
38 lines (28 loc) · 1.12 KB
/
crypto_scalarmult.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
const test = require('brittle')
const sodium = require('..')
test('crypto_scalarmult_base', function (t) {
const keys = keyPair()
t.not(keys.secretKey, Buffer.alloc(keys.secretKey.length), 'secret key not blank')
t.not(keys.publicKey, Buffer.alloc(keys.publicKey.length), 'public key not blank')
})
test('crypto_scalarmult', function (t) {
const peer1 = keyPair()
const peer2 = keyPair()
t.not(peer1.secretKey, peer2.secretKey, 'diff secret keys')
t.not(peer1.publicKey, peer2.publicKey, 'diff public keys')
const shared1 = Buffer.alloc(sodium.crypto_scalarmult_BYTES)
const shared2 = Buffer.alloc(sodium.crypto_scalarmult_BYTES)
sodium.crypto_scalarmult(shared1, peer1.secretKey, peer2.publicKey)
sodium.crypto_scalarmult(shared2, peer2.secretKey, peer1.publicKey)
t.alike(shared1, shared2, 'same shared secret')
})
function keyPair () {
const secretKey = Buffer.alloc(sodium.crypto_scalarmult_SCALARBYTES)
sodium.randombytes_buf(secretKey)
const publicKey = Buffer.alloc(sodium.crypto_scalarmult_BYTES)
sodium.crypto_scalarmult_base(publicKey, secretKey)
return {
publicKey,
secretKey
}
}