Name lists now live in settings instead of the web bundle. Admins edit them under Settings -> Names (add/remove names, create/rename/delete lists, reset a built-in list). Names are folded to hostname-safe form (a-z, 0-9, hyphen) and validated on the server; saves are audited. Adds Swedish boy names, Pixar, Norse mythology and Astrid Lindgren lists, and lengthens the Swedish girl and Disney lists. "Mixed" is now every list with each name counted once. Admins can preview and import the most common Swedish names from Skatteverket's open "Namn pa nyfodda" data (girls or boys, latest 1-5 full years); nothing is stored until the editor is saved. The old comment that credited SCB statistics is gone: SCB stopped publishing name statistics after 2023. Privacy page, README and ROLES updated for the new outbound call and page. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
112 lines
4.6 KiB
TypeScript
112 lines
4.6 KiB
TypeScript
/** Uniform random integer in [0, max) via rejection sampling — avoids the modulo bias a plain `% max` would introduce. */
|
|
function randomInt(max: number): number {
|
|
const array = new Uint32Array(1);
|
|
const limit = Math.floor(0x100000000 / max) * max;
|
|
let value: number;
|
|
do {
|
|
crypto.getRandomValues(array);
|
|
value = array[0];
|
|
} while (value >= limit);
|
|
return value % max;
|
|
}
|
|
|
|
function pick<T>(list: T[]): T {
|
|
return list[randomInt(list.length)];
|
|
}
|
|
|
|
// ─── Server names ───────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Picks a name from `pool` that isn't already in `existing` (case-insensitively). Only if the whole pool is taken does it
|
|
* fall back to numbering one — maja2, maja3, …. The pools themselves are the name lists under Settings → Names.
|
|
*/
|
|
export function generateServerName(pool: string[], existing: string[] = []): string {
|
|
if (pool.length === 0) return "";
|
|
const used = new Set(existing.map((n) => n.toLowerCase()));
|
|
const available = pool.filter((n) => !used.has(n.toLowerCase()));
|
|
if (available.length > 0) return pick(available);
|
|
const base = pick(pool);
|
|
for (let suffix = 2; suffix < 1000; suffix++) {
|
|
const candidate = `${base}${suffix}`;
|
|
if (!used.has(candidate.toLowerCase())) return candidate;
|
|
}
|
|
return `${base}${randomInt(100000)}`;
|
|
}
|
|
|
|
// ─── Usernames ───────────────────────────────────────────────────────────────
|
|
|
|
const USERNAME_ADJECTIVES = [
|
|
"clever", "quiet", "swift", "brave", "calm", "bright", "steady", "gentle", "bold", "keen",
|
|
"quick", "sharp", "lucky", "sunny", "cool", "wise", "mellow", "spry", "nimble", "vivid",
|
|
];
|
|
const USERNAME_NOUNS = [
|
|
"otter", "falcon", "lynx", "raven", "badger", "heron", "wolf", "sparrow", "fox", "hawk",
|
|
"marten", "puffin", "osprey", "harrier", "kestrel", "pike", "elk", "moose", "wren", "orca",
|
|
];
|
|
|
|
export interface UsernameOptions {
|
|
separator: "" | "-" | "_" | ".";
|
|
includeNumber: boolean;
|
|
}
|
|
|
|
export function generateUsername(options: UsernameOptions): string {
|
|
const parts = [pick(USERNAME_ADJECTIVES), pick(USERNAME_NOUNS)];
|
|
if (options.includeNumber) parts.push(String(randomInt(90) + 10));
|
|
return parts.join(options.separator);
|
|
}
|
|
|
|
// ─── Passwords ───────────────────────────────────────────────────────────────
|
|
|
|
const CHARSETS = {
|
|
uppercase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
lowercase: "abcdefghijklmnopqrstuvwxyz",
|
|
numbers: "0123456789",
|
|
symbols: "!@#$%^&*()-_=+[]{}",
|
|
};
|
|
// Characters that are easy to misread in most fonts (0/O, 1/l/I, etc).
|
|
const AMBIGUOUS = "0O1lI";
|
|
|
|
export interface PasswordOptions {
|
|
length: number;
|
|
uppercase: boolean;
|
|
lowercase: boolean;
|
|
numbers: boolean;
|
|
symbols: boolean;
|
|
excludeAmbiguous: boolean;
|
|
}
|
|
|
|
export function generatePassword(options: PasswordOptions): string {
|
|
let charset = "";
|
|
if (options.uppercase) charset += CHARSETS.uppercase;
|
|
if (options.lowercase) charset += CHARSETS.lowercase;
|
|
if (options.numbers) charset += CHARSETS.numbers;
|
|
if (options.symbols) charset += CHARSETS.symbols;
|
|
if (options.excludeAmbiguous) charset = [...charset].filter((c) => !AMBIGUOUS.includes(c)).join("");
|
|
if (charset.length === 0) return "";
|
|
|
|
let result = "";
|
|
for (let i = 0; i < options.length; i++) {
|
|
result += charset[randomInt(charset.length)];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/** log2(charsetSize^length) — a standard, if approximate (assumes uniform random selection, which generatePassword provides), entropy estimate. */
|
|
export function passwordEntropyBits(options: PasswordOptions): number {
|
|
let charsetSize = 0;
|
|
if (options.uppercase) charsetSize += CHARSETS.uppercase.length;
|
|
if (options.lowercase) charsetSize += CHARSETS.lowercase.length;
|
|
if (options.numbers) charsetSize += CHARSETS.numbers.length;
|
|
if (options.symbols) charsetSize += CHARSETS.symbols.length;
|
|
if (options.excludeAmbiguous) charsetSize = Math.max(0, charsetSize - AMBIGUOUS.length);
|
|
if (charsetSize === 0) return 0;
|
|
return Math.round(options.length * Math.log2(charsetSize));
|
|
}
|
|
|
|
export function passwordStrengthLabel(bits: number): { label: string; className: string } {
|
|
if (bits < 40) return { label: "Weak", className: "text-red" };
|
|
if (bits < 60) return { label: "Fair", className: "text-yellow" };
|
|
if (bits < 90) return { label: "Strong", className: "text-green" };
|
|
return { label: "Very strong", className: "text-green" };
|
|
}
|