|
| 1 | +import crypto from 'crypto'; |
| 2 | +import { createSHA512 } from 'hash-wasm'; |
| 3 | +import { generateFileBucketKey, generateFileKey, getFileDeterministicKey } from './crypto'; |
| 4 | + |
| 5 | +type Bytes = { buffer: ArrayBufferLike; byteOffset: number; byteLength: number }; |
| 6 | +const asUint8 = (v: Bytes): Uint8Array => new Uint8Array(v.buffer, v.byteOffset, v.byteLength); |
| 7 | + |
| 8 | +const reference = { |
| 9 | + getFileDeterministicKey(key: Bytes, data: Bytes): Buffer { |
| 10 | + return crypto.createHash('sha512').update(asUint8(key)).update(asUint8(data)).digest(); |
| 11 | + }, |
| 12 | + |
| 13 | + async generateFileBucketKey(mnemonic: string, bucketId: string): Promise<Buffer> { |
| 14 | + const seed = crypto.pbkdf2Sync(mnemonic, 'mnemonic', 2048, 64, 'sha512'); |
| 15 | + return reference.getFileDeterministicKey(seed, Buffer.from(bucketId, 'hex')); |
| 16 | + }, |
| 17 | + |
| 18 | + async generateFileKey(mnemonic: string, bucketId: string, index: Bytes): Promise<Buffer> { |
| 19 | + const bucketKey = await reference.generateFileBucketKey(mnemonic, bucketId); |
| 20 | + return reference.getFileDeterministicKey(bucketKey.subarray(0, 32), index).subarray(0, 32); |
| 21 | + }, |
| 22 | +}; |
| 23 | + |
| 24 | +const MNEMONIC = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'; |
| 25 | +const BUCKET_ID = 'a1b2c3d4e5f6a1b2c1d2e3f4a5b6c7d8'; |
| 26 | +const INDEX = Buffer.from([0, 0, 0, 1]); |
| 27 | + |
| 28 | +describe('getFileDeterministicKey', () => { |
| 29 | + describe('output shape', () => { |
| 30 | + it('when called with buffer inputs, then returns a 64-byte Buffer', () => { |
| 31 | + const result = getFileDeterministicKey(Buffer.from('key'), Buffer.from('data')); |
| 32 | + expect(result).toBeInstanceOf(Buffer); |
| 33 | + expect(result.length).toBe(64); |
| 34 | + }); |
| 35 | + |
| 36 | + it('when called with string inputs, then returns a 64-byte Buffer', () => { |
| 37 | + const result = getFileDeterministicKey('key', 'data'); |
| 38 | + expect(result).toBeInstanceOf(Buffer); |
| 39 | + expect(result.length).toBe(64); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('compatibility', () => { |
| 44 | + it('when compared to Node.js SHA512, then produces identical output', () => { |
| 45 | + const key = Buffer.from('test_key_bytes'); |
| 46 | + const data = Buffer.from('test_data_bytes'); |
| 47 | + const result = getFileDeterministicKey(key, data); |
| 48 | + const expected = reference.getFileDeterministicKey(key, data); |
| 49 | + expect(result.toString('hex')).toBe(expected.toString('hex')); |
| 50 | + }); |
| 51 | + |
| 52 | + it('when compared to hash-wasm SHA512 (drive-web), then produces identical output', async () => { |
| 53 | + const key = Buffer.from('test_key_bytes'); |
| 54 | + const data = Buffer.from('test_data_bytes'); |
| 55 | + const result = getFileDeterministicKey(key, data); |
| 56 | + const hash = await createSHA512(); |
| 57 | + const expected = hash.init().update(key).update(data).digest(); |
| 58 | + expect(result.toString('hex')).toBe(expected); |
| 59 | + }); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('generateFileBucketKey', () => { |
| 64 | + describe('output shape', () => { |
| 65 | + it('when called with a valid mnemonic and bucketId, then returns a 64-byte Buffer', async () => { |
| 66 | + const result = await generateFileBucketKey(MNEMONIC, BUCKET_ID); |
| 67 | + expect(result).toBeInstanceOf(Buffer); |
| 68 | + expect(result.length).toBe(64); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('compatibility', () => { |
| 73 | + it('when compared to Node.js PBKDF2 + SHA512 reference, then produces identical output', async () => { |
| 74 | + const result = await generateFileBucketKey(MNEMONIC, BUCKET_ID); |
| 75 | + const expected = await reference.generateFileBucketKey(MNEMONIC, BUCKET_ID); |
| 76 | + expect(result.toString('hex')).toBe(expected.toString('hex')); |
| 77 | + }); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe('generateFileKey', () => { |
| 82 | + describe('output shape', () => { |
| 83 | + it('when called with valid inputs, then returns a 32-byte Buffer', async () => { |
| 84 | + const result = await generateFileKey(MNEMONIC, BUCKET_ID, INDEX); |
| 85 | + expect(result).toBeInstanceOf(Buffer); |
| 86 | + expect(result.length).toBe(32); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('compatibility', () => { |
| 91 | + it('when compared to Node.js reference, then produces identical output for Buffer index', async () => { |
| 92 | + const result = await generateFileKey(MNEMONIC, BUCKET_ID, INDEX); |
| 93 | + const expected = await reference.generateFileKey(MNEMONIC, BUCKET_ID, INDEX); |
| 94 | + expect(result.toString('hex')).toBe(expected.toString('hex')); |
| 95 | + }); |
| 96 | + |
| 97 | + it('when compared to Node.js reference, then produces identical output for string index', async () => { |
| 98 | + const index = 'abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890'; |
| 99 | + const result = await generateFileKey(MNEMONIC, BUCKET_ID, index); |
| 100 | + const expected = await reference.generateFileKey(MNEMONIC, BUCKET_ID, Buffer.from(index)); |
| 101 | + expect(result.toString('hex')).toBe(expected.toString('hex')); |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments