(length: number)
| 77 | } |
| 78 | |
| 79 | function getRandomBytes(length: number): Uint8Array { |
| 80 | const bytes = new Uint8Array(length) |
| 81 | const cryptoObj = typeof globalThis !== "undefined" ? globalThis.crypto : undefined |
| 82 | |
| 83 | if (cryptoObj && typeof cryptoObj.getRandomValues === "function") { |
| 84 | cryptoObj.getRandomValues(bytes) |
| 85 | return bytes |
| 86 | } |
| 87 | |
| 88 | for (let i = 0; i < length; i += 1) { |
| 89 | bytes[i] = Math.floor(Math.random() * 256) |
| 90 | } |
| 91 | |
| 92 | return bytes |
| 93 | } |