From: Chris Duncan Date: Thu, 7 Aug 2025 20:45:11 +0000 (-0700) Subject: Narrow type checking in blake digest. X-Git-Tag: v0.10.5~43^2~49 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=ad8771958dd24387a2d0cf5029932f9ac69f9d03;p=libnemo.git Narrow type checking in blake digest. --- diff --git a/src/lib/blake2b.ts b/src/lib/blake2b.ts index 319e73b..1c539e5 100644 --- a/src/lib/blake2b.ts +++ b/src/lib/blake2b.ts @@ -145,7 +145,7 @@ export class Blake2b { * * @param {Uint8Array} out - Buffer to store the final output */ - #blake2bFinal (out: Uint8Array): void { + #blake2bFinal (out: Uint8Array): void { this.#t += BigInt(this.#c) // add final message block size to total bytes this.#b.fill(0, this.#c) // pad final block with zeros this.#blake2bCompress(true) // set final block flag and compress @@ -270,9 +270,11 @@ export class Blake2b { digest (): Uint8Array digest (out: 'hex'): string digest (out: 'binary' | Uint8Array): Uint8Array - digest (out?: 'binary' | 'hex' | Uint8Array): string | Uint8Array { - const buf = (!out || out === 'binary' || out === 'hex') ? new Uint8Array(this.#outlen) : out - if (!(buf instanceof Uint8Array)) throw new TypeError('out must be "binary", "hex", Uint8Array, or Buffer') + digest (out?: unknown): string | Uint8Array { + if (out !== undefined && out !== 'binary' && out !== 'hex' && !(out instanceof Uint8Array)) { + throw new TypeError('out must be "binary", "hex", Uint8Array, or Buffer') + } + const buf = (out === undefined || typeof out === 'string') ? new Uint8Array(this.#outlen) : new Uint8Array(out) if (buf.length < this.#outlen) throw new RangeError('out must have at least outlen bytes of space') this.#blake2bFinal(buf) return (out === 'hex')