Add pbkdf2 and exceptions support

This commit is contained in:
Hugo Pointcheval 2020-04-18 15:26:42 +02:00
parent 2a5c719c2e
commit 900178ba82

View File

@ -4,6 +4,7 @@ import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:native_crypto/exceptions.dart';
/// [Sources]
/// Plugin class.
@ -16,14 +17,38 @@ class NativeCrypto {
static const MethodChannel _channel =
const MethodChannel('native.crypto.helper');
/// PBKDF2.
///
/// [keyLength] is in Bytes.
/// It returns an `Uint8List`.
Future<Uint8List> pbkdf2(String password, String salt, {int keyLength: 32, int iteration: 10000}) async {
Uint8List key;
try {
key = await _channel.invokeMethod('pbkdf2', <String, dynamic>{
'password': password,
'salt': salt,
'keyLength': keyLength,
'iteration': iteration,
});
} on PlatformException catch (e) {
throw e;
}
return key;
}
/// Generates AES key.
///
/// [size] is in bits, 128, 192 or 256.
/// It returns an `Uint8List`.
Future<Uint8List> symKeygen(int size) async {
final Uint8List aesKey = await _channel.invokeMethod('symKeygen', <String, dynamic>{
'size': size
Uint8List aesKey;
try {
aesKey = await _channel.invokeMethod('symKeygen', <String, dynamic>{
'size': size,
});
} on PlatformException catch (e) {
throw e;
}
return aesKey;
}
@ -48,11 +73,15 @@ class NativeCrypto {
/// with encrypted cipher as first and IV as second member.
Future<Uint8List> symDecrypt(
List<Uint8List> payloadbytes, Uint8List aesKey) async {
final Uint8List decryptedPayload =
await _channel.invokeMethod('symDecrypt', <String, dynamic>{
Uint8List decryptedPayload;
try {
decryptedPayload = await _channel.invokeMethod('symDecrypt', <String, dynamic>{
'payload': payloadbytes,
'aesKey': aesKey,
});
} on PlatformException catch (e) {
throw DecryptionException(e.message);
}
return decryptedPayload;
}
}