migrated some routes to hono

This commit is contained in:
John Yeo
2026-01-15 16:23:20 +00:00
parent 105e4dad40
commit 6580ec3dac
50 changed files with 850 additions and 1141 deletions

View File

@@ -0,0 +1,48 @@
import * as crypto from "node:crypto";
/**
* Generates a cryptographically secure 6-digit OTP
*/
export const generateOtp = (): string => {
const getRandomInt = (): number => {
if (
typeof crypto !== "undefined" &&
typeof crypto.getRandomValues === "function"
) {
const array = new Uint32Array(1);
crypto.getRandomValues(array);
return array[0];
}
// Node.js (SSR / tests) – use crypto module's webcrypto if available
try {
const { webcrypto } = require("node:crypto");
if (webcrypto?.getRandomValues) {
const arr = new Uint32Array(1);
webcrypto.getRandomValues(arr);
return arr[0];
}
} catch (_) {
/* ignore */
}
// Fallback (non-cryptographic)
return Math.floor(Math.random() * 0xffffffff);
};
// Limit to range [100000, 999999]
const randomSixDigits = (getRandomInt() % 900000) + 100000;
return randomSixDigits.toString();
};
/**
* Generates a random hex key of the specified length
*/
export const generateRandomKey = (lengthInBytes: number = 32): string => {
if (lengthInBytes <= 0) {
throw new Error("Key length must be a positive number.");
}
return crypto.randomBytes(lengthInBytes).toString("hex");
};
export const OTP_TTL = 300;