-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.test.ts
132 lines (116 loc) · 3.92 KB
/
browser.test.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* @jest-environment jsdom
*/
import dcrypto from "../src";
import {
crypto_sign_ed25519_SECRETKEYBYTES,
crypto_hash_sha512_BYTES,
} from "../src/utils/interfaces";
const arraysAreEqual = (arr1: Uint8Array, arr2: Uint8Array): boolean => {
const len = arr1.length;
if (len !== arr2.length) return false;
for (let i = 0; i < len; i++) {
if (arr1[i] !== arr2[i]) return false;
}
return true;
};
describe("Browser-based tests.", () => {
test("Generating random bytes with webcrypto works.", async () => {
const message = await dcrypto.randomBytes(32);
const keypair = await dcrypto.keyPair();
const previousBlockHash = await dcrypto.randomBytes(
crypto_hash_sha512_BYTES,
);
const encrypted = await dcrypto.encryptForwardSecrecy(
message,
keypair.publicKey,
previousBlockHash,
);
const decrypted = await dcrypto.decryptForwardSecrecy(
encrypted,
keypair.secretKey,
previousBlockHash,
);
expect(decrypted[0]).toBe(message[0]);
expect(decrypted[1]).toBe(message[1]);
expect(decrypted[31]).toBe(message[31]);
});
test("Loading libsodium wasm module in the browser and crypto operations work.", async () => {
const randomBytes = await dcrypto.randomBytes(256);
const hash = await dcrypto.sha512(randomBytes);
const key = await dcrypto.randomBytes(
dcrypto.constants.crypto_kx_SESSIONKEYBYTES,
);
const keypair = await dcrypto.keyPair();
const signature = await dcrypto.sign(randomBytes, keypair.secretKey);
const verification = await dcrypto.verify(
randomBytes,
signature,
keypair.publicKey,
);
const encrypted = await dcrypto.encryptForwardSecrecy(
randomBytes,
keypair.publicKey,
hash,
);
const decrypted = await dcrypto.decryptForwardSecrecy(
encrypted,
keypair.secretKey,
hash,
);
const encrypted1 = await dcrypto.encryptSymmetricKey(
randomBytes,
key,
hash,
);
const decrypted1 = await dcrypto.decryptSymmetricKey(encrypted1, key, hash);
expect(verification).toBe(true);
expect(arraysAreEqual(randomBytes, decrypted)).toBe(true);
expect(arraysAreEqual(randomBytes, decrypted1)).toBe(true);
});
test("Loading shamir wasm module in the browser and splitting/restoring works.", async () => {
const shamirSplitMemory = dcrypto.loadWasmMemory.splitSecret(
crypto_sign_ed25519_SECRETKEYBYTES,
20,
11,
);
const splitModule = await dcrypto.loadWasmModule({
wasmMemory: shamirSplitMemory,
});
const keypair = await dcrypto.keyPair();
const shares = await dcrypto.splitSecret(
keypair.secretKey,
10,
6,
splitModule,
);
const shuffled = await dcrypto.arrayRandomShuffle(shares);
const shamirRestoreMemory = dcrypto.loadWasmMemory.restoreSecret(
crypto_sign_ed25519_SECRETKEYBYTES,
20,
);
const restoreModule = await dcrypto.loadWasmModule({
wasmMemory: shamirRestoreMemory,
});
const reconstructed = await dcrypto.restoreSecret(shuffled, restoreModule);
expect(arraysAreEqual(keypair.secretKey, reconstructed)).toBe(true);
});
test("Loading utils wasm module in the browser and operations work.", async () => {
const min = 1;
const max = 256000;
const someNumber = await dcrypto.randomNumberInRange(min, max);
const someOtherNumber = await dcrypto.randomNumberInRange(min, max);
const someArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const reshuffled = await dcrypto.arrayRandomShuffle(someArray);
let everyElementInSamePlace = true;
for (let i = 0; i < reshuffled.length; i++) {
if (someArray[i] !== reshuffled[i]) {
everyElementInSamePlace = false;
}
}
expect(everyElementInSamePlace).toBe(false);
expect(min).toBeLessThanOrEqual(someNumber);
expect(someNumber).toBeLessThanOrEqual(max);
expect(someNumber === someOtherNumber).toBe(false);
});
});