55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
/**
|
|
* SHA-224 bundled digest vectors; SHA-384 via Web Crypto (same as sha384sum command).
|
|
*/
|
|
import { readFileSync } from 'node:fs'
|
|
import path from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import vm from 'node:vm'
|
|
import test from 'brittle'
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
const sha224Lib = path.join(__dirname, '../lib/engines/sha224.js')
|
|
|
|
function loadBareSha224() {
|
|
const src = readFileSync(sha224Lib, 'utf8')
|
|
const ctx = {}
|
|
vm.createContext(ctx)
|
|
vm.runInContext(src + '\nthis.__fn = bareSha224DigestBytes;', ctx)
|
|
return ctx.__fn
|
|
}
|
|
|
|
test('bareSha224DigestBytes matches known vectors', async (t) => {
|
|
const bareSha224DigestBytes = loadBareSha224()
|
|
t.is(
|
|
bareSha224DigestBytes(new Uint8Array(0)),
|
|
'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f'
|
|
)
|
|
t.is(
|
|
bareSha224DigestBytes(new TextEncoder().encode('abc')),
|
|
'23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7'
|
|
)
|
|
})
|
|
|
|
test('SHA-384 empty and abc via subtle.digest', async (t) => {
|
|
const subtle = globalThis.crypto?.subtle
|
|
if (!subtle?.digest) {
|
|
t.pass('skip: no Web Crypto subtle')
|
|
return
|
|
}
|
|
async function hex384(u8) {
|
|
const hash = await subtle.digest('SHA-384', u8)
|
|
return [...new Uint8Array(hash)]
|
|
.map((b) => b.toString(16).padStart(2, '0'))
|
|
.join('')
|
|
}
|
|
t.is(
|
|
await hex384(new Uint8Array(0)),
|
|
'38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b'
|
|
)
|
|
const abc = new TextEncoder().encode('abc')
|
|
t.is(
|
|
await hex384(abc),
|
|
'cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7'
|
|
)
|
|
})
|