* Generates a cryptographically secure random hex string of given length * * @param {number} bytesCount * @returns {Promise } Deterministic hex string generated from the seed
(bytesCount)
| 93 | * @returns {Promise<string>} Deterministic hex string generated from the seed |
| 94 | */ |
| 95 | async function randomHex(bytesCount) { |
| 96 | if (crypto.getRandomValues) { |
| 97 | const bytes = new Uint8Array(bytesCount); |
| 98 | crypto.getRandomValues(bytes); |
| 99 | |
| 100 | return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(""); |
| 101 | } |
| 102 | |
| 103 | return new Promise((resolve, reject) => { |
| 104 | crypto.randomBytes(bytesCount, (err, buf) => { |
| 105 | if (err) return reject(err); |
| 106 | resolve(buf.toString("hex")); |
| 107 | }); |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Calculates a SHA256 hash of a given string |
no outgoing calls
no test coverage detected