|
| 1 | +/** |
| 2 | + * @author cktgh [[email protected]] |
| 3 | + * @copyright Crown Copyright 2026 |
| 4 | + * @license Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import Operation from "../Operation.mjs"; |
| 8 | +import OperationError from "../errors/OperationError.mjs"; |
| 9 | +import forge from "node-forge"; |
| 10 | +import Utils, { isWorkerEnvironment } from "../Utils.mjs"; |
| 11 | +import { DELIM_OPTIONS } from "../lib/Delim.mjs"; |
| 12 | + |
| 13 | +/** |
| 14 | + * Pseudo-Random Integer Generator operation |
| 15 | + */ |
| 16 | +class PseudoRandomIntegerGenerator extends Operation { |
| 17 | + |
| 18 | + // in theory 2**53 is the max range, but we use Number.MAX_SAFE_INTEGER (2**53 - 1) as it is more consistent. |
| 19 | + static MAX_RANGE = Number.MAX_SAFE_INTEGER; |
| 20 | + // arbitrary choice |
| 21 | + static BUFFER_SIZE = 1024; |
| 22 | + |
| 23 | + /** |
| 24 | + * PseudoRandomIntegerGenerator constructor |
| 25 | + */ |
| 26 | + constructor() { |
| 27 | + super(); |
| 28 | + |
| 29 | + this.name = "Pseudo-Random Integer Generator"; |
| 30 | + this.module = "Ciphers"; |
| 31 | + this.description = "A cryptographically-secure pseudo-random number generator (PRNG).<br><br>Generates random integers within a specified range using the browser's built-in <code>crypto.getRandomValues()</code> method if available.<br><br>The supported range of integers is from <code>-(2^53 - 1)</code> to <code>(2^53 - 1)</code>."; |
| 32 | + this.infoURL = "https://wikipedia.org/wiki/Pseudorandom_number_generator"; |
| 33 | + this.inputType = "string"; |
| 34 | + this.outputType = "string"; |
| 35 | + this.args = [ |
| 36 | + { |
| 37 | + "name": "Number of Integers", |
| 38 | + "type": "number", |
| 39 | + "value": 1, |
| 40 | + "min": 1 |
| 41 | + }, |
| 42 | + { |
| 43 | + "name": "Min Value", |
| 44 | + "type": "number", |
| 45 | + "value": 0, |
| 46 | + "min": Number.MIN_SAFE_INTEGER, |
| 47 | + "max": Number.MAX_SAFE_INTEGER |
| 48 | + }, |
| 49 | + { |
| 50 | + "name": "Max Value", |
| 51 | + "type": "number", |
| 52 | + "value": 99, |
| 53 | + "min": Number.MIN_SAFE_INTEGER, |
| 54 | + "max": Number.MAX_SAFE_INTEGER |
| 55 | + }, |
| 56 | + { |
| 57 | + "name": "Delimiter", |
| 58 | + "type": "option", |
| 59 | + "value": DELIM_OPTIONS |
| 60 | + } |
| 61 | + ]; |
| 62 | + |
| 63 | + // not using BigUint64Array to avoid BigInt handling overhead |
| 64 | + this.randomBuffer = new Uint32Array(PseudoRandomIntegerGenerator.BUFFER_SIZE); |
| 65 | + this.randomBufferOffset = PseudoRandomIntegerGenerator.BUFFER_SIZE; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param {string} input |
| 70 | + * @param {Object[]} args |
| 71 | + * @returns {string} |
| 72 | + */ |
| 73 | + run(input, args) { |
| 74 | + const [numInts, minInt, maxInt, delimiter] = args; |
| 75 | + |
| 76 | + if (minInt === null || maxInt === null) return ""; |
| 77 | + |
| 78 | + const min = Math.ceil(minInt); |
| 79 | + const max = Math.floor(maxInt); |
| 80 | + const delim = Utils.charRep(delimiter || "Space"); |
| 81 | + |
| 82 | + if (!Number.isSafeInteger(min) || !Number.isSafeInteger(max)) { |
| 83 | + throw new OperationError("Min and Max must be between `-(2^53 - 1)` and `2^53 - 1`."); |
| 84 | + } |
| 85 | + if (min > max) { |
| 86 | + throw new OperationError("Min cannot be larger than Max."); |
| 87 | + } |
| 88 | + const range = max - min + 1; // inclusive range |
| 89 | + if (range > PseudoRandomIntegerGenerator.MAX_RANGE) { |
| 90 | + throw new OperationError("Range between Min and Max cannot be larger than `2^53`"); |
| 91 | + } |
| 92 | + |
| 93 | + // as large as possible while divisible by range |
| 94 | + const rejectionThreshold = PseudoRandomIntegerGenerator.MAX_RANGE - (PseudoRandomIntegerGenerator.MAX_RANGE % range); |
| 95 | + const output = []; |
| 96 | + for (let i = 0; i < numInts; i++) { |
| 97 | + const result = this._generateRandomValue(rejectionThreshold); |
| 98 | + const intValue = min + (result % range); |
| 99 | + output.push(intValue.toString()); |
| 100 | + } |
| 101 | + |
| 102 | + return output.join(delim); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Generate a random value, result will be less than the rejection threshold (exclusive). |
| 107 | + * |
| 108 | + * @param {number} rejectionThreshold |
| 109 | + * @returns {number} |
| 110 | + */ |
| 111 | + _generateRandomValue(rejectionThreshold) { |
| 112 | + let result; |
| 113 | + do { |
| 114 | + if (this.randomBufferOffset + 2 > this.randomBuffer.length) { |
| 115 | + this._resetRandomBuffer(); |
| 116 | + } |
| 117 | + // stitching a 53 bit number; not using BigUint64Array to avoid BigInt handling overhead |
| 118 | + result = (this.randomBuffer[this.randomBufferOffset++] & 0x1f_ffff) * 0x1_0000_0000 + |
| 119 | + this.randomBuffer[this.randomBufferOffset++]; |
| 120 | + } while (result >= rejectionThreshold); |
| 121 | + |
| 122 | + return result; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Fill random buffer with new random values and rseet the offset. |
| 127 | + */ |
| 128 | + _resetRandomBuffer() { |
| 129 | + if (isWorkerEnvironment() && self.crypto) { |
| 130 | + self.crypto.getRandomValues(this.randomBuffer); |
| 131 | + } else { |
| 132 | + const bytes = forge.random.getBytesSync(this.randomBuffer.length * 4); |
| 133 | + for (let j = 0; j < this.randomBuffer.length; j++) { |
| 134 | + this.randomBuffer[j] = (bytes.charCodeAt(j * 4) << 24) | |
| 135 | + (bytes.charCodeAt(j * 4 + 1) << 16) | |
| 136 | + (bytes.charCodeAt(j * 4 + 2) << 8) | |
| 137 | + bytes.charCodeAt(j * 4 + 3); |
| 138 | + } |
| 139 | + } |
| 140 | + this.randomBufferOffset = 0; |
| 141 | + } |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +export default PseudoRandomIntegerGenerator; |
0 commit comments