From 2e507eb3251af29010e92743b34452f03ca27936 Mon Sep 17 00:00:00 2001 From: Herbert Poul Date: Tue, 2 Jun 2020 15:29:17 +0200 Subject: [PATCH] copied ffi definitions --- lib/argon2_ffi_base.dart | 116 +++++++++++++++++++++++- pubspec.lock | 155 ++------------------------------- pubspec.yaml | 43 +-------- test/argon2_ffi_base_test.dart | 13 --- 4 files changed, 121 insertions(+), 206 deletions(-) delete mode 100644 test/argon2_ffi_base_test.dart diff --git a/lib/argon2_ffi_base.dart b/lib/argon2_ffi_base.dart index fc2b7d4..14f5cfc 100644 --- a/lib/argon2_ffi_base.dart +++ b/lib/argon2_ffi_base.dart @@ -1,7 +1,115 @@ library argon2_ffi_base; -/// A Calculator. -class Calculator { - /// Returns [value] plus 1. - int addOne(int value) => value + 1; +import 'dart:convert'; +import 'dart:ffi'; + +import 'dart:io'; +import 'dart:typed_data'; + +import 'package:ffi/ffi.dart'; +import 'package:ffi_helper/ffi_helper.dart'; + +typedef Argon2HashNative = Pointer Function( + Pointer key, + Uint32 keyLen, + Pointer salt, + Uint32 saltlen, + Uint32 m_cost, // memory cost + Uint32 t_cost, // time cost (number iterations) + Uint32 parallelism, + IntPtr hashlen, + Uint8 type, + Uint32 version, +); + +typedef Argon2Hash = Pointer Function( + Pointer key, + int keyLen, + Pointer salt, + int saltlen, + int m_cost, // memory cost + int t_cost, // time cost (number iterations) + int parallelism, + int hashlen, + int type, + int version, +); + +class Argon2Ffi { + int Function(int x, int y) _nativeAdd; + Argon2Hash argon2hash; + + Argon2Ffi() { + final argon2lib = Platform.isAndroid + ? DynamicLibrary.open('libargon2_ffi.so') + : Platform.isLinux + ? DynamicLibrary.open('libargon2_ffi_plugin.so') + : DynamicLibrary.executable(); + _nativeAdd = argon2lib + .lookup>('native_add') + .asFunction(); + } + + int addIt(int x, int y) => _nativeAdd(x, y); +} + +abstract class Argon2 { + Uint8List argon2(Argon2Arguments args); + + Future argon2Async(Argon2Arguments args); +} + +class Argon2Arguments { + Argon2Arguments(this.key, this.salt, this.memory, this.iterations, + this.length, this.parallelism, this.type, this.version); + + final Uint8List key; + final Uint8List salt; + final int memory; + final int iterations; + final int length; + final int parallelism; + final int type; + final int version; +} + +abstract class Argon2Base extends Argon2 { +// @protected + Argon2Hash get argon2hash; + + @override + Uint8List argon2(Argon2Arguments args) { + final keyArray = Uint8Array.fromTypedList(args.key); + final saltArray = Uint8Array.fromTypedList(args.salt); +// final saltArray = allocate(count: args.salt.length); +// final saltList = saltArray.asTypedList(args.length); +// saltList.setAll(0, args.salt); +// const memoryCost = 1 << 16; + +// _logger.fine('saltArray: ${ByteUtils.toHexList(saltArray.view)}'); + + final result = argon2hash( + keyArray.rawPtr, + keyArray.length, + saltArray.rawPtr, + saltArray.length, + args.memory, + args.iterations, + args.parallelism, + args.length, + args.type, + args.version, + ); + + keyArray.free(); + saltArray.free(); +// free(saltArray); + final resultString = Utf8.fromUtf8(result); + return base64.decode(resultString); + } + + @override + Future argon2Async(Argon2Arguments args) async { + return argon2(args); + } } diff --git a/pubspec.lock b/pubspec.lock index 3107c6e..a4ab656 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,20 +1,6 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - archive: - dependency: transitive - description: - name: archive - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.13" - args: - dependency: transitive - description: - name: args - url: "https://pub.dartlang.org" - source: hosted - version: "1.6.0" async: dependency: transitive description: @@ -22,20 +8,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.4.1" - boolean_selector: - dependency: transitive - description: - name: boolean_selector - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.0" - charcode: - dependency: transitive - description: - name: charcode - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.3" collection: dependency: transitive description: @@ -43,44 +15,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.14.12" - convert: - dependency: transitive - description: - name: convert - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.1" - crypto: - dependency: transitive - description: - name: crypto - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.4" - flutter: + ffi: dependency: "direct main" - description: flutter - source: sdk - version: "0.0.0" - flutter_test: - dependency: "direct dev" - description: flutter - source: sdk - version: "0.0.0" - image: - dependency: transitive description: - name: image + name: ffi url: "https://pub.dartlang.org" source: hosted - version: "2.1.12" - matcher: - dependency: transitive + version: "0.1.3" + ffi_helper: + dependency: "direct main" description: - name: matcher + name: ffi_helper url: "https://pub.dartlang.org" source: hosted - version: "0.12.6" + version: "1.4.0" meta: dependency: transitive description: @@ -88,94 +36,5 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.1.8" - path: - dependency: transitive - description: - name: path - url: "https://pub.dartlang.org" - source: hosted - version: "1.6.4" - petitparser: - dependency: transitive - description: - name: petitparser - url: "https://pub.dartlang.org" - source: hosted - version: "2.4.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.3" - sky_engine: - dependency: transitive - description: flutter - source: sdk - version: "0.0.99" - source_span: - dependency: transitive - description: - name: source_span - url: "https://pub.dartlang.org" - source: hosted - version: "1.7.0" - stack_trace: - dependency: transitive - description: - name: stack_trace - url: "https://pub.dartlang.org" - source: hosted - version: "1.9.3" - stream_channel: - dependency: transitive - description: - name: stream_channel - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.0" - string_scanner: - dependency: transitive - description: - name: string_scanner - url: "https://pub.dartlang.org" - source: hosted - version: "1.0.5" - term_glyph: - dependency: transitive - description: - name: term_glyph - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.0" - test_api: - dependency: transitive - description: - name: test_api - url: "https://pub.dartlang.org" - source: hosted - version: "0.2.15" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.6" - vector_math: - dependency: transitive - description: - name: vector_math - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.8" - xml: - dependency: transitive - description: - name: xml - url: "https://pub.dartlang.org" - source: hosted - version: "3.6.1" sdks: dart: ">=2.7.0 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 0f778c9..cb38bb1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,46 +8,7 @@ environment: sdk: ">=2.7.0 <3.0.0" dependencies: - flutter: - sdk: flutter + ffi: ^0.1.3 + ffi_helper: ^1.4.0 dev_dependencies: - flutter_test: - sdk: flutter - -# For information on the generic Dart part of this file, see the -# following page: https://dart.dev/tools/pub/pubspec - -# The following section is specific to Flutter. -flutter: - - # To add assets to your package, add an assets section, like this: - # assets: - # - images/a_dot_burr.jpeg - # - images/a_dot_ham.jpeg - # - # For details regarding assets in packages, see - # https://flutter.dev/assets-and-images/#from-packages - # - # An image asset can refer to one or more resolution-specific "variants", see - # https://flutter.dev/assets-and-images/#resolution-aware. - - # To add custom fonts to your package, add a fonts section here, - # in this "flutter" section. Each entry in this list should have a - # "family" key with the font family name, and a "fonts" key with a - # list giving the asset and other descriptors for the font. For - # example: - # fonts: - # - family: Schyler - # fonts: - # - asset: fonts/Schyler-Regular.ttf - # - asset: fonts/Schyler-Italic.ttf - # style: italic - # - family: Trajan Pro - # fonts: - # - asset: fonts/TrajanPro.ttf - # - asset: fonts/TrajanPro_Bold.ttf - # weight: 700 - # - # For details regarding fonts in packages, see - # https://flutter.dev/custom-fonts/#from-packages diff --git a/test/argon2_ffi_base_test.dart b/test/argon2_ffi_base_test.dart deleted file mode 100644 index 457208f..0000000 --- a/test/argon2_ffi_base_test.dart +++ /dev/null @@ -1,13 +0,0 @@ -import 'package:flutter_test/flutter_test.dart'; - -import 'package:argon2_ffi_base/argon2_ffi_base.dart'; - -void main() { - test('adds one to input values', () { - final calculator = Calculator(); - expect(calculator.addOne(2), 3); - expect(calculator.addOne(-7), -6); - expect(calculator.addOne(0), 1); - expect(() => calculator.addOne(null), throwsNoSuchMethodError); - }); -}