Created
February 1, 2024 06:57
-
-
Save ffkev/a831db8062e0d57407ce2356d2d84694 to your computer and use it in GitHub Desktop.
Custom Action implementation to encrypt a payload sent to the API
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 'package:encrypt/encrypt.dart' as en; | |
Future<String> encryptWithAesNonce( | |
dynamic jsonPayload, | |
String aesKeyString, | |
String nonceKeyString, | |
) async { | |
try { | |
// Your function code goes here | |
print("JSON Payload:"); | |
print(jsonPayload); | |
print(jsonPayload.runtimeType); | |
final data = json.encode(jsonPayload); | |
print(data); | |
final key = en.Key.fromBase64(aesKeyString); | |
final nonceKey = nonceKeyString; | |
final nonce = base64Url.decode(nonceKey); | |
final encrypter = en.Encrypter(en.AES(key, mode: en.AESMode.cbc)); | |
final encrypted = encrypter.encrypt(data, iv: en.IV(nonce)); | |
print(encrypted); | |
print(encrypted.base64); | |
return encrypted.base64; | |
} catch (e) { | |
// Handle the exception | |
print('An error occurred: $e'); | |
// For demonstration, let's return a simple error message. | |
return 'Error: Encryption failed'; | |
} finally { | |
// Optional: This block executes regardless of whether an exception occurred or not. | |
print('Encryption attempt finished.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment