- 新增许可证服务器 (license-server/) - 新增 Agent 部署模块 (flnxi-agent/) - 添加许可证验证和激活页面 - 添加 Harbor 镜像同步脚本 - 更新素材系统支持许可证验证
125 lines
3.2 KiB
TypeScript
125 lines
3.2 KiB
TypeScript
import * as crypto from 'crypto';
|
|
|
|
// RSA 密钥
|
|
const RSA_PRIVATE_KEY = process.env.RSA_PRIVATE_KEY?.replace(/\\n/g, '\n') || '';
|
|
const RSA_PUBLIC_KEY = process.env.RSA_PUBLIC_KEY?.replace(/\\n/g, '\n') || '';
|
|
|
|
// AES 密钥
|
|
const AES_SECRET_KEY = process.env.AES_SECRET_KEY || '';
|
|
|
|
/**
|
|
* 使用 RSA 私钥签名数据
|
|
*/
|
|
export function signData(data: string): string {
|
|
const sign = crypto.createSign('RSA-SHA256');
|
|
sign.update(data);
|
|
sign.end();
|
|
return sign.sign(RSA_PRIVATE_KEY, 'base64');
|
|
}
|
|
|
|
/**
|
|
* 使用 RSA 公钥验证签名
|
|
*/
|
|
export function verifySignature(data: string, signature: string): boolean {
|
|
try {
|
|
const verify = crypto.createVerify('RSA-SHA256');
|
|
verify.update(data);
|
|
verify.end();
|
|
return verify.verify(RSA_PUBLIC_KEY, signature, 'base64');
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 使用 AES-256-GCM 加密数据
|
|
*/
|
|
export function encryptAES(plaintext: string): string {
|
|
const iv = crypto.randomBytes(12);
|
|
const key = Buffer.from(AES_SECRET_KEY, 'hex');
|
|
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
|
|
|
|
let encrypted = cipher.update(plaintext, 'utf8', 'base64');
|
|
encrypted += cipher.final('base64');
|
|
|
|
const authTag = cipher.getAuthTag();
|
|
|
|
// 格式: iv:authTag:encrypted
|
|
return `${iv.toString('base64')}:${authTag.toString('base64')}:${encrypted}`;
|
|
}
|
|
|
|
/**
|
|
* 使用 AES-256-GCM 解密数据
|
|
*/
|
|
export function decryptAES(ciphertext: string): string {
|
|
const [ivBase64, authTagBase64, encrypted] = ciphertext.split(':');
|
|
|
|
const iv = Buffer.from(ivBase64, 'base64');
|
|
const authTag = Buffer.from(authTagBase64, 'base64');
|
|
const key = Buffer.from(AES_SECRET_KEY, 'hex');
|
|
|
|
const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
|
|
decipher.setAuthTag(authTag);
|
|
|
|
let decrypted = decipher.update(encrypted, 'base64', 'utf8');
|
|
decrypted += decipher.final('utf8');
|
|
|
|
return decrypted;
|
|
}
|
|
|
|
/**
|
|
* 生成许可证密钥 (XXXX-XXXX-XXXX-XXXX 格式)
|
|
*/
|
|
export function generateLicenseKey(): string {
|
|
const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; // 排除容易混淆的字符
|
|
const segments: string[] = [];
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
let segment = '';
|
|
for (let j = 0; j < 4; j++) {
|
|
segment += chars[crypto.randomInt(chars.length)];
|
|
}
|
|
segments.push(segment);
|
|
}
|
|
|
|
return segments.join('-');
|
|
}
|
|
|
|
/**
|
|
* 验证许可证密钥格式
|
|
*/
|
|
export function isValidLicenseKeyFormat(key: string): boolean {
|
|
return /^[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}$/.test(key);
|
|
}
|
|
|
|
/**
|
|
* 计算硬件指纹哈希
|
|
*/
|
|
export function hashFingerprint(fingerprint: object): string {
|
|
const data = JSON.stringify(fingerprint);
|
|
return crypto.createHash('sha256').update(data).digest('hex');
|
|
}
|
|
|
|
/**
|
|
* 签名许可证数据
|
|
*/
|
|
export function signLicense(licenseData: object): { data: object; signature: string; issuedAt: string } {
|
|
const issuedAt = new Date().toISOString();
|
|
const dataWithTimestamp = { ...licenseData, issuedAt };
|
|
const dataString = JSON.stringify(dataWithTimestamp);
|
|
const signature = signData(dataString);
|
|
|
|
return {
|
|
data: dataWithTimestamp,
|
|
signature,
|
|
issuedAt,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 获取公钥(供客户端使用)
|
|
*/
|
|
export function getPublicKey(): string {
|
|
return RSA_PUBLIC_KEY;
|
|
}
|