🎉 feat: initialization

This commit is contained in:
web
2025-11-26 19:56:16 -08:00
commit a801849fb2
553 changed files with 213088 additions and 0 deletions
@@ -0,0 +1,4 @@
export { generateMLKEM768KeyPair } from "./mlkem768";
export { generateRealityShortId } from "./short-id";
export { generatePassword } from "./uid";
export { generateRealityKeyPair } from "./x25519";
@@ -0,0 +1,22 @@
import mlkem from "mlkem-wasm";
import { toB64Url } from "./util";
export async function generateMLKEM768KeyPair() {
const mlkemKeyPair = await mlkem.generateKey({ name: "ML-KEM-768" }, true, [
"encapsulateBits",
"decapsulateBits",
]);
const mlkemPublicKeyRaw = await mlkem.exportKey(
"raw-public",
mlkemKeyPair.publicKey
);
const mlkemPrivateKeyRaw = await mlkem.exportKey(
"raw-seed",
mlkemKeyPair.privateKey
);
return {
publicKey: toB64Url(new Uint8Array(mlkemPublicKeyRaw)),
privateKey: toB64Url(new Uint8Array(mlkemPrivateKeyRaw)),
};
}
@@ -0,0 +1,15 @@
/**
* Generate a short ID for Reality
* @returns A random hexadecimal string of length 2, 4, 6, 8, 10, 12, 14, or 16
*/
export function generateRealityShortId() {
const hex = "0123456789abcdef";
const lengths = [2, 4, 6, 8, 10, 12, 14, 16];
const idx = Math.floor(Math.random() * lengths.length);
const len = lengths[idx] ?? 16;
let out = "";
for (let i = 0; i < len; i++) {
out += hex.charAt(Math.floor(Math.random() * hex.length));
}
return out;
}
@@ -0,0 +1,11 @@
import { uid } from "radash";
/**
* Generate a random password
* @param length Length of the password
* @param charset Character set to use (defaults to alphanumeric)
* @returns Randomly generated password
*/
export function generatePassword(length = 16, charset?: string) {
return uid(length, charset).toLowerCase();
}
@@ -0,0 +1,6 @@
export function toB64Url(bytes: Uint8Array) {
return btoa(String.fromCharCode(...bytes))
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/g, "");
}
@@ -0,0 +1,11 @@
import { x25519 } from "@noble/curves/ed25519.js";
import { toB64Url } from "./util";
/**
* Generate a Reality key pair
* @returns An object containing the private and public keys in base64url format
*/
export function generateRealityKeyPair() {
const { secretKey, publicKey } = x25519.keygen();
return { privateKey: toB64Url(secretKey), publicKey: toB64Url(publicKey) };
}