aboutsummaryrefslogtreecommitdiff
path: root/src/node-fallbacks/crypto.js
blob: 0f428df132d23837a458037d76ea84a86e7e714f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
export * from "crypto-browserify";

// we deliberately reference crypto. directly here because we want to preserve the This binding
export const getRandomValues = (array) => {
  return crypto.getRandomValues(array);
};

export const randomUUID = () => {
  return crypto.randomUUID();
};

export const timingSafeEqual =
  "timingSafeEqual" in crypto
    ? (a, b) => {
        const { byteLength: byteLengthA } = a;
        const { byteLength: byteLengthB } = b;
        if (
          typeof byteLengthA !== "number" ||
          typeof byteLengthB !== "number"
        ) {
          throw new TypeError("Input must be an array buffer view");
        }

        if (byteLengthA !== byteLengthB) {
          throw new RangeError("Input buffers must have the same length");
        }

        return crypto.timingSafeEqual(a, b);
      }
    : undefined;

if (timingSafeEqual) {
  // hide it from stack trace
  Object.defineProperty(timingSafeEqual, "name", {
    value: "::bunternal::",
  });
}

export const webcrypto = crypto;