From 082f0ecadf9a053a5c606f1eb6f19772bd944082 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 27 Nov 2024 14:04:04 -0800 Subject: [PATCH] Condense assertions. --- src/lib/blake2b.ts | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/src/lib/blake2b.ts b/src/lib/blake2b.ts index cbc9777..6b8e634 100644 --- a/src/lib/blake2b.ts +++ b/src/lib/blake2b.ts @@ -216,10 +216,8 @@ function blake2b (outlen: number, key?: Uint8Array, salt?: Uint8Array, personal? function digest (out?: 'binary' | Uint8Array): Uint8Array function digest (out?: 'binary' | 'hex' | Uint8Array): string | Uint8Array { var buf = (!out || out === 'binary' || out === 'hex') ? new Uint8Array(outlen) : out - if (!(buf instanceof Uint8Array)) - throw new TypeError(`out must be "binary", "hex", Uint8Array, or Buffer`) - if (buf.length < outlen) - throw new RangeError(`out must have at least outlen bytes of space`) + if (!(buf instanceof Uint8Array)) throw new TypeError(`out must be "binary", "hex", Uint8Array, or Buffer`) + if (buf.length < outlen) throw new RangeError(`out must have at least outlen bytes of space`) blake2bFinal(this, buf) if (out === 'hex') return hexSlice(buf) return buf @@ -266,9 +264,9 @@ function blake2b (outlen: number, key?: Uint8Array, salt?: Uint8Array, personal? const toHex = function (n: number) { if (typeof n !== 'number') - throw new TypeError(`expected number to convert to hex, received convert ${typeof n}`) + throw new TypeError(`expected number to convert to hex; received ${typeof n}`) if (n < 0 || n > 255) - throw new RangeError(`expected byte value 0-255, received ${n}`) + throw new RangeError(`expected byte value 0-255; received ${n}`) return n.toString(16).padStart(2, '0') } @@ -312,29 +310,20 @@ function blake2b (outlen: number, key?: Uint8Array, salt?: Uint8Array, personal? // finally execute the original function call if (noAssert !== true) { - if (outlen < BYTES_MIN) - throw new RangeError(`outlen must be at least ${BYTES_MIN}, was given ${outlen}`) - if (outlen > BYTES_MAX) - throw new RangeError(`outlen must be at most ${BYTES_MAX}, was given ${outlen}`) + if (outlen < BYTES_MIN) throw new RangeError(`expected outlen >= ${BYTES_MIN}; actual ${outlen}`) + if (outlen > BYTES_MAX) throw new RangeError(`expectd outlen <= ${BYTES_MAX}; actual ${outlen}`) if (key != null) { - if (!(key instanceof Uint8Array)) - throw new TypeError(`key must be Uint8Array or Buffer`) - if (key.length < KEYBYTES_MIN) - throw new RangeError(`key must be at least ${KEYBYTES_MIN}, was given ${key.length}`) - if (key.length > KEYBYTES_MAX) - throw new RangeError(`key must be at most ${KEYBYTES_MAX}, was given ${key.length}`) + if (!(key instanceof Uint8Array)) throw new TypeError(`key must be Uint8Array or Buffer`) + if (key.length < KEYBYTES_MIN) throw new RangeError(`expected key >= ${KEYBYTES_MIN}; actual ${key.length}`) + if (key.length > KEYBYTES_MAX) throw new RangeError(`expected key <= ${KEYBYTES_MAX}; actual ${key.length}`) } if (salt != null) { - if (!(salt instanceof Uint8Array)) - throw new TypeError(`salt must be Uint8Array or Buffer`) - if (salt.length !== SALTBYTES) - throw new RangeError(`salt must be exactly ${SALTBYTES}, was given ${salt.length}`) + if (!(salt instanceof Uint8Array)) throw new TypeError(`salt must be Uint8Array or Buffer`) + if (salt.length !== SALTBYTES) throw new RangeError(`expected salt ${SALTBYTES} bytes; actual ${salt.length} bytes`) } if (personal != null) { - if (!(personal instanceof Uint8Array)) - throw new TypeError(`personal must be Uint8Array or Buffer`) - if (personal.length !== PERSONALBYTES) - throw new RangeError(`personal must be exactly ${PERSONALBYTES}, was given ${personal.length}`) + if (!(personal instanceof Uint8Array)) throw new TypeError(`personal must be Uint8Array or Buffer`) + if (personal.length !== PERSONALBYTES) throw new RangeError(`expected personal ${PERSONALBYTES} bytes; actual ${personal.length} bytes`) } } -- 2.34.1