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