"use client"; import { useState, useCallback } from "react"; import { Settings, RefreshCw, Copy, Shield, Zap } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Checkbox } from "@/components/ui/checkbox"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { Separator } from "@/components/ui/separator"; import { Badge } from "@/components/ui/badge"; import { Textarea } from "@/components/ui/textarea"; import { toast } from "sonner"; interface StringConfig { length: number; includeUppercase: boolean; includeLowercase: boolean; includeNumbers: boolean; includeSymbols: boolean; excludeSimilar: boolean; customChars: string; batchCount: number; } const defaultConfig: StringConfig = { length: 16, includeUppercase: true, includeLowercase: true, includeNumbers: true, includeSymbols: false, excludeSimilar: false, customChars: "", batchCount: 1, }; const UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const LOWERCASE = "abcdefghijklmnopqrstuvwxyz"; const NUMBERS = "0123456789"; const SYMBOLS = "!@#$%^&*()_+-=[]{}|;:,.<>?"; const SIMILAR_CHARS = "0O1lI|"; export default function RandomStringGenerator() { const [config, setConfig] = useState(defaultConfig); const [generatedStrings, setGeneratedStrings] = useState([]); const [currentString, setCurrentString] = useState(""); const handleConfigChange = (field: keyof StringConfig, value: any) => { setConfig((prev) => ({ ...prev, [field]: value })); }; const getCharacterSet = useCallback(() => { let chars = ""; if (config.customChars) { chars = config.customChars; } else { if (config.includeUppercase) chars += UPPERCASE; if (config.includeLowercase) chars += LOWERCASE; if (config.includeNumbers) chars += NUMBERS; if (config.includeSymbols) chars += SYMBOLS; } if (config.excludeSimilar && !config.customChars) { chars = chars .split("") .filter((char) => !SIMILAR_CHARS.includes(char)) .join(""); } return chars; }, [config]); const generateRandomString = useCallback( (length: number, charset: string) => { if (!charset) return ""; let result = ""; const charactersLength = charset.length; for (let i = 0; i < length; i++) { result += charset.charAt(Math.floor(Math.random() * charactersLength)); } return result; }, [] ); const generateStrings = useCallback(() => { const charset = getCharacterSet(); if (!charset) { toast.error("请至少选择一种字符类型"); return; } const newStrings = []; for (let i = 0; i < config.batchCount; i++) { const randomStr = generateRandomString(config.length, charset); newStrings.push(randomStr); } setGeneratedStrings(newStrings); setCurrentString(newStrings[0] || ""); toast.success(`成功生成 ${newStrings.length} 个随机字符串`); }, [config, getCharacterSet, generateRandomString]); const copyToClipboard = (text: string) => { navigator.clipboard .writeText(text) .then(() => { toast.success("已复制到剪贴板"); }) .catch(() => { toast.error("复制失败"); }); }; const copyAllStrings = () => { const allStrings = generatedStrings.join("\n"); copyToClipboard(allStrings); }; const getStrengthInfo = () => { const charset = getCharacterSet(); const entropy = Math.log2(Math.pow(charset.length, config.length)); let strength = "弱"; let color = "bg-red-500 hover:bg-red-600"; if (entropy >= 60) { strength = "极强"; color = "bg-emerald-500 hover:bg-emerald-600"; } else if (entropy >= 40) { strength = "强"; color = "bg-cyan-500 hover:bg-cyan-600"; } else if (entropy >= 25) { strength = "中等"; color = "bg-amber-500 hover:bg-amber-600"; } return { strength, entropy: entropy.toFixed(1), color }; }; const strengthInfo = getStrengthInfo(); return (
{/* Page Header */}

随机字符串生成器

生成安全可靠的随机字符串,支持多种字符集配置

{/* Left: Configuration */}
基础设置
handleConfigChange("length", parseInt(e.target.value) || 1) } className="pr-8" />
handleConfigChange("batchCount", parseInt(e.target.value) || 1) } className="pr-8" />
字符集选择
handleConfigChange("includeUppercase", checked) } />
handleConfigChange("includeLowercase", checked) } />
handleConfigChange("includeNumbers", checked) } />
handleConfigChange("includeSymbols", checked) } />
handleConfigChange("excludeSimilar", checked) } />
handleConfigChange("customChars", e.target.value) } placeholder="输入自定义字符集(将覆盖上述选择)" />
{/* Right: Generate & Result */}
安全强度
字符集大小
{getCharacterSet().length}
安全熵
{strengthInfo.entropy}
强度等级
{strengthInfo.strength}
生成控制 {generatedStrings.length > 0 && (
)}
{generatedStrings.length > 0 && ( 生成结果
{config.batchCount === 1 ? (
) : (