diff options
| author | Xe Iaso <me@xeiaso.net> | 2025-03-25 17:02:48 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-25 17:02:48 -0400 |
| commit | 4155719422d416fb9af8cc6266697ebe16264538 (patch) | |
| tree | 5b788492a2478d7bbdc0d118d94b344b61e5fb77 /docs/src/components/RandomKey/index.tsx | |
| parent | f29a200f09ca3f720266164421304ed28de57dc6 (diff) | |
| download | anubis-4155719422d416fb9af8cc6266697ebe16264538.tar.xz anubis-4155719422d416fb9af8cc6266697ebe16264538.zip | |
cmd/anubis: allow setting key bytes in flag/envvar (#97)
* cmd/anubis: allow setting key bytes in flag/envvar
Docs are updated to generate a random key on load and when people press
the recycle button.
Signed-off-by: Xe Iaso <me@xeiaso.net>
* review feedback fixups
Signed-off-by: Xe Iaso <me@xeiaso.net>
* Update cmd/anubis/main.go
Signed-off-by: Xe Iaso <me@xeiaso.net>
* Apply suggestions from code review
Co-authored-by: Ryan Cao <70191398+ryanccn@users.noreply.github.com>
Signed-off-by: Xe Iaso <me@xeiaso.net>
---------
Signed-off-by: Xe Iaso <me@xeiaso.net>
Co-authored-by: Ryan Cao <70191398+ryanccn@users.noreply.github.com>
Diffstat (limited to 'docs/src/components/RandomKey/index.tsx')
| -rw-r--r-- | docs/src/components/RandomKey/index.tsx | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/docs/src/components/RandomKey/index.tsx b/docs/src/components/RandomKey/index.tsx new file mode 100644 index 0000000..e7ced3e --- /dev/null +++ b/docs/src/components/RandomKey/index.tsx @@ -0,0 +1,42 @@ +import { useState, useCallback } 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 genRandomKeyCb = useCallback(() => { + setKey(genRandomKey()); + }); + return ( + <span> + <Code>{key}</Code> + <span style={{ marginLeft: "0.25rem", marginRight: "0.25rem" }} /> + <button + onClick={() => { + genRandomKeyCb(); + }} + > + ♻️ + </button> + </span> + ); + }} + </BrowserOnly> + ); +} |
