]> git.codecow.com Git - libnemo.git/commitdiff
Extract blake initialization to separate private method.
authorChris Duncan <chris@codecow.com>
Wed, 1 Jul 2026 05:45:29 +0000 (22:45 -0700)
committerChris Duncan <chris@codecow.com>
Wed, 1 Jul 2026 05:45:29 +0000 (22:45 -0700)
src/lib/crypto/blake2b.ts

index d4526ace80a4e78d079f9a7929236ed611b8b111..d4071991db95d1a0f308126ae3ebbf0b0c88ed6b 100644 (file)
@@ -102,6 +102,32 @@ export class Blake2b {
                this.#G(r, 7, 3, 4, 9, 14)
        }
 
+       #blake2bInit (length: number, key?: Bytes, salt?: Bytes, personal?: Bytes): void {
+               // output length in bytes
+               this.#outlen = length
+
+               // state, 'param block'
+               this.#parameter_block[0] = length
+               this.#parameter_block[1] = key?.length ?? 0
+               this.#parameter_block[2] = 1 // fanout
+               this.#parameter_block[3] = 1 // depth
+
+               if (salt) this.#parameter_block.set(salt, 32)
+               if (personal) this.#parameter_block.set(personal, 48)
+
+               // initialize hash state
+               for (let i = 0; i < 8; i++) {
+                       this.#h[i] = Blake2b.IV[i] ^ this.#parameter_view.getBigUint64(i << 3, true)
+               }
+
+               // key the hash, if applicable
+               if (key) {
+                       this.#blake2bUpdate(key)
+                       // force input buffer to reset for first block of actual message
+                       this.#c = 128
+               }
+       }
+
        /**
         * Compression function called during three phases: on keying (if applicable),
         * each time the input buffer is filled, and when finalizing message block.
@@ -169,7 +195,7 @@ export class Blake2b {
                this.#b.fill(0, this.#c) // pad final block with zeros
                this.#blake2bCompress(true) // set final block flag and compress
                const data = new DataView(this.#h.buffer)
-               for (let i = 0; i < this.#outlen; i++) {
+               for (let i = 0; i < out.length; i++) {
                        out[i] = data.getUint8(i)
                }
        }
@@ -206,7 +232,7 @@ export class Blake2b {
        /** Message block */
        #m: BigUint64Array = new BigUint64Array(16)
        /** User-requested output length */
-       #outlen: number
+       #outlen: number = 64
 
        /**
         * Creates a BLAKE2b hashing context.
@@ -252,29 +278,7 @@ export class Blake2b {
                                throw new RangeError(`personal must be ${B.PERSONALBYTES} bytes`)
                        }
                }
-
-               this.#outlen = length // output length in bytes
-
-               // state, 'param block'
-               this.#parameter_block[0] = length
-               this.#parameter_block[1] = key?.length ?? 0
-               this.#parameter_block[2] = 1 // fanout
-               this.#parameter_block[3] = 1 // depth
-
-               if (salt) this.#parameter_block.set(salt, 32)
-               if (personal) this.#parameter_block.set(personal, 48)
-
-               // initialize hash state
-               for (let i = 0; i < 8; i++) {
-                       this.#h[i] = B.IV[i] ^ this.#parameter_view.getBigUint64(i << 3, true)
-               }
-
-               // key the hash, if applicable
-               if (key) {
-                       this.#blake2bUpdate(key)
-                       // force input buffer to reset for first block of actual message
-                       this.#c = 128
-               }
+               this.#blake2bInit(length, key, salt, personal)
        }
 
        /**