Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
import { createHash } from 'node:crypto'
import { describe, expect, it } from 'vitest'
import { sha256Hex } from '../sha256'
const utf8 = (text: string) => new TextEncoder().encode(text)
describe('sha256Hex', () => {
it('matches the FIPS 180-4 test vectors', () => {
expect(sha256Hex(utf8(''))).toBe(
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
)
expect(sha256Hex(utf8('abc'))).toBe(
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
)
expect(
sha256Hex(utf8('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'))
).toBe('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1')
expect(sha256Hex(utf8('a'.repeat(1_000_000)))).toBe(
'cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0'
)
})
it('agrees with node:crypto at every padding boundary', () => {
for (let length = 0; length <= 130; length++) {
const bytes = Uint8Array.from({ length }, (_, i) => (i * 31 + length) & 0xff)
expect(sha256Hex(bytes), `length ${length}`).toBe(
createHash('sha256').update(bytes).digest('hex')
)
}
})
})