Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/hashes/hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const DEFAULT_MIN_DIGEST_LENGTH = 20
export interface HasherInit <Name extends string, Code extends number> {
name: Name
code: Code
encode(input: Uint8Array): Await<Uint8Array>
encode(input: Uint8Array): Await<Uint8Array<ArrayBuffer>>

/**
* The minimum length a hash is allowed to be truncated to in bytes
Expand All @@ -24,7 +24,7 @@ export interface HasherInit <Name extends string, Code extends number> {
maxDigestLength?: number
}

export function from <Name extends string, Code extends number> ({ name, code, encode, minDigestLength, maxDigestLength }: HasherInit<Name, Code>): Hasher<Name, Code> {
export function from <Name extends string, Code extends number> ({ name, code, encode, minDigestLength, maxDigestLength }: HasherInit<Name, Code>): MultihashHasher<Code> {
return new Hasher(name, code, encode, minDigestLength, maxDigestLength)
}

Expand All @@ -49,11 +49,11 @@ export interface DigestOptions {
export class Hasher<Name extends string, Code extends number> implements MultihashHasher<Code> {
readonly name: Name
readonly code: Code
readonly encode: (input: Uint8Array) => Await<Uint8Array>
readonly encode: (input: Uint8Array) => Await<Uint8Array<ArrayBuffer>>
readonly minDigestLength: number
readonly maxDigestLength?: number

constructor (name: Name, code: Code, encode: (input: Uint8Array) => Await<Uint8Array>, minDigestLength?: number, maxDigestLength?: number) {
constructor (name: Name, code: Code, encode: (input: Uint8Array) => Await<Uint8Array<ArrayBuffer>>, minDigestLength?: number, maxDigestLength?: number) {
this.name = name
this.code = code
this.encode = encode
Expand Down
5 changes: 3 additions & 2 deletions src/hashes/identity.ts
Original file line number Diff line number Diff line change
@@ -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<ArrayBuffer> = coerce

function digest (input: Uint8Array, options?: DigestOptions): Digest.Digest<typeof code, number> {
if (options?.truncate != null && options.truncate !== input.byteLength) {
Expand All @@ -19,4 +20,4 @@ function digest (input: Uint8Array, options?: DigestOptions): Digest.Digest<type
return Digest.create(code, encode(input))
}

export const identity = { code, name, encode, digest }
export const identity: SyncMultihashHasher<0x00> = { code, name, encode, digest }
7 changes: 7 additions & 0 deletions src/hashes/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export interface MultihashHasher<Code extends number = number> {
*/
digest(input: Uint8Array, options?: DigestOptions): Promise<MultihashDigest<Code>> | MultihashDigest<Code>

/**
* Returns the raw digest representation of the binary input (e.g. without
* hashing codec)
*/
encode(input: Uint8Array): Promise<Uint8Array<ArrayBuffer>> | Uint8Array<ArrayBuffer>

/**
* Name of the multihash
*/
Expand All @@ -69,4 +75,5 @@ export interface MultihashHasher<Code extends number = number> {
*/
export interface SyncMultihashHasher<Code extends number = number> extends MultihashHasher<Code> {
digest(input: Uint8Array, options?: DigestOptions): MultihashDigest<Code>
encode(input: Uint8Array): Uint8Array<ArrayBuffer>
}
2 changes: 0 additions & 2 deletions src/hashes/sha1-browser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* global crypto */

import { from } from './hasher.ts'

const sha = (name: AlgorithmIdentifier) =>
Expand Down
4 changes: 1 addition & 3 deletions src/hashes/sha2-browser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* global crypto */

import { from } from './hasher.ts'

function sha (name: AlgorithmIdentifier): (data: Uint8Array<ArrayBuffer>) => Promise<Uint8Array> {
function sha (name: AlgorithmIdentifier): (data: Uint8Array<ArrayBuffer>) => Promise<Uint8Array<ArrayBuffer>> {
return async data => new Uint8Array(await crypto.subtle.digest(name, data))
}

Expand Down
Loading