// SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>
// SPDX-License-Identifier: GPL-3.0-or-later
-
+//@ts-nocheck
'use strict'
/**
* TypeScript, and bundle into web workers.
* Original source commit: https://github.com/emilbayes/blake2b/blob/1f63e02e3f226642959506cdaa67c8819ff145cd/index.js
*/
-const BYTES_MIN = 16
-const BYTES_MAX = 64
-const KEYBYTES_MIN = 16
-const KEYBYTES_MAX = 64
-const SALTBYTES = 16
-const PERSONALBYTES = 16
+export default class Blake2b {
+ const BYTES_MIN = 16
+ const BYTES_MAX = 64
+ const KEYBYTES_MIN = 16
+ const KEYBYTES_MAX = 64
+ const SALTBYTES = 16
+ const PERSONALBYTES = 16
+
+ constructor (outlen: number, key?: Uint8Array, salt?: Uint8Array, personal?: Uint8Array, noAssert?: boolean) {
+ 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 (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 (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 (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}`)
+ }
+ }
+
+ return this.create(outlen, key, salt, personal)
+ }
+
+/**
+* Creates a BLAKE2b hashing context
+* Requires an output length between 1 and 64 bytes
+* Takes an optional Uint8Array key
+*/
+function create (this: typeof Blake2b, outlen: number, key?: Uint8Array, salt?: Uint8Array, personal?: Uint8Array) {
+ // zero out parameter_block before usage
+ parameter_block.fill(0)
+ // state, 'param block'
+
+ this.b = new Uint8Array(128)
+ this.h = new Uint32Array(16)
+ this.t = 0 // input count
+ this.c = 0 // pointer within buffer
+ this.outlen = outlen // output length in bytes
+
+ parameter_block[0] = outlen
+ if (key) parameter_block[1] = key.length
+ parameter_block[2] = 1 // fanout
+ parameter_block[3] = 1 // depth
+
+ if (salt) parameter_block.set(salt, 32)
+ if (personal) parameter_block.set(personal, 48)
+
+ // initialize hash state
+ for (var i = 0; i < 16; i++) {
+ this.h[i] = BLAKE2B_IV32[i] ^ B2B_GET32(parameter_block, i * 4)
+ }
+
+ // key the hash, if applicable
+ if (key) {
+ blake2bUpdate(this, key)
+ // at the end
+ this.c = 128
+ }
+ return new this
+}
/**
* 64-bit unsigned addition
* Sets v[a,a+1] += v[b,b+1]
* v should be a Uint32Array
*/
-function ADD64AA (v, a, b) {
+function ADD64AA (v: Uint32Array, a, b) {
var o0 = v[a] + v[b]
var o1 = v[a + 1] + v[b + 1]
if (o0 >= 0x100000000) {
0, 0, 0, 0 // 60: personal
])
-/**
-* Creates a BLAKE2b hashing context
-* Requires an output length between 1 and 64 bytes
-* Takes an optional Uint8Array key
-*/
-function Blake2b (outlen: number, key?: Uint8Array, salt?: Uint8Array, personal?: Uint8Array) {
- // zero out parameter_block before usage
- parameter_block.fill(0)
- // state, 'param block'
-
- this.b = new Uint8Array(128)
- this.h = new Uint32Array(16)
- this.t = 0 // input count
- this.c = 0 // pointer within buffer
- this.outlen = outlen // output length in bytes
-
- parameter_block[0] = outlen
- if (key) parameter_block[1] = key.length
- parameter_block[2] = 1 // fanout
- parameter_block[3] = 1 // depth
-
- if (salt) parameter_block.set(salt, 32)
- if (personal) parameter_block.set(personal, 48)
-
- // initialize hash state
- for (var i = 0; i < 16; i++) {
- this.h[i] = BLAKE2B_IV32[i] ^ B2B_GET32(parameter_block, i * 4)
- }
-
- // key the hash, if applicable
- if (key) {
- blake2bUpdate(this, key)
- // at the end
- this.c = 128
- }
-}
-
-Blake2b.prototype.update = function (input: Uint8Array) {
+static function update (input: Uint8Array) {
if (!(input instanceof Uint8Array))
throw new TypeError(`input must be Uint8Array or Buffer`)
blake2bUpdate(this, input)
throw new RangeError(`expected byte value 0-255, received ${n}`)
return n.toString(16).padStart(2, '0')
}
-
-export default function createHash (outlen: number, key?: Uint8Array, salt?: Uint8Array, personal?: Uint8Array, noAssert?: boolean) {
- 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 (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 (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 (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}`)
- }
- }
-
- return new Blake2b(outlen, key, salt, personal)
}