From 5149edfa13b06564b713ef34bd63faefaf77c6f2 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 2 Dec 2024 11:46:52 -0800 Subject: [PATCH] Add return typings to blake2b functions. --- src/lib/blake2b.ts | 53 ++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/src/lib/blake2b.ts b/src/lib/blake2b.ts index e6073b3..cc960c1 100644 --- a/src/lib/blake2b.ts +++ b/src/lib/blake2b.ts @@ -12,22 +12,22 @@ */ export class Blake2b { - static BYTES_MIN = 1 - static BYTES_MAX = 64 - static KEYBYTES_MIN = 16 - static KEYBYTES_MAX = 64 - static SALTBYTES = 16 - static PERSONALBYTES = 16 + static BYTES_MIN: number = 1 + static BYTES_MAX: number = 64 + static KEYBYTES_MIN: number = 16 + static KEYBYTES_MAX: number = 64 + static SALTBYTES: number = 16 + static PERSONALBYTES: number = 16 // Initialization Vector - static BLAKE2B_IV32 = new Uint32Array([ + static BLAKE2B_IV32: Uint32Array = new Uint32Array([ 0xF3BCC908, 0x6A09E667, 0x84CAA73B, 0xBB67AE85, 0xFE94F82B, 0x3C6EF372, 0x5F1D36F1, 0xA54FF53A, 0xADE682D1, 0x510E527F, 0x2B3E6C1F, 0x9B05688C, 0xFB41BD6B, 0x1F83D9AB, 0x137E2179, 0x5BE0CD19 ]) - static SIGMA8 = [ + static SIGMA8: number[] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3, 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4, @@ -47,10 +47,10 @@ export class Blake2b { * Multiply them all by 2 to make them offsets into a uint32 buffer, * because this is Javascript and we don't have uint64s */ - static SIGMA82 = new Uint8Array(this.SIGMA8.map(x => x * 2)) + static SIGMA82: Uint8Array = new Uint8Array(this.SIGMA8.map(x => x * 2)) // reusable parameter_block - static parameter_block = new Uint8Array([ + static parameter_block: Uint8Array = new Uint8Array([ 0, 0, 0, 0, // 0: outlen, keylen, fanout, depth 0, 0, 0, 0, // 4: leaf length, sequential mode 0, 0, 0, 0, // 8: node offset @@ -70,15 +70,15 @@ export class Blake2b { ]) - static v = new Uint32Array(32) - static m = new Uint32Array(32) + static v: Uint32Array = new Uint32Array(32) + static m: Uint32Array = new Uint32Array(32) /** * 64-bit unsigned addition * Sets v[a,a+1] += v[b,b+1] * v should be a Uint32Array */ - static ADD64AA (v: Uint32Array, a: number, b: number) { + static ADD64AA (v: Uint32Array, a: number, b: number): void { var o0 = v[a] + v[b] var o1 = v[a + 1] + v[b + 1] if (o0 >= 0x100000000) { @@ -93,7 +93,7 @@ export class Blake2b { * Sets v[a,a+1] += b * b0 is the low 32 bits of b, b1 represents the high 32 bits */ - static ADD64AC (v: Uint32Array, a: number, b0: number, b1: number) { + static ADD64AC (v: Uint32Array, a: number, b0: number, b1: number): void { var o0 = v[a] + b0 if (b0 < 0) { o0 += 0x100000000 @@ -107,7 +107,7 @@ export class Blake2b { } // Little-endian byte access - static B2B_GET32 (arr: Uint8Array, i: number) { + static B2B_GET32 (arr: Uint8Array, i: number): number { return (arr[i] ^ (arr[i + 1] << 8) ^ (arr[i + 2] << 16) ^ @@ -118,7 +118,7 @@ export class Blake2b { * G Mixing function * The ROTRs are inlined for speed */ - static B2B_G (a: number, b: number, c: number, d: number, ix: number, iy: number) { + static B2B_G (a: number, b: number, c: number, d: number, ix: number, iy: number): void { var x0 = Blake2b.m[ix] var x1 = Blake2b.m[ix + 1] var y0 = Blake2b.m[iy] @@ -163,7 +163,7 @@ export class Blake2b { * Compression function. 'last' flag indicates last block. * Note we're representing 16 uint64s as 32 uint32s */ - static blake2bCompress (ctx: any, last: boolean) { + static blake2bCompress (ctx: any, last: boolean): void { var i = 0 // init work variables @@ -209,7 +209,7 @@ export class Blake2b { * Updates a BLAKE2b streaming hash * Requires hash context and Uint8Array (byte array) */ - static blake2bUpdate (ctx: any, input: Uint8Array) { + static blake2bUpdate (ctx: any, input: Uint8Array): void { for (var i = 0; i < input.length; i++) { if (ctx.c === 128) { // buffer full ? ctx.t += ctx.c // add counters @@ -224,7 +224,7 @@ export class Blake2b { * Completes a BLAKE2b streaming hash * Returns a Uint8Array containing the message digest */ - static blake2bFinal (ctx: any, out: Uint8Array) { + static blake2bFinal (ctx: any, out: Uint8Array): Uint8Array { ctx.t += ctx.c // mark last block offset while (ctx.c < 128) { // fill up with zeros @@ -238,13 +238,13 @@ export class Blake2b { return out } - static hexSlice (buf: Uint8Array) { - var str = '' - for (var i = 0; i < buf.length; i++) str += Blake2b.toHex(buf[i]) + static hexSlice (buf: Uint8Array): string { + let str = '' + for (let i = 0; i < buf.length; i++) str += Blake2b.toHex(buf[i]) return str } - static toHex (n: number) { + static toHex (n: number): string { if (typeof n !== 'number') throw new TypeError(`expected number to convert to hex; received ${typeof n}`) if (n < 0 || n > 255) @@ -313,15 +313,18 @@ export class Blake2b { } } - update (input: Uint8Array) { + update (input: Uint8Array): Blake2b { if (!(input instanceof Uint8Array)) throw new TypeError(`input must be Uint8Array or Buffer`) Blake2b.blake2bUpdate(this, input) return this } + digest (): Uint8Array + digest (out: 'hex'): string + digest (out: 'binary' | Uint8Array): Uint8Array digest (out?: 'binary' | 'hex' | Uint8Array): string | Uint8Array { - var buf = (!out || out === 'binary' || out === 'hex') ? new Uint8Array(this.outlen) : out + const buf = (!out || out === 'binary' || out === 'hex') ? new Uint8Array(this.outlen) : out if (!(buf instanceof Uint8Array)) throw new TypeError(`out must be "binary", "hex", Uint8Array, or Buffer`) if (buf.length < this.outlen) throw new RangeError(`out must have at least outlen bytes of space`) Blake2b.blake2bFinal(this, buf) -- 2.34.1