import { logger } from "mslib"
const CryptoJS = require('crypto-js');
import * as dayjs from 'dayjs'

/**
 * 
 * @date 2021-07-16 14:04:33
 * @author 林颖
 */
export class StcLotIdUtils {

    /**
     * lotId加密
     */
    lotIdEncrypt(lotId: string): any {
        let ciphertext = lotId;
        let key = CryptoJS.enc.Utf8.parse(dayjs().format('YYYYMMDDYYYYMMDD'));
        let iv = CryptoJS.enc.Hex.parse('00000000');
        var options = {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        };
        ciphertext = CryptoJS.AES.encrypt(lotId, key, options).toString();
        logger.info("lotId:%s AES加密出来的密文----%s", lotId, ciphertext);
        return ciphertext;
    }

    /**
     * lotId解密
     */
    lotIdDecrypt(ciphertext: string): any {
        let lotId = ciphertext;
        let key = CryptoJS.enc.Utf8.parse(dayjs().format('YYYYMMDDYYYYMMDD'));
        let iv = CryptoJS.enc.Hex.parse('00000000');
        var options = {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        };
        ciphertext = ciphertext.replace(/=/g, "").replace(/\-/g, "+").replace(/_/g, "/"); // 转换为字符串
        lotId = CryptoJS.AES.decrypt(ciphertext, key, options).toString(CryptoJS.enc.Utf8);
        logger.info("ciphertext:%s AES解密出来的明文----%s", ciphertext, lotId);
        return lotId;
    }

}