Add callList invoke and catch all platform exceptions
This commit is contained in:
parent
efcf21ebc8
commit
a5e42970ce
@ -4,9 +4,10 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:native_crypto/src/digest.dart';
|
||||
|
||||
import 'cipher.dart';
|
||||
import 'digest.dart';
|
||||
import 'exceptions.dart';
|
||||
import 'utils.dart';
|
||||
|
||||
/// Represents a platform, and is usefull to calling
|
||||
@ -20,6 +21,12 @@ class Platform {
|
||||
return _channel.invokeMethod(method, arguments);
|
||||
}
|
||||
|
||||
/// Calls native code that return list.
|
||||
static Future<List<T>> callList<T>(String method,
|
||||
[Map<String, dynamic> arguments]) {
|
||||
return _channel.invokeListMethod(method, arguments);
|
||||
}
|
||||
|
||||
/// Digests a message with a specific algorithm
|
||||
///
|
||||
/// Takes message and algorithm as parameters.
|
||||
@ -29,11 +36,15 @@ class Platform {
|
||||
Uint8List message,
|
||||
HashAlgorithm algorithm,
|
||||
) async {
|
||||
try {
|
||||
final Uint8List hash = await call('digest', <String, dynamic>{
|
||||
'message': message,
|
||||
'algorithm': algorithm.name,
|
||||
});
|
||||
return hash;
|
||||
} on PlatformException catch (e) {
|
||||
throw DigestException(e.code + " : " + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls native PBKDF2.
|
||||
@ -49,6 +60,7 @@ class Platform {
|
||||
int iteration: 10000,
|
||||
HashAlgorithm algorithm: HashAlgorithm.SHA256,
|
||||
}) async {
|
||||
try {
|
||||
final Uint8List key = await call('pbkdf2', <String, dynamic>{
|
||||
'password': password,
|
||||
'salt': salt,
|
||||
@ -57,6 +69,9 @@ class Platform {
|
||||
'algorithm': algorithm.name,
|
||||
});
|
||||
return key;
|
||||
} on PlatformException catch (e) {
|
||||
throw KeyDerivationException(e.code + " : " + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a random key.
|
||||
@ -65,11 +80,14 @@ class Platform {
|
||||
///
|
||||
/// Returns a key as [Uint8List].
|
||||
Future<Uint8List> keygen(int size) async {
|
||||
try {
|
||||
final Uint8List key = await call('keygen', <String, dynamic>{
|
||||
'size': size,
|
||||
});
|
||||
|
||||
return key;
|
||||
} on PlatformException catch (e) {
|
||||
throw KeyGenerationException(e.code + " : " + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates an RSA key pair.
|
||||
@ -79,12 +97,15 @@ class Platform {
|
||||
/// Returns a key pair as list of [Uint8List], the public key is the
|
||||
/// first element, and the private is the last.
|
||||
Future<List<Uint8List>> rsaKeypairGen(int size) async {
|
||||
try {
|
||||
final List<Uint8List> keypair =
|
||||
await call('rsaKeypairGen', <String, dynamic>{
|
||||
'size': size,
|
||||
});
|
||||
|
||||
return keypair;
|
||||
} on PlatformException catch (e) {
|
||||
throw KeyPairGenerationException(e.code + " : " + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
/// Encrypts data with a secret key and algorithm.
|
||||
@ -99,7 +120,9 @@ class Platform {
|
||||
CipherAlgorithm algorithm,
|
||||
CipherParameters parameters,
|
||||
) async {
|
||||
final List<Uint8List> payload = await call('encrypt', <String, dynamic>{
|
||||
try {
|
||||
final List<Uint8List> payload =
|
||||
await callList('encrypt', <String, dynamic>{
|
||||
'data': data,
|
||||
'key': key,
|
||||
'algorithm': algorithm.name,
|
||||
@ -107,6 +130,9 @@ class Platform {
|
||||
'padding': parameters.padding.name,
|
||||
});
|
||||
return payload;
|
||||
} on PlatformException catch (e) {
|
||||
throw EncryptionException(e.code + " : " + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
/// Decrypts a payload with a secret key and algorithm.
|
||||
@ -119,6 +145,7 @@ class Platform {
|
||||
CipherAlgorithm algorithm,
|
||||
CipherParameters parameters,
|
||||
) async {
|
||||
try {
|
||||
final Uint8List data =
|
||||
await _channel.invokeMethod('decrypt', <String, dynamic>{
|
||||
'payload': payload,
|
||||
@ -128,5 +155,8 @@ class Platform {
|
||||
'padding': parameters.padding.name,
|
||||
});
|
||||
return data;
|
||||
} on PlatformException catch (e) {
|
||||
throw DecryptionException(e.code + " : " + e.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user