UNPKG

@joingo/sdk-security

Version:

SDK for JavaScript: Security Application Block!

159 lines (145 loc) 6.24 kB
/* ************************************************************************************************************************ *\ * SDK for JavaScript! * * * * COPYRIGHT © 2021 BEIJING JOINGO INFORMATION TECHNOLOGY CO., LTD. ALL RIGHTS RESERVED. * * LICENSED UNDER THE MIT LICENSE. SEE LICENSE FILE IN THE PROJECT ROOT FOR FULL LICENSE INFORMATION. * * * * AUTHOR : WANG YUCAI * * E-MAIL ADDRESS: WANGYUCAI@JOINGO.VIP * * DATE TIME : 2021-12-29 14:30 * \* ************************************************************************************************************************ */ import { isDevelopmentEnvironment, isNullOrEmpty, sealed } from "@joingo/sdk-core"; import CryptoJS from "crypto-js"; // CODEFILE: cryptography.ts // FEATURE: 提供了加密、解密相关的方法。 // FILE-VERSION: v2021.12.29-build.1430 /** * 定义了加密服务的接口。 * * @export * @interface ICryptographyServiceProvider */ export interface ICryptographyServiceProvider { /** * 使用密钥 secureKey 加密明文 plainText。 * * @param {string} plainText 需要加密的明文字符串。 * @param {string} secureKey 密钥。 * @returns {string} * @memberof ICryptographyServiceProvider */ encrypt(plainText: string, secureKey: string): string; /** * 使用密钥 secureKey 解密字符串 encryptedText。 * * @param {string} encryptedText 需要解密的密文。 * @param {string} secureKey 密钥。 * @returns {string} * @memberof ICryptographyServiceProvider */ decrypt(encryptedText: string, secureKey: string): string; } /** * 提供了加密解密相关的基本方法。 * * @export * @abstract * @class CryptographyServiceProvider * @implements {ICryptographyServiceProvider} */ export abstract class CryptographyServiceProvider implements ICryptographyServiceProvider { protected abstract readonly DEFAULT_SECURE_KEY: string; private readonly _allowDebug: boolean; /** * 用于初始化一个 CryptographyServiceProvider 类型的对象实例。 * @memberof CryptographyServiceProvider * @constructor */ constructor() { this._allowDebug = isDevelopmentEnvironment(); } /** * 获取密钥。 * * @protected * @param {string} secureKey 密钥。 * @returns {string} * @memberof CryptographyServiceProvider */ protected safeGetSecureKey(secureKey: string): string { if (isNullOrEmpty(secureKey)) { secureKey = this.DEFAULT_SECURE_KEY; console.warn(`[WARN]: 将使用默认的密钥 "${CryptoJS.MD5(secureKey).toString(CryptoJS.enc.Hex)}"。`); } return secureKey; } /** * 使用密钥 secureKey 加密明文 plainText。 * * @abstract * @protected * @param {string} plainText 需要加密的明文字符串。 * @param {string} secureKey 密钥。 * @returns {string} * @memberof ICryptographyServiceProvider */ protected abstract internalEncrypt(plainText: string, secureKey: string): string; /** * 使用密钥 secureKey 解密字符串 encryptedText。 * * @abstract * @protected * @param {string} encryptedText 需要解密的密文。 * @param {string} secureKey 密钥。 * @returns {string} * @memberof ICryptographyServiceProvider */ protected abstract internalDecrypt(encryptedText: string, secureKey: string): string; encrypt(plainText: string, secureKey: string): string { if (isNullOrEmpty(plainText)) return ""; secureKey = this.safeGetSecureKey(secureKey); const encryptedText: string = this.internalEncrypt(plainText, secureKey); if (this._allowDebug) console.debug(`[DEBUG]: 字符串 "${plainText}" 加密为 "${encryptedText}"。`); return encryptedText; } decrypt(encryptedText: string, secureKey: string): string { if (isNullOrEmpty(encryptedText)) return ""; secureKey = this.safeGetSecureKey(secureKey); const plainText: string = this.internalDecrypt(encryptedText, secureKey); if (this._allowDebug) console.debug(`[DEBUG]: 密文 "${encryptedText}" 解密为 "${plainText}"。`); return plainText; } } /** * 提供了 AES 加密、解密相关的方法。密闭的,不可以从此类型派生。 * * @export * @class AESCryptographyServiceProvider * @extends {CryptographyServiceProvider} * @implements {ICryptographyServiceProvider} */ @sealed export class AESCryptographyServiceProvider extends CryptographyServiceProvider implements ICryptographyServiceProvider { protected DEFAULT_SECURE_KEY: string; /** * 用于初始化一个 AESCryptographyServiceProvider 类型的对象实例。 * @memberof AESCryptographyServiceProvider * @constructor */ constructor() { super(); this.DEFAULT_SECURE_KEY = isDevelopmentEnvironment() ? "S7IUwPgankdLXH8EVQNMosAWF1eDl2Tm" : "xoDghadlKZ8ym6jcz5p1MtLbVsAvGWI4"; } protected internalEncrypt(plainText: string, secureKey: string): string { return CryptoJS.AES.encrypt(plainText, secureKey).toString(); } protected internalDecrypt(encryptedText: string, secureKey: string): string { return CryptoJS.AES.decrypt(encryptedText, secureKey).toString(CryptoJS.enc.Utf8); } } /** * 提供了 AES 加密解密相关的方法。 */ export const AES: ICryptographyServiceProvider = new AESCryptographyServiceProvider();