From 98c0dce1de5d2629f2fd87a83cf05e2994cee986 Mon Sep 17 00:00:00 2001 From: Pointcheval Hugo Date: Thu, 17 Dec 2020 22:09:55 +0100 Subject: [PATCH] Add new platform layer --- lib/src/platform.dart | 114 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 lib/src/platform.dart diff --git a/lib/src/platform.dart b/lib/src/platform.dart new file mode 100644 index 0000000..c423847 --- /dev/null +++ b/lib/src/platform.dart @@ -0,0 +1,114 @@ +// Copyright (c) 2020 +// Author: Hugo Pointcheval + +import 'dart:typed_data'; + +import 'package:flutter/services.dart'; + +import 'cipher.dart'; + +/// Represents a platform, and is usefull to calling +/// methods from a specific platform. +class Platform { + /// Contains the channel for platform specific code. + static const MethodChannel _channel = const MethodChannel('native.crypto'); + + /// Calls native code. + static Future call(String method, [Map arguments]) { + return _channel.invokeMethod(method, arguments); + } + + /// Calls native PBKDF2. + /// + /// Takes password and salt as parameters. + /// And optionnally keyLength in bytes, number of iterations and hash algorithm. + /// + /// Returns a key as [Uint8List]. + Future pbkdf2( + String password, + String salt, { + int keyLength: 32, + int iteration: 10000, + String algorithm: 'sha256', + }) async { + final Uint8List key = await call('pbkdf2', { + 'password': password, + 'salt': salt, + 'keyLength': keyLength, + 'iteration': iteration, + 'algorithm': algorithm, + }); + return key; + } + + /// Generates a random key. + /// + /// Takes size in bits. + /// + /// Returns a key as [Uint8List]. + Future keygen(int size) async { + final Uint8List key = await call('keygen', { + 'size': size, + }); + + return key; + } + + /// Generates an RSA key pair. + /// + /// Takes size in bits. + /// + /// Returns a key pair as list of [Uint8List], the public key is the + /// first element, and the private is the last. + Future> rsaKeypairGen(int size) async { + final List keypair = + await call('rsaKeypairGen', { + 'size': size, + }); + + return keypair; + } + + /// Encrypts data with a secret key and algorithm. + /// + /// Takes data, key, algorithm, mode and padding as parameters. + /// + /// Encrypts data and returns cipher text + /// and IV as a list of [Uint8List]. + Future> encrypt( + Uint8List data, + Uint8List key, + String algorithm, + CipherParameters parameters, + ) async { + final List payload = await call('encrypt', { + 'data': data, + 'key': key, + 'algorithm': algorithm, + 'mode': parameters.mode.index, + 'padding': parameters.padding.index, + }); + return payload; + } + + /// Decrypts a payload with a secret key and algorithm. + /// + /// The payload must be a list of `Uint8List` + /// with encrypted cipher as first and IV as second member. + Future decrypt( + List payload, + Uint8List key, + String 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, + }); + return data; + } +}