| 23 | const defaultDict = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; |
| 24 | |
| 25 | export function secureRandomString(digit = 32, dict = defaultDict) { |
| 26 | let result = ''; |
| 27 | const crypto = window.crypto || (window as any).msCrypto; |
| 28 | if (!crypto?.getRandomValues) throw new Error('crypto.getRandomValues not supported'); |
| 29 | const array = new Uint32Array(digit); |
| 30 | crypto.getRandomValues(array); |
| 31 | for (let i = 0; i < digit; i++) result += dict[array[i] % dict.length]; |
| 32 | return result; |
| 33 | } |
| 34 | |
| 35 | type Substitution = string | number | { templateRaw: true, html: string }; |
| 36 | |