|
| 1 | +import Layout from "@theme/Layout"; |
| 2 | +import { ReactNode, useEffect, useState } from "react"; |
| 3 | +import { ToastContainer } from "../components/Toast"; |
| 4 | + |
| 5 | +const Page = (): ReactNode => { |
| 6 | + const [uid, setUID] = useState<string>(""); |
| 7 | + |
| 8 | + useEffect(() => { |
| 9 | + const rnd = randomBytes(8); |
| 10 | + |
| 11 | + function toHex(number: number): string { |
| 12 | + return ("00" + number.toString(16)).slice(-2).toUpperCase(); |
| 13 | + } |
| 14 | + |
| 15 | + const serialised = `0x${toHex(rnd[0])}${toHex(rnd[1])}${toHex( |
| 16 | + rnd[2] |
| 17 | + )}${toHex(rnd[3])}${toHex(rnd[4])}${toHex(rnd[5])}${toHex(rnd[6])}${toHex( |
| 18 | + rnd[7] |
| 19 | + )}`; |
| 20 | + |
| 21 | + setUID(serialised); |
| 22 | + }, []); |
| 23 | + |
| 24 | + return ( |
| 25 | + <div> |
| 26 | + <Layout |
| 27 | + title={`UID Generator`} |
| 28 | + description="Generate UID to use for IComponent or IExtension unique identifiers" |
| 29 | + > |
| 30 | + <section className="servers-container"> |
| 31 | + <h1>Component UID Generator</h1> |
| 32 | + Copy the <code>PROVIDE_UID</code> macro below in to your new |
| 33 | + component, in place of the default UID provider macro. Each component |
| 34 | + should have a unique UID, hence the <em>U</em> in <em>UID</em> ( |
| 35 | + <em>Unique IDentifier</em>). The default <code>PROVIDE_UID</code> is |
| 36 | + invalid and will not compile, to avoid duplicates when creating new |
| 37 | + components from templates. |
| 38 | + <br /> |
| 39 | + <br /> |
| 40 | + Find this placeholder: |
| 41 | + <pre>PROVIDE_UID(/* UID GOES HERE */);</pre> |
| 42 | + <br /> |
| 43 | + And replace it with: |
| 44 | + <pre>{`PROVIDE_UID(${uid});`}</pre> |
| 45 | + <br /> |
| 46 | + If you are modifying an existing component still do remember to |
| 47 | + replace the existing UID, which will be a valid value not a |
| 48 | + placeholder. |
| 49 | + </section> |
| 50 | + </Layout> |
| 51 | + <ToastContainer /> |
| 52 | + </div> |
| 53 | + ); |
| 54 | +}; |
| 55 | + |
| 56 | +export default Page; |
| 57 | + |
| 58 | +function randomBytes(size: number): Uint8Array { |
| 59 | + // Input validation |
| 60 | + if (!Number.isInteger(size) || size < 0) { |
| 61 | + throw new TypeError("Size must be a non-negative integer"); |
| 62 | + } |
| 63 | + |
| 64 | + // Use Web Crypto API which provides cryptographically secure random values |
| 65 | + if (typeof window !== "undefined" && window.crypto) { |
| 66 | + // Browser environment |
| 67 | + const buffer = new Uint8Array(size); |
| 68 | + window.crypto.getRandomValues(buffer); |
| 69 | + return buffer; |
| 70 | + } else if (typeof global !== "undefined" && global.crypto) { |
| 71 | + // Node.js environment with Web Crypto API |
| 72 | + const buffer = new Uint8Array(size); |
| 73 | + global.crypto.getRandomValues(buffer); |
| 74 | + return buffer; |
| 75 | + } else { |
| 76 | + // Fallback for environments without Web Crypto API |
| 77 | + const buffer = new Uint8Array(size); |
| 78 | + for (let i = 0; i < size; i++) { |
| 79 | + // Generate random bytes using Math.random() |
| 80 | + // Note: This is NOT cryptographically secure! |
| 81 | + buffer[i] = Math.floor(Math.random() * 256); |
| 82 | + } |
| 83 | + console.warn( |
| 84 | + "Warning: Using non-cryptographically secure random number generation" |
| 85 | + ); |
| 86 | + return buffer; |
| 87 | + } |
| 88 | +} |
0 commit comments