(from, to)
| 57 | * @return {Number} - a secure random number |
| 58 | */ |
| 59 | export function getSecureRandom(from, to) { |
| 60 | let randUint = getSecureRandomUint(); |
| 61 | const bits = ((to - from)).toString(2).length; |
| 62 | while ((randUint & (Math.pow(2, bits) - 1)) > (to - from)) { |
| 63 | randUint = getSecureRandomUint(); |
| 64 | } |
| 65 | return from + (Math.abs(randUint & (Math.pow(2, bits) - 1))); |
| 66 | } |
| 67 | |
| 68 | function getSecureRandomUint() { |
| 69 | const buf = new Uint8Array(4); |
no test coverage detected