UNPKG

long-git-cli

Version:

A CLI tool for Git tag management.

133 lines 4.54 kB
"use strict"; /** * 加密模块 * 使用 AES-256-GCM 进行可逆加密 */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Encryption = void 0; const crypto = __importStar(require("crypto")); /** * 加密类 */ class Encryption { /** * 加密文本 * @param text 明文 * @returns 加密后的文本(格式:iv:encryptedData:authTag) */ static encrypt(text) { // 生成随机 IV const iv = crypto.randomBytes(16); // 创建加密器 const cipher = crypto.createCipheriv("aes-256-gcm", this.ENCRYPTION_KEY, iv); // 加密数据 let encrypted = cipher.update(text, "utf8", "hex"); encrypted += cipher.final("hex"); // 获取认证标签 const authTag = cipher.getAuthTag(); // 返回格式:iv:encryptedData:authTag return `${iv.toString("hex")}:${encrypted}:${authTag.toString("hex")}`; } /** * 解密文本 * @param encryptedText 加密的文本(格式:iv:encryptedData:authTag) * @returns 明文 */ static decrypt(encryptedText) { try { // 分割加密数据 const parts = encryptedText.split(":"); if (parts.length !== 3) { throw new Error("Invalid encrypted text format"); } const iv = Buffer.from(parts[0], "hex"); const encrypted = parts[1]; const authTag = Buffer.from(parts[2], "hex"); // 创建解密器 const decipher = crypto.createDecipheriv("aes-256-gcm", this.ENCRYPTION_KEY, iv); decipher.setAuthTag(authTag); // 解密数据 let decrypted = decipher.update(encrypted, "hex", "utf8"); decrypted += decipher.final("utf8"); return decrypted; } catch (error) { throw new Error(`Decryption failed: ${error instanceof Error ? error.message : String(error)}`); } } /** * 验证加密文本是否有效 * @param encryptedText 加密的文本 * @returns 是否有效 */ static isValid(encryptedText) { try { this.decrypt(encryptedText); return true; } catch { return false; } } /** * 兼容旧版本:hash 方法(现在直接调用 encrypt) * @deprecated 使用 encrypt 代替 */ static async hash(text) { return this.encrypt(text); } /** * 兼容旧版本:verify 方法(现在通过解密比较) * @deprecated 不再需要验证,直接解密使用 */ static async verify(text, hash) { try { const decrypted = this.decrypt(hash); return decrypted === text; } catch { return false; } } } exports.Encryption = Encryption; // 加密密钥(实际应用中应该从环境变量或用户输入获取) // 这里使用固定密钥,因为是本地存储 Encryption.ENCRYPTION_KEY = crypto .createHash("sha256") .update("long-cli-encryption-key-v1") .digest(); //# sourceMappingURL=encryption.js.map