From d2d83b0a66b046675d82505bb7521a2891049fe0 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 26 Nov 2024 14:00:08 -0800 Subject: [PATCH] Add type guards to function. Spaces. --- src/lib/blake2b.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/lib/blake2b.ts b/src/lib/blake2b.ts index 41237d6..3f040ec 100644 --- a/src/lib/blake2b.ts +++ b/src/lib/blake2b.ts @@ -283,9 +283,12 @@ function hexSlice (buf: Uint8Array) { return str } -function toHex (n) { - if (n < 16) return '0' + n.toString(16) - return n.toString(16) +function toHex (n: number) { + if (typeof n !== 'number') + throw new TypeError(`expected number to convert to hex, received convert ${typeof n}`) + if (n < 0 || n > 255) + throw new RangeError(`expected byte value 0-255, received ${n}`) + return n.toString(16).padStart(2, '0') } var Proto = Blake2b @@ -305,15 +308,15 @@ export default function createHash (outlen: number, key?: Uint8Array, salt?: Uin throw new RangeError('key must be at most ' + KEYBYTES_MAX + ', was given ' + key.length) } if (salt != null) { - if(!(salt instanceof Uint8Array)) + 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 (personal != null) { - if(!(personal instanceof Uint8Array)) - throw new TypeError( 'personal must be Uint8Array or Buffer') - if(personal.length !== PERSONALBYTES) + 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) } } -- 2.34.1