]> git.codecow.com Git - libnemo.git/commitdiff
Narrow type checking in blake digest.
authorChris Duncan <chris@zoso.dev>
Thu, 7 Aug 2025 20:45:11 +0000 (13:45 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 7 Aug 2025 20:45:11 +0000 (13:45 -0700)
src/lib/blake2b.ts

index 319e73b25f765bf69ef572b894ba35372f1ad507..1c539e5c8eba4c0477285dfccb07c455f324d5f9 100644 (file)
@@ -145,7 +145,7 @@ export class Blake2b {
        *
        * @param {Uint8Array} out - Buffer to store the final output
        */
-       #blake2bFinal (out: Uint8Array): void {
+       #blake2bFinal (out: Uint8Array<ArrayBuffer>): 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<ArrayBuffer>
        digest (out: 'hex'): string
        digest (out: 'binary' | Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer>
-       digest (out?: 'binary' | 'hex' | Uint8Array<ArrayBuffer>): string | Uint8Array<ArrayBuffer> {
-               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<ArrayBuffer> {
+               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')