const CryptoJS = require('crypto-js');

/**
 * crypto工具类, `md5`、`base64`、`sha1`、`sha256`、`hmac`、`aes`、`des`、`rsa`
 * 
 * @see [crypto-js](https://code.google.com/archive/p/crypto-js/)
 */
export default class CryptoUtils {
    /**
     * md5
     */
    static md5(data: any) {
        return CryptoJS.MD5(data).toString();
    }

    /**
     * sha1
     */
    static sha1(data: crypto.BinaryLike): string {
        const hash = crypto.createHash('sha1');
        hash.update(data);
        return hash.digest('hex');
    }

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

    /**
     * 对数据进行`base64`编码
     */
    static base64Encode(data: string) {
        return Buffer.from(data).toString('base64');
    }

    /**
     * 对字符串进行`base64`解码
     */
    static base64Decode(data: string, encoding?: BufferEncoding) {
        return Buffer.from(data, 'base64').toString(encoding);
    }
}

export { CryptoUtils }