"use client"; import React, { useState } from "react"; import bcrypt from "bcryptjs"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Lock, CheckCircle, XCircle, Copy, Hash } from "lucide-react"; import { toast } from "sonner"; import { Slider } from "@/components/ui/slider"; export default function BcryptPage() { const [plainText, setPlainText] = useState(""); const [saltRounds, setSaltRounds] = useState(10); const [hashResult, setHashResult] = useState(""); const [verifyText, setVerifyText] = useState(""); const [verifyHash, setVerifyHash] = useState(""); const [isMatch, setIsMatch] = useState(null); const handleGenerate = async () => { if (!plainText) return; try { const hash = await bcrypt.hash(plainText, saltRounds); setHashResult(hash); toast.success("哈希生成成功"); } catch { toast.error("生成失败"); } }; const handleVerify = async () => { if (!verifyText || !verifyHash) return; try { const match = await bcrypt.compare(verifyText, verifyHash); setIsMatch(match); if (match) { toast.success("验证成功:匹配"); } else { toast.error("验证失败:不匹配"); } } catch { setIsMatch(false); toast.error("验证过程出错,请检查哈希格式"); } }; const copyToClipboard = async (text: string) => { try { await navigator.clipboard.writeText(text); toast.success("已复制到剪贴板"); } catch { toast.error("复制失败"); } }; return (

Bcrypt 哈希生成与验证

生成安全的 Bcrypt 密码哈希或验证明文与哈希是否匹配

{/* Generate Card */} 生成哈希
setPlainText(e.target.value)} placeholder="请输入要加密的文本" />
越高越慢
setSaltRounds(v[0])} min={4} max={16} step={1} />
{hashResult && (
{hashResult}
)}
{/* Verify Card */} 验证哈希
setVerifyText(e.target.value)} placeholder="请输入明文" />
setVerifyHash(e.target.value)} placeholder="$2a$10$..." />
{isMatch !== null && (
{isMatch ? ( <> 验证通过:匹配 ) : ( <> 验证失败:不匹配 )}
)}
); }