From: Chris Duncan Date: Fri, 3 Jul 2026 07:25:57 +0000 (-0700) Subject: Deprecate remaining padStart usages. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=664a2a1f3db43254ce6388c3eeeef86b0ac86971;p=libnemo.git Deprecate remaining padStart usages. --- diff --git a/src/lib/crypto/blake2b.ts b/src/lib/crypto/blake2b.ts index 18bfca7..b7b3e67 100644 --- a/src/lib/crypto/blake2b.ts +++ b/src/lib/crypto/blake2b.ts @@ -248,9 +248,11 @@ export class Blake2b { * * @returns {Bytes} */ - digest (): Bytes { + digest (): Bytes + digest (format: 'hex'): string + digest (format?: 'hex'): Bytes | string { const buf = new Uint8Array(this.#outlen) this.#blake2bFinal(buf) - return buf + return (format === 'hex') ? bytes.toHex(buf) : buf } } diff --git a/src/lib/tools.ts b/src/lib/tools.ts index 52e1a9c..0f4f90b 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -89,7 +89,8 @@ export class Tools { // convert to desired denomination const outUnit = UNITS[outputUnit] - i = int.toString().padStart(40, '0') + i = int.toString() + while (i.length < 40) i = '0' + i f = i.slice(40 - outUnit).replace(/0*$/g, '') i = i.slice(0, 40 - outUnit).replace(/^0*/g, '') const output = `${i === '' ? '0' : i}${f === '' ? '' : '.'}${f}` diff --git a/test/test.blake2b.mjs b/test/test.blake2b.mjs index 0810381..87ab22f 100644 --- a/test/test.blake2b.mjs +++ b/test/test.blake2b.mjs @@ -31,8 +31,8 @@ await Promise.all([ ) } try { - const output = new Blake2b(64, key).update(input).digest() - const hex = [...output].map(b => b.toString(16).padStart(2, '0')).join('') + /** @type {string} */ + const hex = new Blake2b(64, key).update(input).digest('hex') assert.equal(hex, test.out) } catch (err) { console.error(`blake2b reference test vector ${i} failed`, { cause: err }) @@ -83,9 +83,8 @@ await Promise.all([ ) } try { - /** @type {Uint8Array} */ - const output = new Blake2b(test.outlen ?? 64, key, salt, personal).update(input).digest() - const hex = [...output].map(b => b.toString(16).padStart(2, '0')).join('') + /** @type {string} */ + const hex = new Blake2b(test.outlen ?? 64, key, salt, personal).update(input).digest('hex') assert.equal(hex, test.out) } catch (err) { console.error(`blake2b libsodium test vector ${i} failed`, { cause: err })