-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiffie-hellman.spec.js
74 lines (59 loc) · 2.09 KB
/
diffie-hellman.spec.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
/* eslint-disable no-new */
import { DiffieHellman } from './diffie-hellman';
describe('diffie-hellman', () => {
const p = 23;
const g = 5;
const diffieHellman = new DiffieHellman(p, g);
const alicePrivateKey = 6;
const alicePublicKey = 8;
const bobPrivateKey = 15;
const bobPublicKey = 19;
test('throws an error if the constructor arguments are out of range', () => {
expect(() => {
new DiffieHellman(0, 9999);
}).toThrow();
});
test('throws an error if the constructor arguments are not prime', () => {
expect(() => {
new DiffieHellman(10, 13);
}).toThrow();
});
test('throws an error if private key is negative', () => {
expect(() => {
diffieHellman.getPublicKeyFromPrivateKey(-1);
}).toThrow();
});
test('throws an error if private key is zero', () => {
expect(() => {
diffieHellman.getPublicKeyFromPrivateKey(0);
}).toThrow();
});
test('throws an error if private key is one', () => {
expect(() => {
diffieHellman.getPublicKeyFromPrivateKey(1);
}).toThrow();
});
test('throws an error if private key equals the modulus parameter p', () => {
expect(() => {
diffieHellman.getPublicKeyFromPrivateKey(p);
}).toThrow();
});
test('throws an error if private key is greater than the modulus parameter p', () => {
expect(() => {
diffieHellman.getPublicKeyFromPrivateKey(p + 1);
}).toThrow();
});
test('when given a private key, returns the correct public one', () => {
expect(diffieHellman.getPublicKeyFromPrivateKey(alicePrivateKey)).toEqual(alicePublicKey);
});
test('when given a different private key, returns the correct public one', () => {
expect(diffieHellman.getPublicKeyFromPrivateKey(bobPrivateKey)).toEqual(bobPublicKey);
});
test('can generate a shared secret from our private key and their public key', () => {
const sharedSecret = 2;
expect(diffieHellman.getSharedSecret(alicePrivateKey, bobPublicKey))
.toEqual(sharedSecret);
expect(diffieHellman.getSharedSecret(bobPrivateKey, alicePublicKey))
.toEqual(sharedSecret);
});
});