Files
XCDesktop/remote/scripts/migrate-password.js
2026-03-08 01:34:54 +08:00

56 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const bcrypt = require('bcryptjs');
const readline = require('readline');
const SALT_ROUNDS = 10;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function question(prompt) {
return new Promise((resolve) => {
rl.question(prompt, resolve);
});
}
async function hashPassword(password) {
return bcrypt.hash(password, SALT_ROUNDS);
}
async function main() {
console.log('=== 密码迁移脚本 ===');
console.log('此脚本将明文密码转换为 bcrypt 哈希值\n');
let continueInput = true;
while (continueInput) {
const password = await question('请输入要哈希的密码 (或输入 q 退出): ');
if (password.toLowerCase() === 'q') {
continueInput = false;
continue;
}
if (!password.trim()) {
console.log('密码不能为空,请重新输入\n');
continue;
}
try {
const hashedPassword = await hashPassword(password);
console.log('\n--- 结果 ---');
console.log(`明文密码: ${password}`);
console.log(`bcrypt 哈希: ${hashedPassword}`);
console.log('\n请将哈希值更新到环境变量或配置文件中\n');
} catch (error) {
console.error('哈希失败:', error.message);
}
}
console.log('\n迁移完成');
rl.close();
}
main().catch(console.error);