import { describe, it, expect } from 'vitest'; import { sha256Base64 } from './hashing.js'; describe('hashing', () => { describe('sha256Base64', () => { const testString = 'This is a test'; const expectedHash = 'x74e2QL7jdTUiZfGRS9dflCfvNvigIsWvPTtzkwH0U4='; it('hashes a string correctly', async () => { const hash = await sha256Base64(testString); expect(hash).toEqual(expectedHash); }); it('hashes a node.js buffer correctly', async () => { const buffer = Buffer.from(testString); const hash = await sha256Base64(buffer); expect(hash).toEqual(expectedHash); }); it('hashes a Uint8Array correctly', async () => { const uint8Array = new TextEncoder().encode(testString); const hash = await sha256Base64(uint8Array); expect(hash).toEqual(expectedHash); }); it('hashes a Uint16Array correctly', async () => { const uint8Array = new TextEncoder().encode(testString); // before we convert (the underlying buffer) to a Uint16Array, we need to make sure that the length is a multiple of 2 expect(uint8Array.length % 2).toEqual(0); // create a new Uint16Array that is a view into the same underlying buffer const uint16Array = new Uint16Array(uint8Array.buffer, uint8Array.byteOffset, uint8Array.length / 2); const hash = await sha256Base64(uint16Array); expect(hash).toEqual(expectedHash); }); it('hashes an ArrayBuffer correctly', async () => { const uint8Array = new TextEncoder().encode(testString); // Create a new array buffer that is the same length as the uint8Array. We don't use the underlying buffer // of the uint8Array because it might be longer. Instead, we create a new array buffer that is the same length. const arrayBuffer = new ArrayBuffer(uint8Array.length); // copy the uint8Array into the arrayBuffer const arrayBufferView = new Uint8Array(arrayBuffer); arrayBufferView.set(uint8Array); const hash = await sha256Base64(arrayBuffer); expect(hash).toEqual(expectedHash); // also test the view const hashView = await sha256Base64(arrayBufferView); expect(hashView).toEqual(expectedHash); }); it('hashes a Uint8Array correctly, even when the underlying buffer is longer', async () => { const uint8Array = new TextEncoder().encode(testString); // Create a new array buffer that is longer than the uint8Array. const arrayBuffer = new ArrayBuffer(uint8Array.length + 2); // Copy the uint8Array into the arrayBuffer, starting at index 1. Set the first and last bytes to some fixed values. const arrayBufferView = new Uint8Array(arrayBuffer); arrayBufferView[0] = 1; arrayBufferView[arrayBufferView.length - 1] = 2; arrayBufferView.set(uint8Array, 1); // Create a new uint8Array that has the same content as the uint8Array, but has an underlying buffer that is longer. const uint8ArraySlice = new Uint8Array(arrayBuffer, 1, uint8Array.length); // Verify that the underlying buffer of the uint8ArraySlice is referencially the same as `arrayBuffer`. expect(uint8ArraySlice.buffer).toEqual(arrayBuffer); const hash = await sha256Base64(uint8ArraySlice); expect(hash).toEqual(expectedHash); }); }); });