-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathtimingSafeEqual_tests.ts
More file actions
76 lines (63 loc) · 2.36 KB
/
timingSafeEqual_tests.ts
File metadata and controls
76 lines (63 loc) · 2.36 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
import { Buffer } from '@craftzdog/react-native-buffer';
import { expect } from 'chai';
import { test } from '../util';
import crypto from 'react-native-quick-crypto';
const SUITE = 'timingSafeEqual';
test(SUITE, 'should return true for equal buffers', () => {
const a = Buffer.from('hello world');
const b = Buffer.from('hello world');
expect(crypto.timingSafeEqual(a, b)).to.equal(true);
});
test(SUITE, 'should return false for different buffers', () => {
const a = Buffer.from('hello world');
const b = Buffer.from('hello worlD');
expect(crypto.timingSafeEqual(a, b)).to.equal(false);
});
test(SUITE, 'should work with Uint8Array', () => {
const a = new Uint8Array([1, 2, 3, 4, 5]);
const b = new Uint8Array([1, 2, 3, 4, 5]);
expect(crypto.timingSafeEqual(a, b)).to.equal(true);
});
test(SUITE, 'should return false for different Uint8Array', () => {
const a = new Uint8Array([1, 2, 3, 4, 5]);
const b = new Uint8Array([1, 2, 3, 4, 6]);
expect(crypto.timingSafeEqual(a, b)).to.equal(false);
});
test(SUITE, 'should work with ArrayBuffer', () => {
const a = new Uint8Array([0xde, 0xad, 0xbe, 0xef]).buffer;
const b = new Uint8Array([0xde, 0xad, 0xbe, 0xef]).buffer;
expect(crypto.timingSafeEqual(a, b)).to.equal(true);
});
test(SUITE, 'should throw for different length buffers', () => {
const a = Buffer.from('hello');
const b = Buffer.from('hello world');
expect(() => crypto.timingSafeEqual(a, b)).to.throw(RangeError);
});
test(SUITE, 'should work with empty buffers', () => {
const a = Buffer.alloc(0);
const b = Buffer.alloc(0);
expect(crypto.timingSafeEqual(a, b)).to.equal(true);
});
test(SUITE, 'should work with single byte buffers', () => {
const a = Buffer.from([0xff]);
const b = Buffer.from([0xff]);
expect(crypto.timingSafeEqual(a, b)).to.equal(true);
const c = Buffer.from([0x00]);
expect(crypto.timingSafeEqual(a, c)).to.equal(false);
});
test(SUITE, 'should work for HMAC comparison use case', () => {
const hmac1 = crypto
.createHmac('sha256', 'secret')
.update('message')
.digest();
const hmac2 = crypto
.createHmac('sha256', 'secret')
.update('message')
.digest();
const hmac3 = crypto
.createHmac('sha256', 'secret')
.update('different')
.digest();
expect(crypto.timingSafeEqual(hmac1, hmac2)).to.equal(true);
expect(crypto.timingSafeEqual(hmac1, hmac3)).to.equal(false);
});