import * as crypto from 'crypto'; import * as fs from 'fs'; import * as path from 'path'; // 生成 RSA 密钥对 const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048, publicKeyEncoding: { type: 'spki', format: 'pem', }, privateKeyEncoding: { type: 'pkcs8', format: 'pem', }, }); // 生成 AES 密钥(32字节) const aesKey = crypto.randomBytes(32).toString('hex'); // 生成 JWT 密钥 const jwtSecret = crypto.randomBytes(32).toString('hex'); // 创建 keys 目录 const keysDir = path.join(__dirname, '..', 'keys'); if (!fs.existsSync(keysDir)) { fs.mkdirSync(keysDir, { recursive: true }); } // 保存密钥文件 fs.writeFileSync(path.join(keysDir, 'private.key'), privateKey); fs.writeFileSync(path.join(keysDir, 'public.key'), publicKey); // 生成 .env 内容 const envContent = `# 数据库连接 DATABASE_URL="postgresql://postgres:password@localhost:5432/license_server?schema=public" # RSA 私钥(用于签名许可证) RSA_PRIVATE_KEY="${privateKey.replace(/\n/g, '\\n')}" # RSA 公钥(嵌入客户端用于验证签名) RSA_PUBLIC_KEY="${publicKey.replace(/\n/g, '\\n')}" # AES 加密密钥 AES_SECRET_KEY="${aesKey}" # JWT 密钥 JWT_SECRET="${jwtSecret}" # 管理员初始密码 ADMIN_INITIAL_PASSWORD="admin123" `; fs.writeFileSync(path.join(__dirname, '..', '.env'), envContent); console.log('✅ 密钥生成完成!'); console.log(''); console.log('文件已生成:'); console.log(' - keys/private.key (RSA 私钥)'); console.log(' - keys/public.key (RSA 公钥)'); console.log(' - .env (环境变量配置)'); console.log(''); console.log('⚠️ 请妥善保管私钥,不要提交到代码仓库!'); console.log(''); console.log('📋 客户端公钥(复制到素材系统):'); console.log('─'.repeat(50)); console.log(publicKey);