有时候需要手动替换MySQL中的密码值,根据自己的需要,使用AI写了一个代码,这是一个基于 PHP + Tailwind CSS 构建的在线 BCrypt 密码加密生成工具。
主要作用包括:
安全哈希加密:调用 PHP 原生的 password_hash($password, PASSWORD_BCRYPT) 函数,将输入的明文密码转换为单向强加密的 BCrypt 哈希密文(以 $2y$ 开头)。
自定义加密强度 (Cost):支持选择加密成本(cost,范围在 10~12,内部限制为 4~31)。数值越高,计算迭代次数越多,防暴力破解能力越强。
哈希双重校验:使用 password_verify() 在生成哈希后再次校验,确保生成的密文合法有效。
URL API/快捷调用:支持通过 GET 请求参数(如 ?p=明文密码&c=10)直接生成结果,方便快捷测试或嵌入其他流程,另外新增两种格式,一个是raw原形一个是json格式方便调用。
用户友好界面:内置简单的 JS 复制功能(一键复制哈希值)、输入框自动聚焦,以及使用 Tailwind CSS 打造的响应式视觉界面。
直接将下面的代码保存在php文件中,并在浏览器里访问它即可。代码如下:
<?php
// 设置默认值
$password = '';
$cost = 10;
$hashedPassword = '';
$error = '';
$type = isset($_GET['t']) ? strtolower(trim($_GET['t'])) : 'html';
// 从GET参数获取值
if (isset($_GET['p'])) {
$password = trim($_GET['p']);
}
if (isset($_GET['c']) && is_numeric($_GET['c'])) {
$cost = (int)$_GET['c'];
// 限制cost值在4-31之间,这是BCrypt的有效范围
$cost = max(4, min(31, $cost));
}
// 如果提供了密码,则进行加密
if (!empty($password)) {
try {
// 生成BCrypt哈希
$hashedPassword = password_hash($password, PASSWORD_BCRYPT, ['cost' => $cost]);
// 验证加密结果
if (!password_verify($password, $hashedPassword)) {
$error = "加密过程出现问题,验证失败";
$hashedPassword = '';
}
} catch (Exception $e) {
$error = "加密失败: " . $e->getMessage();
}
}
// ================= API 输出拦截逻辑 =================
// 1. RAW 纯文本输出模式 (?t=raw)
if ($type === 'raw') {
header('Content-Type: text/plain; charset=utf-8');
if (!empty($error)) {
http_response_code(400);
echo "ERROR: " . $error;
} else {
echo $hashedPassword;
}
exit;
}
// 2. JSON 数据输出模式 (?t=json)
if ($type === 'json') {
header('Content-Type: application/json; charset=utf-8');
if (!empty($error)) {
http_response_code(400);
echo json_encode([
'code' => 400,
'status' => 'error',
'message' => $error
], JSON_UNESCAPED_UNICODE);
} else {
echo json_encode([
'code' => 200,
'status' => 'success',
'data' => [
'password' => $password,
'cost' => $cost,
'hash' => $hashedPassword
]
], JSON_UNESCAPED_UNICODE);
}
exit;
}
// ====================================================
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BCrypt密码加密工具</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/css/font-awesome.min.css" rel="stylesheet">
<script>
tailwind.config = {
theme: {
extend: {
colors: {
primary: '#3B82F6',
secondary: '#10B981',
dark: '#1E293B',
light: '#F8FAFC'
},
fontFamily: {
sans: ['Inter', 'system-ui', 'sans-serif'],
},
}
}
}
</script>
<style type="text/tailwindcss">
@layer utilities {
.content-auto {
content-visibility: auto;
}
.shadow-soft {
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}
.transition-custom {
transition: all 0.3s ease;
}
}
</style>
</head>
<body class="bg-gray-50 min-h-screen">
<div class="container mx-auto px-4 py-12 max-w-4xl">
<header class="text-center mb-10">
<h1 class="text-[clamp(1.8rem,4vw,2.5rem)] font-bold text-dark mb-3">
<i class="fa fa-key text-primary mr-2"></i>BCrypt密码加密工具
</h1>
<p class="text-gray-600 max-w-2xl mx-auto">
安全生成BCrypt加密密码,支持自定义加密强度(cost),保护您的密码安全
</p>
</header>
<div class="bg-white rounded-xl shadow-soft p-6 md:p-8 mb-8">
<form method="get" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" class="space-y-6">
<div>
<label for="password" class="block text-sm font-medium text-gray-700 mb-1">
原始密码
</label>
<div class="relative">
<span class="absolute inset-y-0 left-0 flex items-center pl-3 text-gray-500">
<i class="fa fa-lock"></i>
</span>
<input type="text" id="password" name="p"
value="<?php echo htmlspecialchars($password); ?>"
placeholder="输入需要加密的密码"
class="pl-10 w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary/50 focus:border-primary transition-custom outline-none"
required>
</div>
</div>
<div>
<label for="cost" class="block text-sm font-medium text-gray-700 mb-1">
加密强度 (cost)
</label>
<div class="relative">
<span class="absolute inset-y-0 left-0 flex items-center pl-3 text-gray-500">
<i class="fa fa-shield"></i>
</span>
<select id="cost" name="c"
class="pl-10 w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary/50 focus:border-primary transition-custom outline-none appearance-none bg-white">
<?php for ($i = 10; $i <= 12; $i++): ?>
<option value="<?php echo $i; ?>" <?php echo $cost == $i ? 'selected' : ''; ?>>
<?php echo $i; ?> (<?php echo pow(2, $i); ?> 次迭代)
</option>
<?php endfor; ?>
</select>
<div class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-3 text-gray-500">
<i class="fa fa-chevron-down text-xs"></i>
</div>
</div>
<p class="text-xs text-gray-500 mt-1">
数值越高,加密强度越大,生成时间越长(推荐10-12)
</p>
</div>
<div>
<button type="submit"
class="w-full bg-primary hover:bg-primary/90 text-white font-medium py-3 px-4 rounded-lg transition-custom flex items-center justify-center">
<i class="fa fa-cogs mr-2"></i>生成加密密码
</button>
</div>
</form>
</div>
<?php if (!empty($hashedPassword)): ?>
<div class="bg-white rounded-xl shadow-soft p-6 md:p-8 border-l-4 border-secondary">
<h2 class="text-xl font-semibold text-dark mb-4">
<i class="fa fa-check-circle text-secondary mr-2"></i>加密结果
</h2>
<div class="mb-4">
<div class="flex items-center justify-between mb-1">
<span class="text-sm font-medium text-gray-700">加密后的密码:</span>
<button onclick="copyToClipboard('hashedResult')"
class="text-xs text-primary hover:text-primary/80 transition-custom">
<i class="fa fa-copy mr-1"></i>复制
</button>
</div>
<div class="relative">
<textarea id="hashedResult" readonly
class="w-full px-4 py-3 bg-gray-50 border border-gray-300 rounded-lg text-sm font-mono overflow-x-auto"
rows="2"><?php echo htmlspecialchars($hashedPassword); ?></textarea>
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-sm">
<div class="bg-gray-50 p-3 rounded-lg">
<span class="text-gray-500">原始密码:</span>
<span class="font-medium ml-1"><?php echo htmlspecialchars($password ?: '无'); ?></span>
</div>
<div class="bg-gray-50 p-3 rounded-lg">
<span class="text-gray-500">加密强度(cost):</span>
<span class="font-medium ml-1"><?php echo $cost; ?></span>
</div>
</div>
<div class="mt-4 pt-4 border-t border-gray-100">
<p class="text-xs text-gray-500">
<i class="fa fa-info-circle mr-1"></i>
密文格式说明: $2y$[cost]$[22位盐值][31位哈希值]
</p>
</div>
</div>
<?php endif; ?>
<?php if (!empty($error)): ?>
<div class="bg-white rounded-xl shadow-soft p-6 md:p-8 border-l-4 border-red-500">
<h2 class="text-xl font-semibold text-red-500 mb-2">
<i class="fa fa-exclamation-circle mr-2"></i>错误
</h2>
<p class="text-gray-600"><?php echo htmlspecialchars($error); ?></p>
</div>
<?php endif; ?>
<div class="mt-10 bg-white rounded-xl shadow-soft p-6 md:p-8">
<h3 class="text-lg font-semibold text-dark mb-3">
<i class="fa fa-code text-primary mr-2"></i>通过 API 直接调用
</h3>
<p class="text-gray-600 text-sm mb-4">您可以添加 <code class="bg-gray-100 text-red-500 px-1 py-0.5 rounded">t</code> 参数来控制返回结果格式:</p>
<div class="space-y-3">
<div>
<span class="text-xs font-semibold text-gray-500 block mb-1">1. 纯文本格式 (t=raw):</span>
<div class="bg-gray-50 p-3 rounded-lg font-mono text-sm overflow-x-auto">
<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>?p=example123&c=10&t=raw
</div>
</div>
<div>
<span class="text-xs font-semibold text-gray-500 block mb-1">2. JSON 格式 (t=json):</span>
<div class="bg-gray-50 p-3 rounded-lg font-mono text-sm overflow-x-auto">
<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>?p=example123&c=10&t=json
</div>
</div>
</div>
<p class="text-gray-600 text-sm mt-4 mb-2">测试链接:</p>
<div class="flex flex-wrap gap-4 text-sm">
<a href="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>?p=example123&c=10" target="_blank"
class="text-primary hover:text-primary/80 transition-custom inline-flex items-center">
页面模式 <i class="fa fa-external-link ml-1"></i>
</a>
<a href="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>?p=example123&c=10&t=raw" target="_blank"
class="text-primary hover:text-primary/80 transition-custom inline-flex items-center">
Raw 模式 <i class="fa fa-external-link ml-1"></i>
</a>
<a href="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>?p=example123&c=10&t=json" target="_blank"
class="text-primary hover:text-primary/80 transition-custom inline-flex items-center">
JSON 模式 <i class="fa fa-external-link ml-1"></i>
</a>
</div>
</div>
<footer class="mt-12 text-center text-gray-500 text-sm">
<p>使用BCrypt加密算法 | 安全哈希密码生成工具</p>
</footer>
</div>
<script>
// 复制到剪贴板功能
function copyToClipboard(elementId) {
const element = document.getElementById(elementId);
element.select();
document.execCommand('copy');
const originalText = element.value;
element.value = "已复制到剪贴板!";
setTimeout(() => {
element.value = originalText;
}, 2000);
}
// 页面加载时自动聚焦到密码输入框
document.addEventListener('DOMContentLoaded', () => {
const pwdInput = document.getElementById('password');
if (pwdInput) pwdInput.focus();
});
</script>
</body>
</html>
AI味道是不是很浓?有了AI是方便了很多,只要有想法,基本上都能帮助你实现。