From 900178ba827ae40f80c665eb16d8ec2f1a633c7f Mon Sep 17 00:00:00 2001 From: Hugo Pointcheval Date: Sat, 18 Apr 2020 15:26:42 +0200 Subject: [PATCH] Add pbkdf2 and exceptions support --- lib/src/native_crypto.dart | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/src/native_crypto.dart b/lib/src/native_crypto.dart index 5c4cde7..f4cd8ed 100644 --- a/lib/src/native_crypto.dart +++ b/lib/src/native_crypto.dart @@ -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 pbkdf2(String password, String salt, {int keyLength: 32, int iteration: 10000}) async { + Uint8List key; + try { + key = await _channel.invokeMethod('pbkdf2', { + '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 symKeygen(int size) async { - final Uint8List aesKey = await _channel.invokeMethod('symKeygen', { - 'size': size + Uint8List aesKey; + try { + aesKey = await _channel.invokeMethod('symKeygen', { + '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 symDecrypt( List payloadbytes, Uint8List aesKey) async { - final Uint8List decryptedPayload = - await _channel.invokeMethod('symDecrypt', { + Uint8List decryptedPayload; + try { + decryptedPayload = await _channel.invokeMethod('symDecrypt', { 'payload': payloadbytes, 'aesKey': aesKey, }); + } on PlatformException catch (e) { + throw DecryptionException(e.message); + } return decryptedPayload; } } \ No newline at end of file