"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CryptoJsUtils = void 0;
var CryptoJS = require("crypto-js");
// const CryptoJS = require('crypto-js');
var NodeRSA = require("node-rsa");
/**
 * crypto工具类, `md5`、`base64`、`sha1`、`sha256`、`hmac`、`aes`、`des`、`rsa`
 *
 * @see [crypto-js](https://cryptojs.gitbook.io/docs/)
 */
var CryptoJsUtils = /** @class */ (function () {
    function CryptoJsUtils() {
    }
    /**
     * `md5`
     */
    CryptoJsUtils.md5 = function (message) {
        return CryptoJS.MD5(message).toString();
    };
    /**
     * `sha1`
     */
    CryptoJsUtils.sha1 = function (message) {
        return CryptoJS.SHA1(message).toString();
    };
    /**
     * `sha3`
     */
    CryptoJsUtils.sha3 = function (message) {
        return CryptoJS.SHA3(message).toString();
    };
    /**
     * `sha256`
     */
    CryptoJsUtils.sha256 = function (message) {
        return CryptoJS.SHA256(message).toString();
    };
    /**
     * `sha512`
     */
    CryptoJsUtils.sha512 = function (message) {
        return CryptoJS.SHA512(message).toString();
    };
    /**
     * `hmacMD5`
     * @param message
     * @param key
     * @param encoder
     */
    CryptoJsUtils.hmacMD5 = function (message, key) {
        return CryptoJS.HmacMD5(message, key).toString();
    };
    /**
     * `hmacSHA1`
     */
    CryptoJsUtils.hmacSHA1 = function (message, key) {
        return CryptoJS.HmacSHA1(message, key).toString();
    };
    /**
     * `hmacSHA256`
     */
    CryptoJsUtils.hmacSHA256 = function (message, key) {
        return CryptoJS.HmacSHA256(message, key).toString();
    };
    /**
     * `hmacSHA512`
     */
    CryptoJsUtils.hmacSHA512 = function (message, key) {
        return CryptoJS.HmacSHA512(message, key).toString();
    };
    /**
     * `base64`编码
     */
    CryptoJsUtils.base64Encode = function (data) {
        return Buffer.from(data).toString('base64');
    };
    /**
     * `base64`解码
     */
    CryptoJsUtils.base64Decode = function (data) {
        return Buffer.from(data, 'base64').toString();
    };
    /**
     * `aes`加密
     *
     * ```
     * import { CryptoUtils } from "refs/nslib";
     * const CryptoJS = require('crypto-js');
     *
     * let encrypted = CryptoJsUtils.aesEncrypt('123456', '123456', { mode: 'CBC' });
     * logger.info(encrypted.toString());
     *
     * let decrypted = CryptoJsUtils.aesDecrypt(encrypted.toString(), '123456', { mode: 'CBC' });
     * logger.info(decrypted.toString(CryptoJS.enc.Utf8));
     *
     * //杭停
     * let key = CryptoJS.enc.Hex.parse('88888888');
     * let encrypted = CryptoJsUtils.aesEncrypt('123456', key, { mode: 'ECB' });
     * logger.info(encrypted.toString());
     *
     * let decrypted = CryptoJsUtils.aesDecrypt(encrypted.ciphertext.toString(CryptoJS.enc.Base64), key, { mode: 'ECB' });
     * logger.info(decrypted.toString(CryptoJS.enc.Utf8));
     *
     * //iv
     * let iv = CryptoJS.enc.Hex.parse("101112131415161718191a1b1c1d1e1f");
     * let encrypted = CryptoJsUtils.aesEncrypt('123456', '88888888', { mode: 'CBC', iv: iv });
     * logger.info(encrypted.toString());
     *
     * let decrypted = CryptoJsUtils.aesDecrypt(encrypted.toString(), '88888888', { mode: 'CBC', iv: iv });
     * logger.info(decrypted.toString(CryptoJS.enc.Utf8));
     * ```
     * @param message 需要加密的数据
     * @param key 密钥
     * @param cfg 加解密配置, mode默认:`CBC`, padding默认:`Pkcs7`
     */
    CryptoJsUtils.aesEncrypt = function (message, key, cfg) {
        return CryptoJS.AES.encrypt(message, key, {
            padding: CryptoJS.pad[(cfg === null || cfg === void 0 ? void 0 : cfg.padding) || 'Pkcs7'],
            mode: CryptoJS.mode[(cfg === null || cfg === void 0 ? void 0 : cfg.mode) || 'CBC'],
            iv: cfg === null || cfg === void 0 ? void 0 : cfg.iv
        });
    };
    /**
     * `aes`解密
     *
     * @see {@link aesEncrypt}
     */
    CryptoJsUtils.aesDecrypt = function (ciphertext, key, cfg) {
        return CryptoJS.AES.decrypt(ciphertext, key, {
            padding: CryptoJS.pad[(cfg === null || cfg === void 0 ? void 0 : cfg.padding) || 'Pkcs7'],
            mode: CryptoJS.mode[(cfg === null || cfg === void 0 ? void 0 : cfg.mode) || 'CBC'],
            iv: cfg === null || cfg === void 0 ? void 0 : cfg.iv
        });
    };
    /**
     * `des`加密
     *
     * ```
     * import { CryptoUtils } from "refs/nslib";
     * const CryptoJS = require('crypto-js');
     *
     * let encrypted = CryptoJsUtils.desEncrypt('123456', '123456', { mode: 'CBC' });
     * logger.info(encrypted.toString());
     *
     * let decrypted = CryptoJsUtils.desDecrypt(encrypted.toString(), '123456', { mode: 'CBC' });
     * logger.info(decrypted.toString(CryptoJS.enc.Utf8));
     * ```
     *
     * @see {@link aesEncrypt}
     * @param message 需要加密的数据
     * @param key 密钥
     * @param cfg 加解密配置, mode默认:`CBC`, padding默认:`Pkcs7`
     */
    CryptoJsUtils.desEncrypt = function (message, key, cfg) {
        return CryptoJS.DES.encrypt(message, key, {
            padding: CryptoJS.pad[(cfg === null || cfg === void 0 ? void 0 : cfg.padding) || 'Pkcs7'],
            mode: CryptoJS.mode[(cfg === null || cfg === void 0 ? void 0 : cfg.mode) || 'CBC'],
            iv: cfg === null || cfg === void 0 ? void 0 : cfg.iv
        });
    };
    /**
     * `des`解密
     *
     * @see {@link aesEncrypt}
     */
    CryptoJsUtils.desDecrypt = function (ciphertext, key, cfg) {
        return CryptoJS.DES.decrypt(ciphertext, key, {
            padding: CryptoJS.pad[(cfg === null || cfg === void 0 ? void 0 : cfg.padding) || 'Pkcs7'],
            mode: CryptoJS.mode[(cfg === null || cfg === void 0 ? void 0 : cfg.mode) || 'CBC'],
            iv: cfg === null || cfg === void 0 ? void 0 : cfg.iv
        });
    };
    /**
     * `3des`/`TripleDES` 加密
     *
     * ```ts
     * import { CryptoUtils } from "refs/nslib";
     * const CryptoJS = require('crypto-js');
     *
     * let encrypted = CryptoJsUtils.des3Encrypt('123456', '123456', { mode: 'CBC' });
     * logger.info(encrypted.toString());
     *
     * let decrypted = CryptoJsUtils.des3Decrypt(encrypted.toString(), '123456', { mode: 'CBC' });
     * logger.info(decrypted.toString(CryptoJS.enc.Utf8));
     * ```
     * @see {@link aesEncrypt}
     * @param message 需要加密的数据
     * @param key 密钥
     * @param cfg 加解密配置, mode默认:`CBC`, padding默认:`Pkcs7`, `iv === string`? `CryptoJS.enc.Hex.parse(iv) : iv`
     */
    CryptoJsUtils.des3Encrypt = function (message, key, cfg) {
        return CryptoJS.TripleDES.encrypt(message, key, {
            padding: CryptoJS.pad[(cfg === null || cfg === void 0 ? void 0 : cfg.padding) || 'Pkcs7'],
            mode: CryptoJS.mode[(cfg === null || cfg === void 0 ? void 0 : cfg.mode) || 'CBC'],
            iv: cfg === null || cfg === void 0 ? void 0 : cfg.iv
        });
    };
    /**
     * `3des`/`TripleDES` 解密
     *
     * @see {@link aesEncrypt}
     */
    CryptoJsUtils.des3Decrypt = function (ciphertext, key, cfg) {
        return CryptoJS.TripleDES.decrypt(ciphertext, key, {
            padding: CryptoJS.pad[(cfg === null || cfg === void 0 ? void 0 : cfg.padding) || 'Pkcs7'],
            mode: CryptoJS.mode[(cfg === null || cfg === void 0 ? void 0 : cfg.mode) || 'CBC'],
            iv: cfg === null || cfg === void 0 ? void 0 : cfg.iv
        });
    };
    /**
     * 生成`rsa`密钥对
     *
     * ```
     * import { CryptoUtils } from "refs/nslib";
     *
     * let keys = CryptoJsUtils.generateRSAKeys();
     *
     * logger.info(keys.publicKey);
     * logger.info(keys.privateKey);
     * ```
     *
     * @param publicKeyFormat 公钥格式，默认为：`pkcs8-public-pem`
     * @param privateKeyFormat 私钥格式，默认为：`pkcs8-private-pem`
     */
    CryptoJsUtils.generateRSAKeys = function (length, publicKeyFormat, privateKeyFormat) {
        if (length === void 0) { length = 1024; }
        if (publicKeyFormat === void 0) { publicKeyFormat = 'pkcs8-public-pem'; }
        if (privateKeyFormat === void 0) { privateKeyFormat = 'pkcs8-private-pem'; }
        var key = new NodeRSA({ b: length });
        var publicKey = key.exportKey(publicKeyFormat).toString();
        var privateKey = key.exportKey(privateKeyFormat).toString();
        return {
            publicKey: publicKey,
            privateKey: privateKey
        };
    };
    /**
     * `rsa`公钥加密
     *
     * ```
     * import { CryptoUtils } from "refs/nslib";
     *
     * let keys = CryptoJsUtils.generateRSAKeys();
     *
     * let encrypted = CryptoJsUtils.rsaEncrypt(keys.publicKey, '123456', { keyFormat: 'pkcs8-public', encryptionScheme: 'pkcs1' });
     * let decrypted = CryptoJsUtils.rsaDecrypt(keys.privateKey, encrypted, { keyFormat: 'pkcs8-private', encryptionScheme: 'pkcs1' });
     * ```
     * @param publicKey 公钥
     * @param data
     * @param cfg 公钥格式 keyFormat 默认:`pkcs8-public`, 加密方案 encryptionScheme 默认:`pkcs1`
     */
    CryptoJsUtils.rsaEncrypt = function (publicKey, data, cfg) {
        var pub = new NodeRSA(publicKey, (cfg === null || cfg === void 0 ? void 0 : cfg.keyFormat) || 'pkcs8-public');
        pub.setOptions({ encryptionScheme: (cfg === null || cfg === void 0 ? void 0 : cfg.encryptionScheme) || 'pkcs1' });
        return pub.encrypt(data, 'base64');
    };
    /**
     * `rsa`私钥解密
     *
     * @param privateKey 私钥
     * @param data
     * @param cfg 私钥格式 keyFormat 默认:`pkcs8-private`, 加密方案 encryptionScheme 默认:`pkcs1`
     */
    CryptoJsUtils.rsaDecrypt = function (privateKey, data, cfg) {
        var pri = new NodeRSA(privateKey, (cfg === null || cfg === void 0 ? void 0 : cfg.keyFormat) || 'pkcs8-private');
        pri.setOptions({ encryptionScheme: (cfg === null || cfg === void 0 ? void 0 : cfg.encryptionScheme) || 'pkcs1' });
        return pri.decrypt(data, 'utf8');
    };
    /**
     * `rsa`私钥签名
     * ```
     * import { CryptoUtils } from "refs/nslib";
     *
     * let keys = CryptoJsUtils.generateRSAKeys();
     *
     * let sign = CryptoJsUtils.rsaSign(keys.privateKey, '123456', { keyFormat: 'pkcs8-private', signingScheme: 'pkcs1-sha1' });
     * let verify = CryptoJsUtils.rsaVerifySign(keys.publicKey, '123456', sign, { keyFormat: 'pkcs8-public', signingScheme: 'pkcs1-sha1' });
     *
     * ```
     * @param cfg 私钥格式 keyFormat 默认：`pkcs8-private`, 签名方案 signingScheme 默认：`pkcs1-sha1`
     */
    CryptoJsUtils.rsaSign = function (privateKey, data, cfg) {
        var pri = new NodeRSA(privateKey, (cfg === null || cfg === void 0 ? void 0 : cfg.keyFormat) || 'pkcs8-private');
        pri.setOptions({ signingScheme: (cfg === null || cfg === void 0 ? void 0 : cfg.signingScheme) || 'pkcs1-sha1' });
        return pri.sign(data, 'base64');
    };
    /**
     * `rsa`公钥验签
     *
     * @param cfg 公钥格式 keyFormat 默认：`pkcs8-public`, 签名方案 signingScheme 默认：`pkcs1-sha1`
     * @see {@link rsaSign}
     */
    CryptoJsUtils.rsaVerifySign = function (publicKey, data, sign, cfg) {
        var pub = new NodeRSA(publicKey, (cfg === null || cfg === void 0 ? void 0 : cfg.keyFormat) || 'pkcs8-public');
        pub.setOptions({ signingScheme: (cfg === null || cfg === void 0 ? void 0 : cfg.signingScheme) || 'pkcs1-sha1' });
        return pub.verify(data, Buffer.from(sign, 'base64'));
    };
    return CryptoJsUtils;
}());
exports.CryptoJsUtils = CryptoJsUtils;
exports.default = CryptoJsUtils;
//# sourceMappingURL=CryptoJsUtils.js.map