import * as crypto from 'crypto'
const CryptoJS = require('crypto-js');
import * as iconv from 'iconv-lite';
import * as dayjs from 'dayjs'
/**
 * 加密工具
 * @date 2020-09-17 10:34:52
 * @author 吴锋霖
 */
class CryptoUtils {
    /**
     * MD5
     */
    static md5(txt: string): string {
        const hash = crypto.createHash('md5');
        hash.update(txt);
        return hash.digest('hex')
    }

    /**
     * SHA256
     */
    static sha256(txt: string): string {
        const hash = crypto.createHash('sha256');
        hash.update(txt);
        return hash.digest('hex')
    }

    //字符串非空判断
    static isEmpty(txt: string): boolean {
        if (txt == null || txt == '' || txt == undefined) {
            return true
        } else {
            return false
        }
    }

    /**
     * SHA256
     */
    static sha1(txt: string): string {
        const hash = crypto.createHash('sha1');
        hash.update(txt);
        return hash.digest('hex')
    }
    /**
     * Base64编码
     * @param txt 
     */
    static base64Encode(txt: string): string {
        return Buffer.from(txt).toString('base64')
    }

    /**
     * Base64解码
     * @param base64String 
     * @param encoding 
     */
    static base64Decode(base64String: string, encoding:BufferEncoding = 'utf8'): string {
        return Buffer.from(base64String, 'base64').toString(encoding)
    }
}

export { CryptoUtils }