|
| 1 | +import { Buffer } from '@craftzdog/react-native-buffer'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { test } from '../util'; |
| 4 | + |
| 5 | +import crypto from 'react-native-quick-crypto'; |
| 6 | + |
| 7 | +const SUITE = 'timingSafeEqual'; |
| 8 | + |
| 9 | +test(SUITE, 'should return true for equal buffers', () => { |
| 10 | + const a = Buffer.from('hello world'); |
| 11 | + const b = Buffer.from('hello world'); |
| 12 | + expect(crypto.timingSafeEqual(a, b)).to.equal(true); |
| 13 | +}); |
| 14 | + |
| 15 | +test(SUITE, 'should return false for different buffers', () => { |
| 16 | + const a = Buffer.from('hello world'); |
| 17 | + const b = Buffer.from('hello worlD'); |
| 18 | + expect(crypto.timingSafeEqual(a, b)).to.equal(false); |
| 19 | +}); |
| 20 | + |
| 21 | +test(SUITE, 'should work with Uint8Array', () => { |
| 22 | + const a = new Uint8Array([1, 2, 3, 4, 5]); |
| 23 | + const b = new Uint8Array([1, 2, 3, 4, 5]); |
| 24 | + expect(crypto.timingSafeEqual(a, b)).to.equal(true); |
| 25 | +}); |
| 26 | + |
| 27 | +test(SUITE, 'should return false for different Uint8Array', () => { |
| 28 | + const a = new Uint8Array([1, 2, 3, 4, 5]); |
| 29 | + const b = new Uint8Array([1, 2, 3, 4, 6]); |
| 30 | + expect(crypto.timingSafeEqual(a, b)).to.equal(false); |
| 31 | +}); |
| 32 | + |
| 33 | +test(SUITE, 'should work with ArrayBuffer', () => { |
| 34 | + const a = new Uint8Array([0xde, 0xad, 0xbe, 0xef]).buffer; |
| 35 | + const b = new Uint8Array([0xde, 0xad, 0xbe, 0xef]).buffer; |
| 36 | + expect(crypto.timingSafeEqual(a, b)).to.equal(true); |
| 37 | +}); |
| 38 | + |
| 39 | +test(SUITE, 'should throw for different length buffers', () => { |
| 40 | + const a = Buffer.from('hello'); |
| 41 | + const b = Buffer.from('hello world'); |
| 42 | + expect(() => crypto.timingSafeEqual(a, b)).to.throw(RangeError); |
| 43 | +}); |
| 44 | + |
| 45 | +test(SUITE, 'should work with empty buffers', () => { |
| 46 | + const a = Buffer.alloc(0); |
| 47 | + const b = Buffer.alloc(0); |
| 48 | + expect(crypto.timingSafeEqual(a, b)).to.equal(true); |
| 49 | +}); |
| 50 | + |
| 51 | +test(SUITE, 'should work with single byte buffers', () => { |
| 52 | + const a = Buffer.from([0xff]); |
| 53 | + const b = Buffer.from([0xff]); |
| 54 | + expect(crypto.timingSafeEqual(a, b)).to.equal(true); |
| 55 | + |
| 56 | + const c = Buffer.from([0x00]); |
| 57 | + expect(crypto.timingSafeEqual(a, c)).to.equal(false); |
| 58 | +}); |
| 59 | + |
| 60 | +test(SUITE, 'should work for HMAC comparison use case', () => { |
| 61 | + const hmac1 = crypto |
| 62 | + .createHmac('sha256', 'secret') |
| 63 | + .update('message') |
| 64 | + .digest(); |
| 65 | + const hmac2 = crypto |
| 66 | + .createHmac('sha256', 'secret') |
| 67 | + .update('message') |
| 68 | + .digest(); |
| 69 | + const hmac3 = crypto |
| 70 | + .createHmac('sha256', 'secret') |
| 71 | + .update('different') |
| 72 | + .digest(); |
| 73 | + |
| 74 | + expect(crypto.timingSafeEqual(hmac1, hmac2)).to.equal(true); |
| 75 | + expect(crypto.timingSafeEqual(hmac1, hmac3)).to.equal(false); |
| 76 | +}); |
0 commit comments