diff --git a/src/hashes/hasher.ts b/src/hashes/hasher.ts index b4715f0..8691b39 100644 --- a/src/hashes/hasher.ts +++ b/src/hashes/hasher.ts @@ -8,7 +8,7 @@ const DEFAULT_MIN_DIGEST_LENGTH = 20 export interface HasherInit { name: Name code: Code - encode(input: Uint8Array): Await + encode(input: Uint8Array): Await> /** * The minimum length a hash is allowed to be truncated to in bytes @@ -24,7 +24,7 @@ export interface HasherInit { maxDigestLength?: number } -export function from ({ name, code, encode, minDigestLength, maxDigestLength }: HasherInit): Hasher { +export function from ({ name, code, encode, minDigestLength, maxDigestLength }: HasherInit): MultihashHasher { return new Hasher(name, code, encode, minDigestLength, maxDigestLength) } @@ -49,11 +49,11 @@ export interface DigestOptions { export class Hasher implements MultihashHasher { readonly name: Name readonly code: Code - readonly encode: (input: Uint8Array) => Await + readonly encode: (input: Uint8Array) => Await> readonly minDigestLength: number readonly maxDigestLength?: number - constructor (name: Name, code: Code, encode: (input: Uint8Array) => Await, minDigestLength?: number, maxDigestLength?: number) { + constructor (name: Name, code: Code, encode: (input: Uint8Array) => Await>, minDigestLength?: number, maxDigestLength?: number) { this.name = name this.code = code this.encode = encode diff --git a/src/hashes/identity.ts b/src/hashes/identity.ts index 4657707..f7c10a1 100644 --- a/src/hashes/identity.ts +++ b/src/hashes/identity.ts @@ -1,11 +1,12 @@ import { coerce } from '../bytes.ts' import * as Digest from './digest.ts' import type { DigestOptions } from './hasher.ts' +import type { SyncMultihashHasher } from './interface.ts' const code: 0x0 = 0x0 const name = 'identity' -const encode: (input: Uint8Array) => Uint8Array = coerce +const encode: (input: Uint8Array) => Uint8Array = coerce function digest (input: Uint8Array, options?: DigestOptions): Digest.Digest { if (options?.truncate != null && options.truncate !== input.byteLength) { @@ -19,4 +20,4 @@ function digest (input: Uint8Array, options?: DigestOptions): Digest.Digest = { code, name, encode, digest } diff --git a/src/hashes/interface.ts b/src/hashes/interface.ts index 5a6c944..59e469c 100644 --- a/src/hashes/interface.ts +++ b/src/hashes/interface.ts @@ -46,6 +46,12 @@ export interface MultihashHasher { */ digest(input: Uint8Array, options?: DigestOptions): Promise> | MultihashDigest + /** + * Returns the raw digest representation of the binary input (e.g. without + * hashing codec) + */ + encode(input: Uint8Array): Promise> | Uint8Array + /** * Name of the multihash */ @@ -69,4 +75,5 @@ export interface MultihashHasher { */ export interface SyncMultihashHasher extends MultihashHasher { digest(input: Uint8Array, options?: DigestOptions): MultihashDigest + encode(input: Uint8Array): Uint8Array } diff --git a/src/hashes/sha1-browser.ts b/src/hashes/sha1-browser.ts index a4c81ff..07a5679 100644 --- a/src/hashes/sha1-browser.ts +++ b/src/hashes/sha1-browser.ts @@ -1,5 +1,3 @@ -/* global crypto */ - import { from } from './hasher.ts' const sha = (name: AlgorithmIdentifier) => diff --git a/src/hashes/sha2-browser.ts b/src/hashes/sha2-browser.ts index b8c424c..bc63c15 100644 --- a/src/hashes/sha2-browser.ts +++ b/src/hashes/sha2-browser.ts @@ -1,8 +1,6 @@ -/* global crypto */ - import { from } from './hasher.ts' -function sha (name: AlgorithmIdentifier): (data: Uint8Array) => Promise { +function sha (name: AlgorithmIdentifier): (data: Uint8Array) => Promise> { return async data => new Uint8Array(await crypto.subtle.digest(name, data)) }