diff --git a/lib/src/common/crypto.dart b/lib/src/common/crypto.dart index 55369722..ba4b0dcb 100644 --- a/lib/src/common/crypto.dart +++ b/lib/src/common/crypto.dart @@ -1,14 +1,56 @@ import 'dart:async'; +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:basic_utils/basic_utils.dart'; import 'package:flutter/services.dart'; -import 'package:flutter_paystack/src/common/utils.dart'; + +// Former Import from Method Channels +// import 'package:flutter_paystack/src/common/utils.dart'; + +import 'package:pointycastle/export.dart'; + +class Cryptom { + /// String Public Key + String publickey = + "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANIsL+RHqfkBiKGn/D1y1QnNrMkKzxWP" + + "2wkeSokw2OJrCI+d6YGJPrHHx+nmb/Qn885/R01Gw6d7M824qofmCvkCAwEAAQ=="; + + String encrypt(String plaintext, String publicKey) { + /// After a lot of research on how to convert the public key [String] to [RSA PUBLIC KEY] + /// We would have to use PEM Cert Type and the convert it from a PEM to an RSA PUBLIC KEY through basic_utils + /// + var pem = + '-----BEGIN RSA PUBLIC KEY-----\n$publickey\n-----END RSA PUBLIC KEY-----'; + var public = CryptoUtils.rsaPublicKeyFromPem(pem); + + /// Initalizing Cipher + var cipher = PKCS1Encoding(RSAEngine()); + cipher.init(true, PublicKeyParameter(public)); + + /// Converting into a [Unit8List] from List + /// Then Encoding into Base64 + Uint8List output = + cipher.process(Uint8List.fromList(utf8.encode(plaintext))); + var base64EncodedText = base64Encode(output); + return base64EncodedText; + } + + String text(String text) { + return encrypt(text, publickey); + } +} class Crypto { static Future encrypt(String data) async { var completer = Completer(); try { - String? result = await Utils.methodChannel - .invokeMethod('getEncryptedData', {"stringData": data}); + /// This is commented to prevent conflicts + // String? result = await Utils.methodChannel + // .invokeMethod('getEncryptedData', {"stringData": data}); + String? result = Cryptom().text(data); + print(result); completer.complete(result); } on PlatformException catch (e) { completer.completeError(e); diff --git a/pubspec.yaml b/pubspec.yaml index b3850600..6bf8517f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,6 +11,9 @@ dependencies: meta: ^1.3.0 async: ^2.5.0 + # Required for Dart Encryption + basic_utils: + pointycastle: dev_dependencies: flutter_test: sdk: flutter