Initial commit
This commit is contained in:
55
remote/scripts/migrate-password.js
Normal file
55
remote/scripts/migrate-password.js
Normal file
@@ -0,0 +1,55 @@
|
||||
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);
|
||||
Reference in New Issue
Block a user