From: Chris Duncan Date: Mon, 4 Aug 2025 17:30:04 +0000 (-0700) Subject: Privatize entropy constants. X-Git-Tag: v0.10.5~46^2~15 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=65f3c78388f77e351182c130e0a53e46507d469b;p=libnemo.git Privatize entropy constants. --- diff --git a/src/lib/entropy.ts b/src/lib/entropy.ts index bbc8d19..470b4e7 100644 --- a/src/lib/entropy.ts +++ b/src/lib/entropy.ts @@ -13,9 +13,9 @@ import { bytes, hex } from './convert' * brand new source of entropy will be generated at the maximum size of 256 bits. */ export class Entropy { - static MIN: 16 = 16 - static MAX: 32 = 32 - static MOD: 4 = 4 + static #MIN: 16 = 16 + static #MAX: 32 = 32 + static #MOD: 4 = 4 #bytes: Uint8Array get bits (): string { return bytes.toBin(this.#bytes) } @@ -50,32 +50,32 @@ export class Entropy { constructor (input?: unknown) { if (input == null) { - this.#bytes = crypto.getRandomValues(new Uint8Array(Entropy.MAX)) + this.#bytes = crypto.getRandomValues(new Uint8Array(Entropy.#MAX)) } else if (typeof input === 'number') { - if (input < Entropy.MIN || input > Entropy.MAX) { - throw new RangeError(`Entropy must be ${Entropy.MIN}-${Entropy.MAX} bytes`) + if (input < Entropy.#MIN || input > Entropy.#MAX) { + throw new RangeError(`Entropy must be ${Entropy.#MIN}-${Entropy.#MAX} bytes`) } - if (input % Entropy.MOD !== 0) { - throw new RangeError(`Entropy must be a multiple of ${Entropy.MOD} bytes`) + if (input % Entropy.#MOD !== 0) { + throw new RangeError(`Entropy must be a multiple of ${Entropy.#MOD} bytes`) } this.#bytes = crypto.getRandomValues(new Uint8Array(input)) } else if (input instanceof ArrayBuffer || input instanceof Uint8Array) { - if (input.byteLength < Entropy.MIN || input.byteLength > Entropy.MAX) { - throw new Error(`Entropy must be ${Entropy.MIN}-${Entropy.MAX} bytes`) + if (input.byteLength < Entropy.#MIN || input.byteLength > Entropy.#MAX) { + throw new Error(`Entropy must be ${Entropy.#MIN}-${Entropy.#MAX} bytes`) } - if (input.byteLength % Entropy.MOD !== 0) { - throw new RangeError(`Entropy must be a multiple of ${Entropy.MOD} bytes`) + if (input.byteLength % Entropy.#MOD !== 0) { + throw new RangeError(`Entropy must be a multiple of ${Entropy.#MOD} bytes`) } this.#bytes = new Uint8Array(input) } else if (typeof input === 'string') { - if (input.length < Entropy.MIN * 2 || input.length > Entropy.MAX * 2) { - throw new RangeError(`Entropy must be ${Entropy.MIN * 2}-${Entropy.MAX * 2} characters`) + if (input.length < Entropy.#MIN * 2 || input.length > Entropy.#MAX * 2) { + throw new RangeError(`Entropy must be ${Entropy.#MIN * 2}-${Entropy.#MAX * 2} characters`) } - if (input.length % Entropy.MOD * 2 !== 0) { - throw new RangeError(`Entropy must be a multiple of ${Entropy.MOD * 2} characters`) + if (input.length % Entropy.#MOD * 2 !== 0) { + throw new RangeError(`Entropy must be a multiple of ${Entropy.#MOD * 2} characters`) } if (!/^[0-9a-fA-F]+$/i.test(input)) { throw new RangeError('Entropy contains invalid hexadecimal characters')