-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcryptography.test.ts
More file actions
136 lines (110 loc) · 4.26 KB
/
cryptography.test.ts
File metadata and controls
136 lines (110 loc) · 4.26 KB
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
133
134
135
136
import { bytesToHex } from '@metamask/utils';
// eslint-disable-next-line n/no-unsupported-features/node-builtins
import { webcrypto } from 'crypto';
import {
hmacSha512,
keccak256,
pbkdf2Sha512,
ripemd160,
sha256,
} from './cryptography';
import * as utils from './utils';
// Node.js <20 doesn't have `globalThis.crypto`, so we need to define it.
// TODO: Remove this once we drop support for Node.js <20.
Object.defineProperty(globalThis, 'crypto', { value: webcrypto });
describe('hmacSha512', () => {
it('returns the HMAC-SHA-512 when using a custom implementation', async () => {
const key = new Uint8Array(32);
const data = new Uint8Array(32);
const hash = new Uint8Array(64).fill(1);
const customHmacSha512 = jest.fn().mockResolvedValue(hash);
const result = await hmacSha512(key, data, {
hmacSha512: customHmacSha512,
});
expect(result).toBe(hash);
expect(customHmacSha512).toHaveBeenCalledWith(key, data);
});
it('returns the HMAC-SHA-512 when using the Web Crypto API', async () => {
const key = new Uint8Array(32);
const data = new Uint8Array(32);
const result = await hmacSha512(key, data);
expect(bytesToHex(result)).toBe(
'0xbae46cebebbb90409abc5acf7ac21fdb339c01ce15192c52fb9e8aa11a8de9a4ea15a045f2be245fbb98916a9ae81b353e33b9c42a55380c5158241daeb3c6dd',
);
});
it('returns the HMAC-SHA-512 when using the fallback', async () => {
jest.spyOn(utils, 'isWebCryptoSupported').mockReturnValueOnce(false);
const key = new Uint8Array(32);
const data = new Uint8Array(32);
const result = await hmacSha512(key, data);
expect(bytesToHex(result)).toBe(
'0xbae46cebebbb90409abc5acf7ac21fdb339c01ce15192c52fb9e8aa11a8de9a4ea15a045f2be245fbb98916a9ae81b353e33b9c42a55380c5158241daeb3c6dd',
);
});
});
describe('keccak256', () => {
it('returns the keccak-256 hash of the data', () => {
const data = new Uint8Array(32).fill(1);
const hash = keccak256(data);
expect(bytesToHex(hash)).toBe(
'0xcebc8882fecbec7fb80d2cf4b312bec018884c2d66667c67a90508214bd8bafc',
);
});
});
describe('pbkdf2Sha512', () => {
it('returns the PBKDF2-SHA-512 when using a custom implementation', async () => {
const password = new Uint8Array(32);
const salt = new Uint8Array(32);
const iterations = 1000;
const keyLength = 64;
const hash = new Uint8Array(64).fill(1);
const customPbkdf2Sha512 = jest.fn().mockResolvedValue(hash);
const result = await pbkdf2Sha512(password, salt, iterations, keyLength, {
pbkdf2Sha512: customPbkdf2Sha512,
});
expect(result).toBe(hash);
expect(customPbkdf2Sha512).toHaveBeenCalledWith(
password,
salt,
iterations,
keyLength,
);
});
it('returns the PBKDF2-SHA-512 when using the Web Crypto API', async () => {
const password = new Uint8Array(32);
const salt = new Uint8Array(32);
const iterations = 1000;
const keyLength = 64;
const result = await pbkdf2Sha512(password, salt, iterations, keyLength);
expect(bytesToHex(result)).toBe(
'0xab3d65e9e6341a924c752a77b8dc6b78f1e6db5d31df7dd0cc534039dd9662a97bcaf0b959fe78248a49859c7952ddb25d66840f052b27ef1ab60b9446c0c9fd',
);
});
it('returns the PBKDF2-SHA-512 when using the fallback', async () => {
jest.spyOn(utils, 'isWebCryptoSupported').mockReturnValueOnce(false);
const password = new Uint8Array(32);
const salt = new Uint8Array(32);
const iterations = 1000;
const keyLength = 64;
const result = await pbkdf2Sha512(password, salt, iterations, keyLength);
expect(bytesToHex(result)).toBe(
'0xab3d65e9e6341a924c752a77b8dc6b78f1e6db5d31df7dd0cc534039dd9662a97bcaf0b959fe78248a49859c7952ddb25d66840f052b27ef1ab60b9446c0c9fd',
);
});
});
describe('ripemd160', () => {
it('returns the RIPEMD-160 hash of the data', () => {
const data = new Uint8Array(32).fill(1);
const hash = ripemd160(data);
expect(bytesToHex(hash)).toBe('0x422d0010f16ae8539c53eb57a912890244a9eb5a');
});
});
describe('sha256', () => {
it('returns the SHA-256 hash of the data', () => {
const data = new Uint8Array(32).fill(1);
const hash = sha256(data);
expect(bytesToHex(hash)).toBe(
'0x72cd6e8422c407fb6d098690f1130b7ded7ec2f7f5e1d30bd9d521f015363793',
);
});
});