UNPKG

@lyra/form-builder

Version:
28 lines (21 loc) 762 B
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = randomKey; var _getRandomValues = require('get-random-values'); var _getRandomValues2 = _interopRequireDefault(_getRandomValues); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // WHATWG crypto RNG - https://w3c.github.io/webcrypto/Overview.html function whatwgRNG(length = 16) { const rnds8 = new Uint8Array(length); (0, _getRandomValues2.default)(rnds8); return rnds8; } const byteToHex = []; for (let i = 0; i < 256; ++i) { byteToHex[i] = (i + 0x100).toString(16).substring(1); } function randomKey(length) { return whatwgRNG(length).reduce((str, n) => str + byteToHex[n], '').slice(0, length); }