|
| 1 | +/* eslint-disable @typescript-eslint/no-unused-expressions */ |
| 2 | +import { Buffer } from 'safe-buffer'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import { test } from '../util'; |
| 5 | + |
| 6 | +import crypto from 'react-native-quick-crypto'; |
| 7 | + |
| 8 | +const SUITE = 'scrypt'; |
| 9 | + |
| 10 | +// RFC 7914 Test Vectors |
| 11 | +// https://tools.ietf.org/html/rfc7914#section-2 |
| 12 | +const kTests = [ |
| 13 | + { |
| 14 | + password: '', |
| 15 | + salt: '', |
| 16 | + N: 16, |
| 17 | + r: 1, |
| 18 | + p: 1, |
| 19 | + keylen: 64, |
| 20 | + expected: |
| 21 | + '77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906', |
| 22 | + }, |
| 23 | + { |
| 24 | + password: 'password', |
| 25 | + salt: 'NaCl', |
| 26 | + N: 1024, |
| 27 | + r: 8, |
| 28 | + p: 16, |
| 29 | + keylen: 64, |
| 30 | + expected: |
| 31 | + 'fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640', |
| 32 | + }, |
| 33 | + { |
| 34 | + password: 'pleaseletmein', |
| 35 | + salt: 'SodiumChloride', |
| 36 | + N: 16384, |
| 37 | + r: 8, |
| 38 | + p: 1, |
| 39 | + keylen: 64, |
| 40 | + expected: |
| 41 | + '7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2d5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887', |
| 42 | + }, |
| 43 | +]; |
| 44 | + |
| 45 | +kTests.forEach(({ password, salt, N, r, p, keylen, expected }, index) => { |
| 46 | + const description = `RFC 7914 Test Case ${index + 1}`; |
| 47 | + |
| 48 | + test(SUITE, `${description} (async)`, () => { |
| 49 | + crypto.scrypt( |
| 50 | + password, |
| 51 | + salt, |
| 52 | + keylen, |
| 53 | + { N, r, p, maxmem: 32 * 1024 * 1024 }, // 32MB - generous headroom for all test cases |
| 54 | + (err, derivedKey) => { |
| 55 | + expect(err).to.be.null; |
| 56 | + expect(derivedKey).not.to.be.undefined; |
| 57 | + expect(derivedKey!.toString('hex')).to.equal(expected); |
| 58 | + }, |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + test(SUITE, `${description} (sync)`, () => { |
| 63 | + const derivedKey = crypto.scryptSync(password, salt, keylen, { |
| 64 | + N, |
| 65 | + r, |
| 66 | + p, |
| 67 | + maxmem: 32 * 1024 * 1024, // 32MB - generous headroom for all test cases |
| 68 | + }); |
| 69 | + expect(derivedKey).not.to.be.undefined; |
| 70 | + expect(derivedKey.toString('hex')).to.equal(expected); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +test(SUITE, 'should throw if no callback provided (async)', () => { |
| 75 | + expect(() => { |
| 76 | + crypto.scrypt('password', 'salt', 64); |
| 77 | + }).to.throw(/No callback provided/); |
| 78 | +}); |
| 79 | + |
| 80 | +test(SUITE, 'should handle default options (async)', () => { |
| 81 | + // This just tests it doesn't crash and returns a buffer |
| 82 | + crypto.scrypt('password', 'salt', 32, (err, key) => { |
| 83 | + expect(err).to.be.null; |
| 84 | + expect(key).to.be.instanceOf(Buffer); |
| 85 | + expect(key!.length).to.equal(32); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +test(SUITE, 'should handle aliases cost/blockSize/parallelization', () => { |
| 90 | + // Same as Test Case 1 but with named aliases |
| 91 | + const t = kTests[0]!; |
| 92 | + const derivedKey = crypto.scryptSync(t.password, t.salt, t.keylen, { |
| 93 | + cost: t.N, |
| 94 | + blockSize: t.r, |
| 95 | + parallelization: t.p, |
| 96 | + }); |
| 97 | + expect(derivedKey.toString('hex')).to.equal(t.expected); |
| 98 | +}); |
0 commit comments