UNPKG

vtex

Version:

The platform for e-commerce apps

18 lines (17 loc) 622 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const crypto_1 = require("crypto"); function randomCryptoString(stringLength, alphabet) { if (alphabet.length > 256) { throw new Error("Argument 'alphabet' can't have more than 256 characters"); } const bytes = crypto_1.randomBytes(stringLength); const result = new Array(stringLength); let cursor = 0; for (let i = 0; i < stringLength; i++) { cursor += bytes[i]; result[i] = alphabet[cursor % alphabet.length]; } return result.join(''); } exports.randomCryptoString = randomCryptoString;