part of utils;

class EncryptUtil {
  /// md5 加密
  static String encodeMd5(String data) {
    var content = Utf8Encoder().convert(data);
    var digest = md5.convert(content);
    return hex.encode(digest.bytes);
  }

  /// AES 加密
  static aesEncrypt(String content,[String aesKey = AppConfig.aesKey,]) {
    final key = encrypt.Key.fromUtf8(aesKey);
    final iv = encrypt.IV.fromBase64(base64Encode(utf8.encode(AppConfig.aesIV)));
    final encryptor = encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc),);
    final encrypted = encryptor.encrypt(content, iv: iv);
    return base64Encode(utf8.encode(encrypted.base64));
  }
  /// AES 解密
  static aesDecrypt(String content,[String aesKey = AppConfig.aesKey,]) {
    final key = encrypt.Key.fromUtf8(aesKey);
    final iv = encrypt.IV.fromBase64(base64Encode(utf8.encode(AppConfig.aesIV)));
    final encryptor = encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc));
    final encrypted = encrypt.Encrypted.fromBase64(utf8.decode(base64.decode(content)));
    final decrypted = encryptor.decrypt(encrypted, iv: iv);
    return decrypted.trimRight();
  }



}