shuffrand
Version:
Cryptographically secure randomness and shuffling — with soul.
10 lines (9 loc) • 3.15 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("./types.cjs.js"),y=require("./random.cjs.js"),g=require("./shuffle.cjs.js"),s=require("./constants.cjs.js");/**
* shuffrand - Cryptographically Secure Random String Generation
*
* This file contains the core logic for generating cryptographically secure random strings,
* adhering to a flat, dot-categorized structure for clarity.
*
* @author Doron Brayer <doronbrayer@outlook.com>
* @license MIT
*/const u={alphanumeric:s.Constants.ALPHANUMERIC_CHARS,numeric:s.Constants.DIGITS,alpha:s.Constants.LATIN_LETTERS,hex:s.Constants.HEX_CHARS,uppercase:s.Constants.LATIN_UPPERCASE_LETTERS,lowercase:s.Constants.LATIN_LOWERCASE_LETTERS};function S(r={}){if(r===null)throw new TypeError("Invalid cryptoString parameters: 'rawParams' cannot be null. Please provide an object or omit it.");try{p.cryptoStringParamsSchema.assert(r)}catch(e){throw new TypeError(`Invalid cryptoString parameters: ${e.summary||e.message}`)}const t=r.length??16,a=r.characterSet??"alphanumeric",h=r.noRepeat??!1;if(t>1e6)throw new TypeError("Invalid cryptoString parameters: 'length' exceeds maximum safe limit of 1,000,000 characters.");let n;if(typeof a=="string"&&Object.prototype.hasOwnProperty.call(u,a))n=u[a];else{n=a;const e=Array.from(n);if(new Set(e).size!==e.length)throw new TypeError("Invalid cryptoString parameters: Custom character set contains duplicate characters, which would skew randomness distribution.")}if(n.length===0&&t>0)throw new TypeError("Invalid cryptoString parameters: The resolved 'characterSet' cannot be empty.");const i=Array.from(new Set(Array.from(n)));if(i.length<2&&t>1)throw new TypeError("Invalid cryptoString parameters: Character set must contain at least 2 unique characters to generate a string longer than 1.");if(h&&t>i.length)throw new TypeError(`Invalid cryptoString parameters: Cannot generate a string of length ${t} with no repeats from a character set with only ${i.length} unique characters.`);if(t===0)return"";if(h)return g.cryptoShuffle(i).slice(0,t).join("");{const e=new Array(t),o=Array.from(n),l=o.length;for(let c=0;c<t;c++){const f=y.cryptoRandom({lowerBound:0,upperBound:l-1,typeOfNum:"integer",exclusion:"none"});e[c]=o[f]}return e.join("")}}function m(r={}){try{p.cryptoStringParamsSchema.assert(r)}catch(o){throw new TypeError(`Invalid calculateStringEntropy parameters: ${o.summary||o.message}`)}const t=r.length??16,a=r.characterSet??"alphanumeric",h=r.noRepeat??!1;let n;typeof a=="string"&&Object.prototype.hasOwnProperty.call(u,a)?n=u[a]:n=a;const e=new Set(Array.from(n)).size;if(h){if(t>e)throw new TypeError(`Invalid calculateStringEntropy parameters: Cannot calculate entropy for a length of ${t} with no repeats from a character set with only ${e} unique characters.`);let o=0;for(let l=0;l<t;l++){const c=e-l;c>0&&(o+=Math.log2(c))}return o}if(e<2&&t>1)throw new TypeError("Invalid calculateStringEntropy parameters: Character set must contain at least 2 unique characters to calculate meaningful entropy.");return e===0?0:Math.log2(e)*t}exports.calculateStringEntropy=m;exports.cryptoString=S;