Skip to content

Commit

Permalink
[sha256] rename hash()=>digest()
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Aug 8, 2023
1 parent dbca8ed commit 74aa889
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions hash/sha256.fallback.js
Expand Up @@ -119,7 +119,7 @@ class Hasher {
/**
* @param {Uint8Array} data
*/
hash (data) {
digest (data) {
let i = 0
for (; i + 56 <= data.length;) {
// write data in big endianess
Expand Down Expand Up @@ -169,6 +169,6 @@ class Hasher {
/**
* @param {Uint8Array} data
*/
export const hash = data => {
return new Hasher().hash(data)
export const digest = data => {
return new Hasher().digest(data)
}
2 changes: 1 addition & 1 deletion hash/sha256.node.js
Expand Up @@ -3,7 +3,7 @@ import { createHash } from 'node:crypto'
/**
* @param {Uint8Array} data
*/
export const hash = data => {
export const digest = data => {
const hasher = createHash('sha256')
hasher.update(data)
return hasher.digest()
Expand Down
12 changes: 6 additions & 6 deletions hash/sha256.test.js
Expand Up @@ -19,7 +19,7 @@ export const testSha256Basics = async _tc => {
*/
const test = async (data, result) => {
data = typeof data === 'string' ? buffer.fromHexString(data) : data
const res = sha256.hash(data)
const res = sha256.digest(data)
const resHex = buffer.toHexString(res)
t.assert(resHex === result)
const resWebcrypto = new Uint8Array(await webcrypto.subtle.digest('SHA-256', data))
Expand Down Expand Up @@ -54,7 +54,7 @@ export const testLargeValue = async _tc => {
if (env.isNode) {
const sha256Node = await import('./sha256.node.js')
t.measureTime(`[node] Hash message of size ${BS}`, () => {
const res = new Uint8Array(sha256Node.hash(data))
const res = new Uint8Array(sha256Node.digest(data))
if (!f.equalityDeep(res, resNode)) {
console.warn(`Precomputed result should be the same! New result: ${buffer.toBase64(res)}`)
}
Expand All @@ -63,7 +63,7 @@ export const testLargeValue = async _tc => {
})
}
t.measureTime(`[lib0] Hash message of size ${BS}`, () => {
resLib0 = sha256.hash(data)
resLib0 = sha256.digest(data)
})
await t.measureTimeAsync(`[webcrypto] Hash message of size ${BS}`, async () => {
resWebcrypto = new Uint8Array(await webcrypto.subtle.digest('SHA-256', data))
Expand All @@ -78,7 +78,7 @@ export const testLargeValue = async _tc => {
export const testRepeatSha256Hashing = async tc => {
const LEN = prng.bool(tc.prng) ? prng.uint32(tc.prng, 0, 512) : prng.uint32(tc.prng, 0, 3003030)
const data = prng.uint8Array(tc.prng, LEN)
const hashedCustom = sha256.hash(data)
const hashedCustom = sha256.digest(data)
const hashedWebcrypto = new Uint8Array(await webcrypto.subtle.digest('SHA-256', data))
t.compare(hashedCustom, hashedWebcrypto)
}
Expand All @@ -96,15 +96,15 @@ export const testBenchmarkSha256 = async _tc => {
const datas = array.unfold(N, () => prng.uint8Array(gen, BS))
t.measureTime('lib0 (fallback))', () => {
for (let i = 0; i < N; i++) {
const x = sha256.hash(datas[i])
const x = sha256.digest(datas[i])
if (x === null) throw new Error()
}
})
if (env.isNode) {
const nodeSha = await import('./sha256.node.js')
t.measureTime('lib0 (node))', () => {
for (let i = 0; i < N; i++) {
const x = nodeSha.hash(datas[i])
const x = nodeSha.digest(datas[i])
if (x === null) throw new Error()
}
})
Expand Down

0 comments on commit 74aa889

Please sign in to comment.