n-digit-token
Version:
Cryptographically secure pseudo-random token of n digits
15 lines (14 loc) • 479 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateMax = void 0;
/**
* Calculate largest possible value that avoids modulo bias.
* @param {number} byteSize
* @param {number} length
* @return {bigint} max
*/
const calculateMax = ({ byteSize, length, }) => {
const maxDecimal = BigInt(2n ** (BigInt(byteSize) * 8n) - 1n);
return maxDecimal - (maxDecimal % 10n ** BigInt(length)) - 1n;
};
exports.calculateMax = calculateMax;