From 4d84a938acc2157d93e2325381403c4844500d86 Mon Sep 17 00:00:00 2001 From: Pointcheval Hugo Date: Fri, 18 Dec 2020 13:52:30 +0100 Subject: [PATCH] Update platform with algo enums and digest --- lib/src/platform.dart | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/src/platform.dart b/lib/src/platform.dart index c423847..0e04b6b 100644 --- a/lib/src/platform.dart +++ b/lib/src/platform.dart @@ -4,8 +4,10 @@ import 'dart:typed_data'; import 'package:flutter/services.dart'; +import 'package:native_crypto/src/digest.dart'; import 'cipher.dart'; +import 'utils.dart'; /// Represents a platform, and is usefull to calling /// methods from a specific platform. @@ -18,6 +20,22 @@ class Platform { return _channel.invokeMethod(method, arguments); } + /// Digests a message with a specific algorithm + /// + /// Takes message and algorithm as parameters. + /// + /// Returns a hash as [Uint8List]. + Future digest( + Uint8List message, + HashAlgorithm algorithm, + ) async { + final Uint8List hash = await call('digest', { + 'message': message, + 'algorithm': algorithm.name, + }); + return hash; + } + /// Calls native PBKDF2. /// /// Takes password and salt as parameters. @@ -29,14 +47,14 @@ class Platform { String salt, { int keyLength: 32, int iteration: 10000, - String algorithm: 'sha256', + HashAlgorithm algorithm: HashAlgorithm.SHA256, }) async { final Uint8List key = await call('pbkdf2', { 'password': password, 'salt': salt, 'keyLength': keyLength, 'iteration': iteration, - 'algorithm': algorithm, + 'algorithm': algorithm.name, }); return key; } @@ -78,15 +96,15 @@ class Platform { Future> encrypt( Uint8List data, Uint8List key, - String algorithm, + CipherAlgorithm algorithm, CipherParameters parameters, ) async { final List payload = await call('encrypt', { 'data': data, 'key': key, - 'algorithm': algorithm, - 'mode': parameters.mode.index, - 'padding': parameters.padding.index, + 'algorithm': algorithm.name, + 'mode': parameters.mode.name, + 'padding': parameters.padding.name, }); return payload; } @@ -98,16 +116,16 @@ class Platform { Future decrypt( List payload, Uint8List key, - String algorithm, + CipherAlgorithm algorithm, CipherParameters parameters, ) async { final Uint8List data = await _channel.invokeMethod('decrypt', { 'payload': payload, 'key': key, - 'algorithm': algorithm, - 'mode': parameters.mode.index, - 'padding': parameters.padding.index, + 'algorithm': algorithm.name, + 'mode': parameters.mode.name, + 'padding': parameters.padding.name, }); return data; }