*
* @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
}
}
// 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}`
)\r
}\r
try {\r
- const output = new Blake2b(64, key).update(input).digest()\r
- const hex = [...output].map(b => b.toString(16).padStart(2, '0')).join('')\r
+ /** @type {string} */\r
+ const hex = new Blake2b(64, key).update(input).digest('hex')\r
assert.equal(hex, test.out)\r
} catch (err) {\r
console.error(`blake2b reference test vector ${i} failed`, { cause: err })\r
)\r
}\r
try {\r
- /** @type {Uint8Array<ArrayBuffer>} */\r
- const output = new Blake2b(test.outlen ?? 64, key, salt, personal).update(input).digest()\r
- const hex = [...output].map(b => b.toString(16).padStart(2, '0')).join('')\r
+ /** @type {string} */\r
+ const hex = new Blake2b(test.outlen ?? 64, key, salt, personal).update(input).digest('hex')\r
assert.equal(hex, test.out)\r
} catch (err) {\r
console.error(`blake2b libsodium test vector ${i} failed`, { cause: err })\r