Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dart encryption #104

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions lib/src/common/crypto.dart
Original file line number Diff line number Diff line change
@@ -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<RSAPublicKey>(public));

/// Converting into a [Unit8List] from List<int>
/// 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<String> encrypt(String data) async {
var completer = Completer<String>();

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);
Expand Down
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down