aboutsummaryrefslogtreecommitdiff
path: root/docs/src/components/RandomKey/index.tsx
blob: f1eb63a2d428d4ae4053b872582050e1c0d7cb4b (plain)
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
40
41
42
43
import { useState, useEffect } from "react";
import Code from "@theme/CodeInline";
import BrowserOnly from "@docusaurus/BrowserOnly";

// https://www.xaymar.com/articles/2020/12/08/fastest-uint8array-to-hex-string-conversion-in-javascript/
function toHex(buffer) {
  return Array.prototype.map
    .call(buffer, (x) => ("00" + x.toString(16)).slice(-2))
    .join("");
}

export const genRandomKey = (): String => {
  const array = new Uint8Array(32);
  self.crypto.getRandomValues(array);
  return toHex(array);
};

export default function RandomKey() {
  return (
    <BrowserOnly fallback={<div>Loading...</div>}>
      {() => {
        const [key, setKey] = useState<String>(genRandomKey());
        const [refresh, setRefresh] = useState<number>(0);
        useEffect(() => {
          setKey(genRandomKey());
        }, [refresh]);
        return (
          <span>
            <Code>{key}</Code>
            <span style={{ marginLeft: "0.25rem", marginRight: "0.25rem" }} />
            <button
              onClick={() => {
                setRefresh((n) => n + 1);
              }}
            >
              ♻️
            </button>
          </span>
        );
      }}
    </BrowserOnly>
  );
}