secKeyEncryptWithHeaderAppKey static method

Map<String, String>? secKeyEncryptWithHeaderAppKey(
  1. String auth,
  2. Map<String, dynamic>? jsonDecode
)

Implementation

static Map<String, String>? secKeyEncryptWithHeaderAppKey(String auth, Map<String, dynamic>? jsonDecode) {
  try {
    final iv = IV.fromUtf8(UFUtils.encryptionIV);
    final key = Key.fromSecureRandom(32).bytes;
    final appKeyData = DateTime.now().toUtc().toString();
    final jsonMap = {dataKey: appKeyData, accountType: UFUtils.xPortal};
    if (auth.isNotEmpty) jsonMap['authorization'] = "Bearer $auth";
    if (jsonDecode != null) {
      jsonDecode.forEach((key, value) {
        jsonMap[key] = "$value";
      });
    }
    debugPrint("Headers ----------- $jsonMap");
    final encrypter = Encrypter(AES(Key(key), mode: AESMode.cbc));
    final randomString = jsonEncode(jsonMap);
    final encrypted = encrypter.encrypt(randomString, iv: iv);

    final result = UFUtils.applyEncryption
      ? {
          sek: hex.encode(encrypted.bytes),
          hash: hex.encode(key),
          deviceTypeKey: 'android',
        } : jsonMap;
    return result;
  } catch (e) {
    return null;
  }
}