aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-util/src/prng-browser.ts
blob: 79d399e0338e3414276df1cc30e62282c8815e05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { setPRNG } from "./nacl-fast.js";

export function loadBrowserPrng() {
  // Initialize PRNG if environment provides CSPRNG.
  // If not, methods calling randombytes will throw.
  // @ts-ignore-error
  const cr = typeof self !== "undefined" ? self.crypto || self.msCrypto : null;

  const QUOTA = 65536;
  setPRNG(function (x: Uint8Array, n: number) {
    let i;
    const v = new Uint8Array(n);
    for (i = 0; i < n; i += QUOTA) {
      cr.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)));
    }
    for (i = 0; i < n; i++) x[i] = v[i];
    for (i = 0; i < v.length; i++) v[i] = 0;
  });
}