Created
March 27, 2025 09:58
-
-
Save Shreemanarjun/cb583077d7fa423e912c1ad674917ff0 to your computer and use it in GitHub Desktop.
Encrypter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:convert'; | |
| import 'dart:isolate'; | |
| import 'package:encrypt/encrypt.dart'; | |
| import 'package:shippoing/bootstrap.dart'; | |
| import 'package:shippoing/const/secret_const.dart'; | |
| import 'package:shippoing/shared/exception/base_exception.dart'; | |
| /// Function to decrypt encrypted response data. | |
| /// The [body] parameter is a Map<String, dynamic> containing the encrypted response body. | |
| Map<String, dynamic> decryptResponseData(dynamic stringBody) { | |
| var body = {}; | |
| if (stringBody is Map<String, dynamic>) { | |
| body = stringBody; | |
| } else { | |
| body = jsonDecode(stringBody.toString()); | |
| } | |
| /// Extract the encrypted response body from the map. | |
| final jsonResponse = body["resBody"]; | |
| /// Retrieve encryption key and IV from StringConstants. | |
| String hexKey = SecretConst.encryptedKey; | |
| String hexIV = SecretConst.encryptedIV; | |
| /// Create Key and IV instances for decryption. | |
| final Key secretKey = Key.fromBase16(hexKey); | |
| final IV iv = IV.fromBase16(hexIV); | |
| /// Create Encrypted instance with AES decryption parameters. | |
| final Encrypter encrypted = | |
| Encrypter(AES(secretKey, mode: AESMode.cbc, padding: "PKCS7")); | |
| /// Extract the cipher text from the encrypted response. | |
| final String cipherText = jsonResponse.substring(32); | |
| /// Decrypt the cipher text using the specified key and IV. | |
| final decryptedData = | |
| encrypted.decrypt(Encrypted.fromBase16(cipherText), iv: iv); | |
| /// Parse the decrypted data as JSON. | |
| var finalResponse = jsonDecode(decryptedData); | |
| return finalResponse; | |
| } | |
| Map<String, dynamic>? encryptText(String? text) { | |
| String hexKey = SecretConst.encryptedKey; | |
| String hexIV = SecretConst.encryptedIV; | |
| String subIV = hexIV.substring(0, 32); | |
| final Key secretKey = Key.fromBase16(hexKey); | |
| final IV iv = IV.fromBase16(hexIV); | |
| final Encrypter encrypted = | |
| Encrypter(AES(secretKey, mode: AESMode.cbc, padding: 'PKCS7')); | |
| if (text == null) { | |
| return null; | |
| } else { | |
| final encryptedJson = encrypted.encrypt(text, iv: iv); | |
| Map<String, dynamic> encryptedBody = { | |
| "reqBody": subIV + encryptedJson.base16 | |
| }; | |
| return encryptedBody; | |
| } | |
| } | |
| /// This one for onError | |
| Future<String> errorBodyDecryptor(dynamic body, int? statusCode) async { | |
| if (body is Map<String, dynamic>) { | |
| final Map<String, dynamic> map = body; | |
| if (map['resBody'] != null) { | |
| final decrypetedResponse = await Isolate.run( | |
| () => decryptResponseData(body), | |
| ); | |
| final responseBody = | |
| await Isolate.run(() => jsonEncode(decrypetedResponse)); | |
| talker.debug(responseBody); | |
| return responseBody; | |
| } else { | |
| return APIException(response: body).toJson(); | |
| } | |
| } else if (body is String) { | |
| return APIException( | |
| statusCode: statusCode, | |
| response: decryptResponseData(body), | |
| ).toJson(); | |
| } else { | |
| return body.toString(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment