import { describe, expect, it, } from "vitest"; import { bytesToHex, stringToBytes, } from "./bytes.js"; import { emptyHash, hash, HASH_SIZE, hashConcat, hashString, } from "./hash.js"; describe("hash utilities", () => { describe("hash", () => { it("should compute SHA-256 hash", () => { const data = stringToBytes("hello world"); const result = hash(data); // SHA-256 of "hello world" const expected = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"; expect(result.length).toBe(HASH_SIZE); expect(bytesToHex(result)).toBe(expected); }); it("should compute hash of empty bytes", () => { const data = new Uint8Array([]); const result = hash(data); // SHA-256 of empty string const expected = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; expect(bytesToHex(result)).toBe(expected); }); it("should produce different hashes for different inputs", () => { const hash1 = hash(stringToBytes("test1")); const hash2 = hash(stringToBytes("test2")); expect(hash1).not.toEqual(hash2); }); it("should produce same hash for same input", () => { const data = stringToBytes("consistent"); const hash1 = hash(data); const hash2 = hash(data); expect(hash1).toEqual(hash2); }); }); describe("hashConcat", () => { it("should hash concatenated arrays", () => { const a = stringToBytes("hello"); const b = stringToBytes(" "); const c = stringToBytes("world"); const result = hashConcat(a, b, c); // Should match hash of "hello world" const expected = hash(stringToBytes("hello world")); expect(result).toEqual(expected); }); it("should handle empty arrays", () => { const a = stringToBytes("test"); const b = new Uint8Array([]); const c = stringToBytes("data"); const result = hashConcat(a, b, c); const expected = hash(stringToBytes("testdata")); expect(result).toEqual(expected); }); it("should handle single array", () => { const data = stringToBytes("single"); const result = hashConcat(data); const expected = hash(data); expect(result).toEqual(expected); }); it("should handle no arrays", () => { const result = hashConcat(); const expected = hash(new Uint8Array([])); expect(result).toEqual(expected); }); }); describe("hashString", () => { it("should hash UTF-8 string", () => { const result = hashString("hello world"); // SHA-256 of "hello world" const expected = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"; expect(bytesToHex(result)).toBe(expected); }); it("should handle unicode characters", () => { const str = "Hello, 世界!"; const result1 = hashString(str); const result2 = hash(stringToBytes(str)); expect(result1).toEqual(result2); }); it("should handle empty string", () => { const result = hashString(""); // SHA-256 of empty string const expected = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; expect(bytesToHex(result)).toBe(expected); }); }); describe("emptyHash", () => { it("should create hash of correct size", () => { const result = emptyHash(); expect(result.length).toBe(HASH_SIZE); }); it("should create hash with all zeros", () => { const result = emptyHash(); expect(result.every(b => b === 0)).toBe(true); }); it("should create new instance each time", () => { const hash1 = emptyHash(); const hash2 = emptyHash(); expect(hash1).toEqual(hash2); expect(hash1).not.toBe(hash2); // Different object // Modifying one shouldn't affect the other hash1[0] = 255; expect(hash2[0]).toBe(0); }); }); describe("HASH_SIZE constant", () => { it("should be 32 bytes (SHA-256 output size)", () => { expect(HASH_SIZE).toBe(32); }); }); describe("compatibility with known vectors", () => { // Test vectors from various sources to ensure correctness it("should match known SHA-256 test vectors", () => { const testVectors: Array<[string, string]> = [ ["", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"], ["abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"], ["abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"], ]; for (const [input, expectedHex] of testVectors) { const result = hashString(input); expect(bytesToHex(result)).toBe(expectedHex); } }); }); });