From: Chris Duncan Date: Tue, 26 Nov 2024 22:00:08 +0000 (-0800) Subject: Add type guards to function. Spaces. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=d2d83b0a66b046675d82505bb7521a2891049fe0;p=libnemo.git Add type guards to function. Spaces. --- 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) } }