├── .gitignore ├── .metadata ├── ACKNOWLEDGEMENTS.md ├── CHANGELOG.md ├── LICENSE ├── README.md ├── android ├── .gitignore ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ └── gradle-wrapper.properties ├── settings.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── firstfloorsoftware │ │ └── flutter_sodium │ │ └── FlutterSodiumPlugin.java │ └── jniLibs │ ├── arm64-v8a │ └── libsodium.so │ ├── armeabi-v7a │ └── libsodium.so │ ├── x86 │ └── libsodium.so │ └── x86_64 │ └── libsodium.so ├── example ├── .gitignore ├── .metadata ├── README.md ├── android │ ├── .gitignore │ ├── app │ │ ├── build.gradle │ │ └── src │ │ │ ├── debug │ │ │ └── AndroidManifest.xml │ │ │ ├── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── kotlin │ │ │ │ └── com │ │ │ │ │ └── firstfloorsoftware │ │ │ │ │ └── flutter_sodium_example │ │ │ │ │ └── MainActivity.kt │ │ │ └── res │ │ │ │ ├── drawable │ │ │ │ └── launch_background.xml │ │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ │ └── values │ │ │ │ └── styles.xml │ │ │ └── profile │ │ │ └── AndroidManifest.xml │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ └── settings.gradle ├── assets │ ├── fonts │ │ └── RobotoMono-Regular.ttf │ └── screenshots │ │ └── screenshot1.png ├── example.md ├── flutter_sodium_example.iml ├── ios │ ├── .gitignore │ ├── Flutter │ │ ├── AppFrameworkInfo.plist │ │ ├── Debug.xcconfig │ │ └── Release.xcconfig │ ├── Podfile │ ├── Podfile.lock │ ├── Runner.xcodeproj │ │ ├── project.pbxproj │ │ ├── project.xcworkspace │ │ │ └── contents.xcworkspacedata │ │ └── xcshareddata │ │ │ └── xcschemes │ │ │ └── Runner.xcscheme │ ├── Runner.xcworkspace │ │ └── contents.xcworkspacedata │ └── Runner │ │ ├── AppDelegate.swift │ │ ├── Assets.xcassets │ │ ├── AppIcon.appiconset │ │ │ ├── Contents.json │ │ │ ├── Icon-App-1024x1024@1x.png │ │ │ ├── Icon-App-20x20@1x.png │ │ │ ├── Icon-App-20x20@2x.png │ │ │ ├── Icon-App-20x20@3x.png │ │ │ ├── Icon-App-29x29@1x.png │ │ │ ├── Icon-App-29x29@2x.png │ │ │ ├── Icon-App-29x29@3x.png │ │ │ ├── Icon-App-40x40@1x.png │ │ │ ├── Icon-App-40x40@2x.png │ │ │ ├── Icon-App-40x40@3x.png │ │ │ ├── Icon-App-60x60@2x.png │ │ │ ├── Icon-App-60x60@3x.png │ │ │ ├── Icon-App-76x76@1x.png │ │ │ ├── Icon-App-76x76@2x.png │ │ │ └── Icon-App-83.5x83.5@2x.png │ │ └── LaunchImage.imageset │ │ │ ├── Contents.json │ │ │ ├── LaunchImage.png │ │ │ ├── LaunchImage@2x.png │ │ │ ├── LaunchImage@3x.png │ │ │ └── README.md │ │ ├── Base.lproj │ │ ├── LaunchScreen.storyboard │ │ └── Main.storyboard │ │ ├── Info.plist │ │ └── Runner-Bridging-Header.h ├── lib │ ├── main.dart │ ├── sample_widget.dart │ ├── samples.dart │ ├── toc.dart │ └── topic_page.dart ├── macos │ └── Podfile ├── pubspec.yaml └── test │ └── widget_test.dart ├── flutter_sodium.iml ├── ios ├── .gitignore ├── Classes │ ├── FlutterSodiumPlugin.h │ └── FlutterSodiumPlugin.m ├── flutter_sodium.podspec └── libsodium.a ├── lib ├── flutter_sodium.dart └── src │ ├── bindings │ ├── crypto_aead_bindings.dart │ ├── crypto_auth_bindings.dart │ ├── crypto_box_bindings.dart │ ├── crypto_core_bindings.dart │ ├── crypto_generichash_bindings.dart │ ├── crypto_hash_bindings.dart │ ├── crypto_kdf_bindings.dart │ ├── crypto_kx_bindings.dart │ ├── crypto_onetimeauth_bindings.dart │ ├── crypto_pwhash_bindings.dart │ ├── crypto_scalarmult_bindings.dart │ ├── crypto_secretbox_bindings.dart │ ├── crypto_secretstream_bindings.dart │ ├── crypto_shorthash_bindings.dart │ ├── crypto_sign_bindings.dart │ ├── crypto_stream_bindings.dart │ ├── libsodium.dart │ ├── randombytes_bindings.dart │ └── sodium_bindings.dart │ ├── cha_cha20_poly1305.dart │ ├── cha_cha20_poly1305_ietf.dart │ ├── crypto_auth.dart │ ├── crypto_box.dart │ ├── crypto_sign.dart │ ├── crypto_stream.dart │ ├── detached_cipher.dart │ ├── extensions.dart │ ├── generic_hash.dart │ ├── hash.dart │ ├── init_push_result.dart │ ├── key_derivation.dart │ ├── key_exchange.dart │ ├── key_pair.dart │ ├── onetime_auth.dart │ ├── password_hash.dart │ ├── pull_result.dart │ ├── random_bytes.dart │ ├── scalar_mult.dart │ ├── sealed_box.dart │ ├── secret_box.dart │ ├── session_keys.dart │ ├── short_hash.dart │ ├── sodium.dart │ ├── sodium_exception.dart │ └── x_cha_cha20_poly1305_ietf.dart ├── pubspec.yaml └── test └── flutter_sodium_test.dart /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .atom/ 3 | .idea 4 | .dart_tool/ 5 | .packages 6 | .pub/ 7 | .vscode/ 8 | build/ 9 | **/ios/.generated/ 10 | **/ios/Flutter/.last_build_id 11 | packages 12 | pubspec.lock 13 | -------------------------------------------------------------------------------- /.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: f139b11009aeb8ed2a3a3aa8b0066e482709dde3 8 | channel: stable 9 | 10 | project_type: plugin 11 | -------------------------------------------------------------------------------- /ACKNOWLEDGEMENTS.md: -------------------------------------------------------------------------------- 1 | ## Acknowledgements 2 | 3 | The flutter_sodium plugin uses code from the following libraries: 4 | 5 | - [libsodium](https://github.com/jedisct1/libsodium), ISC license -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.2.0 - March 12, 2021 2 | * migrates to ffi 1.0.0 3 | * stable null safety 4 | 5 | ## 0.2.0-nullsafety.1 - February 5, 2021 6 | * adds support for sodium_core_hchacha and hsalsa (merge 0.1.11) 7 | 8 | ## 0.2.0-nullsafety.0 - February 5, 2021 9 | * implements null safety 10 | 11 | ## 0.1.11 - February 5, 2021 12 | * adds support for loading libsodium on Linux and Windows 13 | * adds support for sodium_core_hchacha and hsalsa 14 | 15 | ## 0.1.10 - December 11, 2020 16 | * adds support for sodium_runtime_*, sodium_memcmp, sodium_pad and sodium_unpad 17 | 18 | ## 0.1.9 - October 31, 2020 19 | * sets Android build.gradle minSdkVersion 16, fixing implicit permissions READ_PHONE_STATE, READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE 20 | 21 | ## 0.1.8 - October 2, 2020 22 | * backwards incompatible Sodium.cryptoPwhashStr* changes, str return value and parameter type changed from ascii decoded String to null terminated Uint8List 23 | 24 | ## 0.1.7 - September 30, 2020 25 | * improves API documentation 26 | * removes obsolete convert package dependency 27 | * fixes Android deprecated API build warning 28 | 29 | ## 0.1.6 - September 30, 2020 30 | * fixes "cannot find symbol" compile error on Android 31 | 32 | ## 0.1.5 - September 30, 2020 33 | * fixes symbol lookup issue since flutter 1.20 34 | * fixes platforms key in pubspec.yaml 35 | 36 | ## 0.1.4 - September 16, 2020 37 | * adds sodium hex and base64 conversion helpers 38 | * removes sodium prefix from version and init functions (breaks API) 39 | * fixes generic_hash crash on Android 40 | 41 | ## 0.1.3 - July 17, 2020 42 | * reverts invalid multi-platform pubspec settings 43 | 44 | ## 0.1.2 - July 16, 2020 45 | * fixes documentation and multi-platform support warnings 46 | 47 | ## 0.1.1 - July 15, 2020 48 | * fixes "Failed to lookup symbol" errors on iOS in release mode. 49 | 50 | ## 0.1.0 - June 10, 2020 51 | * rewrite flutter_sodium using FFI 52 | 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | // Copyright 2020 First Floor Software. All rights reserved. 2 | // 3 | // Redistribution and use in source and binary forms, with or without 4 | // modification, are permitted provided that the following conditions are 5 | // met: 6 | // 7 | // * Redistributions of source code must retain the above copyright 8 | // notice, this list of conditions and the following disclaimer. 9 | // * Redistributions in binary form must reproduce the above 10 | // copyright notice, this list of conditions and the following disclaimer 11 | // in the documentation and/or other materials provided with the 12 | // distribution. 13 | // * Neither the name of First Floor Software nor the names of its 14 | // contributors may be used to endorse or promote products derived from 15 | // this software without specific prior written permission. 16 | // 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # flutter_sodium 2 | 3 | With flutter_sodium you get access to the modern, easy-to-use [libsodium](https://download.libsodium.org/doc/) crypto library in your [Flutter](https://flutter.io) apps. One set of crypto APIs supporting both Android and iOS. 4 | 5 | [![Pub](https://img.shields.io/pub/v/flutter_sodium.svg)](https://pub.dartlang.org/packages/flutter_sodium) 6 | 7 | ## Getting Started 8 | 9 | In your flutter project add the dependency: 10 | 11 | ```yml 12 | dependencies: 13 | ... 14 | flutter_sodium: ^0.2.0 15 | ``` 16 | 17 | Import the plugin and initialize it. Sodium.init() initializes the plugin and should be called before any other function provided by flutter_sodium. 18 | 19 | ```dart 20 | import 'package:flutter_sodium/flutter_sodium.dart'; 21 | 22 | // initialize sodium 23 | Sodium.init(); 24 | ``` 25 | 26 | ## Usage example 27 | 28 | ```dart 29 | // Password hashing (using Argon) 30 | final password = 'my password'; 31 | final str = PasswordHash.hashStringStorage(password); 32 | 33 | print(str); 34 | 35 | // verify hash str 36 | final valid = PasswordHash.verifyStorage(str, password); 37 | 38 | assert(valid); 39 | ``` 40 | 41 | This project includes an extensive example app with runnable code samples. Be sure to check it out! 42 | 43 | 44 | 45 | ## API coverage 46 | The flutter_sodium plugin implements the following libsodium APIs: 47 | - crypto_aead 48 | - crypto_auth 49 | - crypto_box 50 | - crypto_generichash 51 | - crypto_hash 52 | - crypto_kdf 53 | - crypto_kx 54 | - crypto_onetimeauth 55 | - crypto_pwhash 56 | - crypto_scalarmult 57 | - crypto_secretbox 58 | - crypto_secretstream 59 | - crypto_shorthash 60 | - crypto_sign 61 | - crypto_stream 62 | - randombytes 63 | - sodium_version 64 | 65 | API coverage is not 100% complete, track the progress in [issue #61](https://github.com/firstfloorsoftware/flutter_sodium/issues/61) 66 | 67 | ## Dart APIs 68 | The plugin includes a core API that maps native libsodium functions 1:1 to Dart equivalents. The core API is available in the class [`Sodium`](https://github.com/firstfloorsoftware/flutter_sodium/blob/master/lib/flutter_sodium.dart). Dart naming conventions are used for core API function names. A native libsodium function such as `crypto_pwhash_str`, is available in flutter as `Sodium.cryptoPwhashStr`. 69 | 70 | Also included in flutter_sodium is a high-level, opinionated API providing access to libsodium in a Dart friendly manner. The various functions are available in separate Dart classes. Password hashing for example is available in the `PasswordHash` class. The high-level API depends on the core API to get things done. 71 | 72 | ## Migrating to fluttter_sodium FFI 73 | The FFI implementation of flutter_sodium is backwards incompatible with the previous platform channel implementation. The list of changes: 74 | - the entire FFI API is now synchronous, while the previous implementation was entirely asynchronous 75 | - all hardcoded libsodium constants are now available as properties on the Sodium class. 76 | - in the platform channel versions the Android and iOS implementations were not in sync. Some functions were available only in iOS, others only in Android. With the FFI implementation, there is a single API covering both platforms. 77 | 78 | ## Background threads 79 | Since the entire FFI API is synchronous, you'll need to do some extra work to execute long running crypto function on a background thread. Luckily this is very easy with Flutter's [compute function](https://api.flutter.dev/flutter/foundation/compute.html). 80 | 81 | The following code snippet demonstrates running a password hash on the background thread. 82 | 83 | ```dart 84 | final pw = 'hello world'; 85 | final str = await compute(PasswordHash.hashStringStorageModerate, pw); 86 | 87 | print(str); 88 | ``` 89 | -------------------------------------------------------------------------------- /android/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /android/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | android { 3 | compileSdkVersion 28 4 | defaultConfig { 5 | minSdkVersion 16 6 | } 7 | } 8 | 9 | dependencies { 10 | } 11 | -------------------------------------------------------------------------------- /android/gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.jvmargs=-Xmx1536M 2 | android.enableR8=true 3 | android.useAndroidX=true 4 | android.enableJetifier=true 5 | -------------------------------------------------------------------------------- /android/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | zipStoreBase=GRADLE_USER_HOME 4 | zipStorePath=wrapper/dists 5 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip 6 | -------------------------------------------------------------------------------- /android/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'flutter_sodium' 2 | -------------------------------------------------------------------------------- /android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | -------------------------------------------------------------------------------- /android/src/main/java/com/firstfloorsoftware/flutter_sodium/FlutterSodiumPlugin.java: -------------------------------------------------------------------------------- 1 | package com.firstfloorsoftware.flutter_sodium; 2 | 3 | import androidx.annotation.NonNull; 4 | 5 | import io.flutter.embedding.engine.plugins.FlutterPlugin; 6 | import io.flutter.plugin.common.MethodCall; 7 | import io.flutter.plugin.common.MethodChannel; 8 | import io.flutter.plugin.common.MethodChannel.MethodCallHandler; 9 | import io.flutter.plugin.common.MethodChannel.Result; 10 | import io.flutter.plugin.common.PluginRegistry.Registrar; 11 | 12 | /** FlutterSodiumPlugin */ 13 | public class FlutterSodiumPlugin implements FlutterPlugin, MethodCallHandler { 14 | /// The MethodChannel that will the communication between Flutter and native Android 15 | /// 16 | /// This local reference serves to register the plugin with the Flutter Engine and unregister it 17 | /// when the Flutter Engine is detached from the Activity 18 | private MethodChannel channel; 19 | 20 | @Override 21 | public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { 22 | channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "sodium"); 23 | channel.setMethodCallHandler(this); 24 | } 25 | 26 | // This static function is optional and equivalent to onAttachedToEngine. It supports the old 27 | // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting 28 | // plugin registration via this function while apps migrate to use the new Android APIs 29 | // post-flutter-1.12 via https://flutter.dev/go/android-project-migration. 30 | // 31 | // It is encouraged to share logic between onAttachedToEngine and registerWith to keep 32 | // them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called 33 | // depending on the user's project. onAttachedToEngine or registerWith must both be defined 34 | // in the same class. 35 | public static void registerWith(Registrar registrar) { 36 | final MethodChannel channel = new MethodChannel(registrar.messenger(), "sodium"); 37 | channel.setMethodCallHandler(new FlutterSodiumPlugin()); 38 | } 39 | 40 | @Override 41 | public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { 42 | if (call.method.equals("getPlatformVersion")) { 43 | result.success("Android " + android.os.Build.VERSION.RELEASE); 44 | } else { 45 | result.notImplemented(); 46 | } 47 | } 48 | 49 | @Override 50 | public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { 51 | channel.setMethodCallHandler(null); 52 | } 53 | } -------------------------------------------------------------------------------- /android/src/main/jniLibs/arm64-v8a/libsodium.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/android/src/main/jniLibs/arm64-v8a/libsodium.so -------------------------------------------------------------------------------- /android/src/main/jniLibs/armeabi-v7a/libsodium.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/android/src/main/jniLibs/armeabi-v7a/libsodium.so -------------------------------------------------------------------------------- /android/src/main/jniLibs/x86/libsodium.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/android/src/main/jniLibs/x86/libsodium.so -------------------------------------------------------------------------------- /android/src/main/jniLibs/x86_64/libsodium.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/android/src/main/jniLibs/x86_64/libsodium.so -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # Miscellaneous 2 | *.class 3 | *.log 4 | *.pyc 5 | *.swp 6 | .DS_Store 7 | .atom/ 8 | .buildlog/ 9 | .history 10 | .svn/ 11 | 12 | # IntelliJ related 13 | *.iml 14 | *.ipr 15 | *.iws 16 | .idea/ 17 | 18 | # The .vscode folder contains launch configuration and tasks you configure in 19 | # VS Code which you may wish to be included in version control, so this line 20 | # is commented out by default. 21 | #.vscode/ 22 | 23 | # Flutter/Dart/Pub related 24 | **/doc/api/ 25 | .dart_tool/ 26 | .flutter-plugins 27 | .flutter-plugins-dependencies 28 | .packages 29 | .pub-cache/ 30 | .pub/ 31 | /build/ 32 | 33 | # Web related 34 | lib/generated_plugin_registrant.dart 35 | 36 | # Exceptions to above rules. 37 | !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages 38 | -------------------------------------------------------------------------------- /example/.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: f139b11009aeb8ed2a3a3aa8b0066e482709dde3 8 | channel: stable 9 | 10 | project_type: app 11 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # flutter_sodium_example 2 | 3 | Demonstrates how to use the flutter_sodium plugin. 4 | -------------------------------------------------------------------------------- /example/android/.gitignore: -------------------------------------------------------------------------------- 1 | gradle-wrapper.jar 2 | /.gradle 3 | /captures/ 4 | /gradlew 5 | /gradlew.bat 6 | /local.properties 7 | GeneratedPluginRegistrant.java 8 | -------------------------------------------------------------------------------- /example/android/app/build.gradle: -------------------------------------------------------------------------------- 1 | def localProperties = new Properties() 2 | def localPropertiesFile = rootProject.file('local.properties') 3 | if (localPropertiesFile.exists()) { 4 | localPropertiesFile.withReader('UTF-8') { reader -> 5 | localProperties.load(reader) 6 | } 7 | } 8 | 9 | def flutterRoot = localProperties.getProperty('flutter.sdk') 10 | if (flutterRoot == null) { 11 | throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") 12 | } 13 | 14 | def flutterVersionCode = localProperties.getProperty('flutter.versionCode') 15 | if (flutterVersionCode == null) { 16 | flutterVersionCode = '1' 17 | } 18 | 19 | def flutterVersionName = localProperties.getProperty('flutter.versionName') 20 | if (flutterVersionName == null) { 21 | flutterVersionName = '1.0' 22 | } 23 | 24 | apply plugin: 'com.android.application' 25 | apply plugin: 'kotlin-android' 26 | apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" 27 | 28 | android { 29 | compileSdkVersion 28 30 | 31 | sourceSets { 32 | main.java.srcDirs += 'src/main/kotlin' 33 | } 34 | 35 | lintOptions { 36 | disable 'InvalidPackage' 37 | } 38 | 39 | defaultConfig { 40 | // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). 41 | applicationId "com.firstfloorsoftware.flutter_sodium_example" 42 | minSdkVersion 16 43 | targetSdkVersion 28 44 | versionCode flutterVersionCode.toInteger() 45 | versionName flutterVersionName 46 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 47 | } 48 | 49 | buildTypes { 50 | release { 51 | // TODO: Add your own signing config for the release build. 52 | // Signing with the debug keys for now, so `flutter run --release` works. 53 | signingConfig signingConfigs.debug 54 | } 55 | } 56 | } 57 | 58 | flutter { 59 | source '../..' 60 | } 61 | 62 | dependencies { 63 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 64 | testImplementation 'junit:junit:4.12' 65 | androidTestImplementation 'androidx.test:runner:1.1.1' 66 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 67 | } 68 | -------------------------------------------------------------------------------- /example/android/app/src/debug/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/android/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 8 | 12 | 19 | 20 | 21 | 22 | 23 | 24 | 26 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /example/android/app/src/main/kotlin/com/firstfloorsoftware/flutter_sodium_example/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.firstfloorsoftware.flutter_sodium_example 2 | 3 | import androidx.annotation.NonNull; 4 | import io.flutter.embedding.android.FlutterActivity 5 | import io.flutter.embedding.engine.FlutterEngine 6 | import io.flutter.plugins.GeneratedPluginRegistrant 7 | 8 | class MainActivity: FlutterActivity() { 9 | override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { 10 | GeneratedPluginRegistrant.registerWith(flutterEngine); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/drawable/launch_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | -------------------------------------------------------------------------------- /example/android/app/src/profile/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/android/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.3.50' 3 | repositories { 4 | google() 5 | jcenter() 6 | } 7 | 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:3.5.0' 10 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 11 | } 12 | } 13 | 14 | allprojects { 15 | repositories { 16 | google() 17 | jcenter() 18 | } 19 | } 20 | 21 | rootProject.buildDir = '../build' 22 | subprojects { 23 | project.buildDir = "${rootProject.buildDir}/${project.name}" 24 | } 25 | subprojects { 26 | project.evaluationDependsOn(':app') 27 | } 28 | 29 | task clean(type: Delete) { 30 | delete rootProject.buildDir 31 | } 32 | -------------------------------------------------------------------------------- /example/android/gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.jvmargs=-Xmx1536M 2 | android.enableR8=true 3 | android.useAndroidX=true 4 | android.enableJetifier=true 5 | -------------------------------------------------------------------------------- /example/android/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/android/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /example/android/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jun 23 08:50:38 CEST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip 7 | -------------------------------------------------------------------------------- /example/android/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /example/android/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /example/android/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | 3 | def flutterProjectRoot = rootProject.projectDir.parentFile.toPath() 4 | 5 | def plugins = new Properties() 6 | def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins') 7 | if (pluginsFile.exists()) { 8 | pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) } 9 | } 10 | 11 | plugins.each { name, path -> 12 | def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile() 13 | include ":$name" 14 | project(":$name").projectDir = pluginDirectory 15 | } 16 | -------------------------------------------------------------------------------- /example/assets/fonts/RobotoMono-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/assets/fonts/RobotoMono-Regular.ttf -------------------------------------------------------------------------------- /example/assets/screenshots/screenshot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/assets/screenshots/screenshot1.png -------------------------------------------------------------------------------- /example/example.md: -------------------------------------------------------------------------------- 1 | ```dart 2 | import 'package:flutter_sodium/flutter_sodium.dart'; 3 | 4 | // initialize sodium (one-time) 5 | Sodium.init(); 6 | 7 | // Password hashing (using Argon) 8 | final password = 'my password'; 9 | final str = PasswordHash.hashStringStorage(password); 10 | 11 | print(str); 12 | 13 | // verify hash str 14 | final valid = PasswordHash.verifyStorage(str, password); 15 | 16 | assert(valid); 17 | ``` 18 | -------------------------------------------------------------------------------- /example/flutter_sodium_example.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /example/ios/.gitignore: -------------------------------------------------------------------------------- 1 | *.mode1v3 2 | *.mode2v3 3 | *.moved-aside 4 | *.pbxuser 5 | *.perspectivev3 6 | **/*sync/ 7 | .sconsign.dblite 8 | .tags* 9 | **/.vagrant/ 10 | **/DerivedData/ 11 | Icon? 12 | **/Pods/ 13 | **/.symlinks/ 14 | profile 15 | xcuserdata 16 | **/.generated/ 17 | Flutter/App.framework 18 | Flutter/Flutter.framework 19 | Flutter/Flutter.podspec 20 | Flutter/Generated.xcconfig 21 | Flutter/app.flx 22 | Flutter/app.zip 23 | Flutter/flutter_assets/ 24 | Flutter/flutter_export_environment.sh 25 | ServiceDefinitions.json 26 | Runner/GeneratedPluginRegistrant.* 27 | 28 | # Exceptions to above rules. 29 | !default.mode1v3 30 | !default.mode2v3 31 | !default.pbxuser 32 | !default.perspectivev3 33 | -------------------------------------------------------------------------------- /example/ios/Flutter/AppFrameworkInfo.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | App 9 | CFBundleIdentifier 10 | io.flutter.flutter.app 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | App 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1.0 23 | MinimumOSVersion 24 | 8.0 25 | 26 | 27 | -------------------------------------------------------------------------------- /example/ios/Flutter/Debug.xcconfig: -------------------------------------------------------------------------------- 1 | #include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" 2 | #include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" 3 | #include "Generated.xcconfig" 4 | -------------------------------------------------------------------------------- /example/ios/Flutter/Release.xcconfig: -------------------------------------------------------------------------------- 1 | #include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" 2 | #include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" 3 | #include "Generated.xcconfig" 4 | -------------------------------------------------------------------------------- /example/ios/Podfile: -------------------------------------------------------------------------------- 1 | # Uncomment this line to define a global platform for your project 2 | # platform :ios, '9.0' 3 | 4 | # CocoaPods analytics sends network stats synchronously affecting flutter build latency. 5 | ENV['COCOAPODS_DISABLE_STATS'] = 'true' 6 | 7 | project 'Runner', { 8 | 'Debug' => :debug, 9 | 'Profile' => :release, 10 | 'Release' => :release, 11 | } 12 | 13 | def flutter_root 14 | generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__) 15 | unless File.exist?(generated_xcode_build_settings_path) 16 | raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first" 17 | end 18 | 19 | File.foreach(generated_xcode_build_settings_path) do |line| 20 | matches = line.match(/FLUTTER_ROOT\=(.*)/) 21 | return matches[1].strip if matches 22 | end 23 | raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get" 24 | end 25 | 26 | require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) 27 | 28 | flutter_ios_podfile_setup 29 | 30 | target 'Runner' do 31 | use_frameworks! 32 | use_modular_headers! 33 | 34 | flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__)) 35 | end 36 | 37 | post_install do |installer| 38 | installer.pods_project.targets.each do |target| 39 | flutter_additional_ios_build_settings(target) 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /example/ios/Podfile.lock: -------------------------------------------------------------------------------- 1 | PODS: 2 | - Flutter (1.0.0) 3 | - flutter_sodium (0.0.1): 4 | - Flutter 5 | - url_launcher (0.0.1): 6 | - Flutter 7 | 8 | DEPENDENCIES: 9 | - Flutter (from `Flutter`) 10 | - flutter_sodium (from `.symlinks/plugins/flutter_sodium/ios`) 11 | - url_launcher (from `.symlinks/plugins/url_launcher/ios`) 12 | 13 | EXTERNAL SOURCES: 14 | Flutter: 15 | :path: Flutter 16 | flutter_sodium: 17 | :path: ".symlinks/plugins/flutter_sodium/ios" 18 | url_launcher: 19 | :path: ".symlinks/plugins/url_launcher/ios" 20 | 21 | SPEC CHECKSUMS: 22 | Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c 23 | flutter_sodium: c84426b4de738514b5b66cfdeb8a06634e72fe0b 24 | url_launcher: 6fef411d543ceb26efce54b05a0a40bfd74cbbef 25 | 26 | PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c 27 | 28 | COCOAPODS: 1.10.1 29 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /example/ios/Runner.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /example/ios/Runner/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import Flutter 3 | 4 | @UIApplicationMain 5 | @objc class AppDelegate: FlutterAppDelegate { 6 | override func application( 7 | _ application: UIApplication, 8 | didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? 9 | ) -> Bool { 10 | GeneratedPluginRegistrant.register(with: self) 11 | return super.application(application, didFinishLaunchingWithOptions: launchOptions) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "size" : "20x20", 5 | "idiom" : "iphone", 6 | "filename" : "Icon-App-20x20@2x.png", 7 | "scale" : "2x" 8 | }, 9 | { 10 | "size" : "20x20", 11 | "idiom" : "iphone", 12 | "filename" : "Icon-App-20x20@3x.png", 13 | "scale" : "3x" 14 | }, 15 | { 16 | "size" : "29x29", 17 | "idiom" : "iphone", 18 | "filename" : "Icon-App-29x29@1x.png", 19 | "scale" : "1x" 20 | }, 21 | { 22 | "size" : "29x29", 23 | "idiom" : "iphone", 24 | "filename" : "Icon-App-29x29@2x.png", 25 | "scale" : "2x" 26 | }, 27 | { 28 | "size" : "29x29", 29 | "idiom" : "iphone", 30 | "filename" : "Icon-App-29x29@3x.png", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "size" : "40x40", 35 | "idiom" : "iphone", 36 | "filename" : "Icon-App-40x40@2x.png", 37 | "scale" : "2x" 38 | }, 39 | { 40 | "size" : "40x40", 41 | "idiom" : "iphone", 42 | "filename" : "Icon-App-40x40@3x.png", 43 | "scale" : "3x" 44 | }, 45 | { 46 | "size" : "60x60", 47 | "idiom" : "iphone", 48 | "filename" : "Icon-App-60x60@2x.png", 49 | "scale" : "2x" 50 | }, 51 | { 52 | "size" : "60x60", 53 | "idiom" : "iphone", 54 | "filename" : "Icon-App-60x60@3x.png", 55 | "scale" : "3x" 56 | }, 57 | { 58 | "size" : "20x20", 59 | "idiom" : "ipad", 60 | "filename" : "Icon-App-20x20@1x.png", 61 | "scale" : "1x" 62 | }, 63 | { 64 | "size" : "20x20", 65 | "idiom" : "ipad", 66 | "filename" : "Icon-App-20x20@2x.png", 67 | "scale" : "2x" 68 | }, 69 | { 70 | "size" : "29x29", 71 | "idiom" : "ipad", 72 | "filename" : "Icon-App-29x29@1x.png", 73 | "scale" : "1x" 74 | }, 75 | { 76 | "size" : "29x29", 77 | "idiom" : "ipad", 78 | "filename" : "Icon-App-29x29@2x.png", 79 | "scale" : "2x" 80 | }, 81 | { 82 | "size" : "40x40", 83 | "idiom" : "ipad", 84 | "filename" : "Icon-App-40x40@1x.png", 85 | "scale" : "1x" 86 | }, 87 | { 88 | "size" : "40x40", 89 | "idiom" : "ipad", 90 | "filename" : "Icon-App-40x40@2x.png", 91 | "scale" : "2x" 92 | }, 93 | { 94 | "size" : "76x76", 95 | "idiom" : "ipad", 96 | "filename" : "Icon-App-76x76@1x.png", 97 | "scale" : "1x" 98 | }, 99 | { 100 | "size" : "76x76", 101 | "idiom" : "ipad", 102 | "filename" : "Icon-App-76x76@2x.png", 103 | "scale" : "2x" 104 | }, 105 | { 106 | "size" : "83.5x83.5", 107 | "idiom" : "ipad", 108 | "filename" : "Icon-App-83.5x83.5@2x.png", 109 | "scale" : "2x" 110 | }, 111 | { 112 | "size" : "1024x1024", 113 | "idiom" : "ios-marketing", 114 | "filename" : "Icon-App-1024x1024@1x.png", 115 | "scale" : "1x" 116 | } 117 | ], 118 | "info" : { 119 | "version" : 1, 120 | "author" : "xcode" 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "LaunchImage.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "filename" : "LaunchImage@2x.png", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "universal", 15 | "filename" : "LaunchImage@3x.png", 16 | "scale" : "3x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md: -------------------------------------------------------------------------------- 1 | # Launch Screen Assets 2 | 3 | You can customize the launch screen with your own desired assets by replacing the image files in this directory. 4 | 5 | You can also do it by opening your Flutter project's Xcode project with `open ios/Runner.xcworkspace`, selecting `Runner/Assets.xcassets` in the Project Navigator and dropping in the desired images. -------------------------------------------------------------------------------- /example/ios/Runner/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /example/ios/Runner/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /example/ios/Runner/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | flutter_sodium_example 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | $(FLUTTER_BUILD_NAME) 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(FLUTTER_BUILD_NUMBER) 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UISupportedInterfaceOrientations 30 | 31 | UIInterfaceOrientationPortrait 32 | UIInterfaceOrientationLandscapeLeft 33 | UIInterfaceOrientationLandscapeRight 34 | 35 | UISupportedInterfaceOrientations~ipad 36 | 37 | UIInterfaceOrientationPortrait 38 | UIInterfaceOrientationPortraitUpsideDown 39 | UIInterfaceOrientationLandscapeLeft 40 | UIInterfaceOrientationLandscapeRight 41 | 42 | UIViewControllerBasedStatusBarAppearance 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /example/ios/Runner/Runner-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | #import "GeneratedPluginRegistrant.h" -------------------------------------------------------------------------------- /example/lib/main.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:flutter_sodium/flutter_sodium.dart'; 3 | import 'toc.dart'; 4 | import 'topic_page.dart'; 5 | 6 | void main() { 7 | Sodium.init(); 8 | 9 | runApp(MyApp()); 10 | } 11 | 12 | class MyApp extends StatelessWidget { 13 | @override 14 | Widget build(BuildContext context) { 15 | return MaterialApp( 16 | title: 'flutter_sodium', 17 | theme: ThemeData( 18 | primarySwatch: Colors.blue, 19 | ), 20 | home: HomePage(), 21 | ); 22 | } 23 | } 24 | 25 | class HomePage extends StatelessWidget { 26 | @override 27 | Widget build(BuildContext context) { 28 | return Scaffold( 29 | appBar: AppBar( 30 | title: Text('flutter_sodium'), 31 | ), 32 | body: SafeArea( 33 | child: FutureBuilder( 34 | // build table of contents 35 | future: buildToc(context), 36 | builder: (BuildContext context, 37 | AsyncSnapshot> snapshot) { 38 | if (snapshot.connectionState == ConnectionState.done) { 39 | return snapshot.hasError 40 | ? Text("Build TOC failed\n\n${snapshot.error}") 41 | : ListView(children: [ 42 | if (snapshot.hasData) 43 | for (var topic in snapshot.data!) 44 | if (topic is Section) 45 | ListTile( 46 | title: Text(topic.title, 47 | style: Theme.of(context) 48 | .textTheme 49 | .headline6)) 50 | else 51 | ListTile( 52 | title: Text(topic.title), 53 | trailing: Icon(Icons.arrow_forward_ios, 54 | size: 12.0), 55 | onTap: () => Navigator.push( 56 | context, 57 | MaterialPageRoute( 58 | builder: (context) => 59 | TopicPage(topic)))) 60 | ]); 61 | } 62 | 63 | return Container(); 64 | }))); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /example/lib/sample_widget.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'toc.dart'; 3 | 4 | class SampleWidget extends StatelessWidget { 5 | final Sample sample; 6 | SampleWidget(this.sample); 7 | 8 | @override 9 | Widget build(BuildContext context) { 10 | return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ 11 | Padding( 12 | padding: EdgeInsets.symmetric(vertical: 16.0), 13 | child: Text(sample.title ?? '(no title)', 14 | style: Theme.of(context).textTheme.headline5)), 15 | if (sample.description != null && sample.description!.length > 0) 16 | Padding( 17 | padding: EdgeInsets.only(bottom: 16.0), 18 | child: Text(sample.description!), 19 | ), 20 | CodeBlock(sample.code), 21 | SampleRunner(sample) 22 | ]); 23 | } 24 | } 25 | 26 | class SampleRunner extends StatefulWidget { 27 | final Sample sample; 28 | 29 | SampleRunner(this.sample); 30 | 31 | @override 32 | State createState() => _SampleRunnerState(); 33 | } 34 | 35 | class _SampleRunnerState extends State { 36 | Future? _sampleRun; 37 | 38 | void _runSample() { 39 | setState(() { 40 | _sampleRun = _runSampleHost(); 41 | }); 42 | } 43 | 44 | Future _runSampleHost() async { 45 | final out = StringBuffer(); 46 | 47 | // run sync or async code sample 48 | if (widget.sample.funcAsync != null) { 49 | await widget.sample.funcAsync!((o) => out.writeln(o)); 50 | } else if (widget.sample.func != null) { 51 | widget.sample.func!((o) => out.writeln(o)); 52 | } 53 | return out.toString().trim(); 54 | } 55 | 56 | @override 57 | Widget build(BuildContext context) { 58 | if (_sampleRun == null) { 59 | return Padding( 60 | padding: EdgeInsets.only(top: 16.0), child: RunButton(_runSample)); 61 | } 62 | 63 | return FutureBuilder( 64 | future: _sampleRun, 65 | builder: (BuildContext context, AsyncSnapshot snapshot) => 66 | Column( 67 | crossAxisAlignment: CrossAxisAlignment.start, 68 | children: [ 69 | Padding( 70 | padding: EdgeInsets.symmetric(vertical: 16.0), 71 | child: RunButton( 72 | snapshot.connectionState == ConnectionState.done 73 | ? _runSample 74 | : null)), 75 | Padding( 76 | padding: EdgeInsets.only(bottom: 16.0), 77 | child: Text('Result', 78 | style: Theme.of(context).textTheme.headline6)), 79 | // display progress only for async code snippets 80 | if (widget.sample.funcAsync != null && 81 | snapshot.connectionState != ConnectionState.done) 82 | LinearProgressIndicator(), 83 | AnimatedOpacity( 84 | opacity: snapshot.connectionState == ConnectionState.done 85 | ? 1 86 | : 0, 87 | duration: Duration( 88 | milliseconds: 89 | snapshot.connectionState == ConnectionState.done 90 | ? 150 91 | : 50), 92 | child: CodeBlock( 93 | snapshot.hasError 94 | ? snapshot.error.toString() 95 | : snapshot.data, 96 | color: snapshot.hasError 97 | ? Colors.red.shade200 98 | : Colors.green.shade200)), 99 | ])); 100 | } 101 | } 102 | 103 | class RunButton extends StatelessWidget { 104 | final VoidCallback? onPressed; 105 | 106 | RunButton(this.onPressed); 107 | 108 | @override 109 | Widget build(BuildContext context) { 110 | return ElevatedButton(child: Text('Run'), onPressed: onPressed); 111 | } 112 | } 113 | 114 | class CodeBlock extends StatelessWidget { 115 | final String? _code; 116 | final Color color; 117 | 118 | CodeBlock(this._code, {this.color = Colors.black12}); 119 | 120 | @override 121 | Widget build(BuildContext context) { 122 | return Container( 123 | padding: EdgeInsets.all(10.0), 124 | color: color, 125 | child: Text(_code ?? '(code not found)', 126 | style: TextStyle(fontFamily: 'RobotoMono', fontSize: 12.0))); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /example/lib/toc.dart: -------------------------------------------------------------------------------- 1 | import 'dart:convert'; 2 | import 'package:flutter/material.dart'; 3 | import 'samples.dart'; 4 | 5 | typedef void SampleFunc(Function(Object) print); 6 | typedef Future SampleFuncAsync(Function(Object) print); 7 | 8 | class Section extends Topic { 9 | Section(String title) : super(title); 10 | } 11 | 12 | class Topic { 13 | final String title; 14 | final String? description; 15 | final String? url; 16 | final List? samples; 17 | 18 | Topic(this.title, {this.description, this.url, this.samples}); 19 | } 20 | 21 | class Sample { 22 | final String name; 23 | final SampleFunc? func; 24 | final SampleFuncAsync? funcAsync; 25 | String? title; 26 | String? description; 27 | String? code; 28 | 29 | Sample(this.name, {this.func, this.funcAsync}); 30 | } 31 | 32 | Future> buildToc(BuildContext context) async { 33 | final samples = Samples(); 34 | 35 | final toc = [ 36 | Section('Common'), 37 | Topic('APIs', 38 | description: 39 | 'The flutter_sodium library contains two sets of APIs, a core API and a high-level API. The core API maps native libsodium functions 1:1 to Dart equivalents. The high-level API provides Dart-friendly, opinionated access to libsodium.', 40 | samples: [ 41 | Sample('api1', func: samples.api1), 42 | Sample('api2', func: samples.api2) 43 | ]), 44 | Topic('Random data', 45 | description: 46 | 'Provides a set of functions to generate unpredictable data, suitable for creating secret keys.', 47 | url: 'https://libsodium.gitbook.io/doc/generating_random_data/', 48 | samples: [ 49 | Sample('random1', func: samples.random1), 50 | Sample('random2', func: samples.random2), 51 | Sample('random3', func: samples.random3) 52 | ]), 53 | Topic('Encoding', 54 | description: 'Encode byte sequence to string and vice versa.', 55 | url: 'https://libsodium.gitbook.io/doc/helpers', 56 | samples: [ 57 | Sample('encoding1', func: samples.encoding1), 58 | Sample('encoding2', func: samples.encoding2) 59 | ]), 60 | Topic('Padding', 61 | description: 'Append padding data', 62 | url: 'https://libsodium.gitbook.io/doc/padding', 63 | samples: [Sample('padding1', func: samples.padding1)]), 64 | Topic('About', 65 | description: 'Provides libsodium version, runtime and algorithm info.', 66 | url: 'https://libsodium.gitbook.io/doc/', 67 | samples: [ 68 | Sample('about1', func: samples.about1), 69 | Sample('about2', func: samples.about2), 70 | Sample('about3', func: samples.about3) 71 | ]), 72 | Section('Secret-key cryptography'), 73 | Topic('Authenticated encryption', 74 | description: 'Secret-key encryption and verification', 75 | url: 76 | 'https://libsodium.gitbook.io/doc/secret-key_cryptography/secretbox', 77 | samples: [ 78 | Sample('secret1', func: samples.secret1), 79 | Sample('secret2', func: samples.secret2) 80 | ]), 81 | Topic('Authentication', 82 | description: 83 | 'Computes an authentication tag for a message and a secret key, and provides a way to verify that a given tag is valid for a given message and a key.', 84 | url: 85 | 'https://libsodium.gitbook.io/doc/secret-key_cryptography/secret-key_authentication', 86 | samples: [ 87 | Sample('auth1', func: samples.auth1), 88 | ]), 89 | Topic('Original ChaCha20-Poly1305', 90 | description: 'Authenticated Encryption with Additional Data.', 91 | url: 92 | 'https://libsodium.gitbook.io/doc/secret-key_cryptography/aead/chacha20-poly1305/original_chacha20-poly1305_construction', 93 | samples: [ 94 | Sample('chacha1', func: samples.chacha1), 95 | Sample('chacha2', func: samples.chacha2) 96 | ]), 97 | Topic('IETF ChaCha20-Poly1305', 98 | description: 'Authenticated Encryption with Additional Data', 99 | url: 100 | 'https://libsodium.gitbook.io/doc/secret-key_cryptography/aead/chacha20-poly1305/ietf_chacha20-poly1305_construction', 101 | samples: [ 102 | Sample('chachaietf1', func: samples.chachaietf1), 103 | Sample('chachaietf2', func: samples.chachaietf2) 104 | ]), 105 | Topic('XChaCha20-Poly1305', 106 | description: 'Authenticated Encryption with Additional Data.', 107 | url: 108 | 'https://libsodium.gitbook.io/doc/secret-key_cryptography/aead/chacha20-poly1305/xchacha20-poly1305_construction', 109 | samples: [ 110 | Sample('xchachaietf1', func: samples.xchachaietf1), 111 | Sample('xchachaietf2', func: samples.xchachaietf2) 112 | ]), 113 | Section('Public-key cryptography'), 114 | Topic('Authenticated encryption', 115 | description: 'Public-key authenticated encryption', 116 | url: 117 | 'https://libsodium.gitbook.io/doc/public-key_cryptography/authenticated_encryption', 118 | samples: [ 119 | Sample('box1', func: samples.box1), 120 | Sample('box2', func: samples.box2), 121 | Sample('box3', func: samples.box3), 122 | Sample('box4', func: samples.box4) 123 | ]), 124 | Topic('Public-key signatures', 125 | description: 126 | 'Computes a signature for a message using a secret key, and provides verification using a public key.', 127 | url: 128 | 'https://libsodium.gitbook.io/doc/public-key_cryptography/public-key_signatures', 129 | samples: [ 130 | Sample('sign1', func: samples.sign1), 131 | Sample('sign2', func: samples.sign2), 132 | Sample('sign3', funcAsync: samples.sign3), 133 | Sample('sign4', func: samples.sign4) 134 | ]), 135 | Topic('Sealed boxes', 136 | description: 137 | 'Anonymously send encrypted messages to a recipient given its public key.', 138 | url: 'https://libsodium.gitbook.io/doc/public-key_cryptography/sealed_boxes', 139 | samples: [Sample('box5', func: samples.box5)]), 140 | Section('Hashing'), 141 | Topic('Generic hashing', 142 | description: 143 | 'Computes a fixed-length fingerprint for an arbitrary long message using the BLAKE2b algorithm.', 144 | url: 'https://libsodium.gitbook.io/doc/hashing/generic_hashing', 145 | samples: [ 146 | Sample('generic1', func: samples.generic1), 147 | Sample('generic2', func: samples.generic2), 148 | Sample('generic3', funcAsync: samples.generic3), 149 | Sample('generic4', funcAsync: samples.generic4) 150 | ]), 151 | Topic('Short-input hashing', 152 | description: 'Computes short hashes using the SipHash-2-4 algorithm.', 153 | url: 'https://libsodium.gitbook.io/doc/hashing/short-input_hashing', 154 | samples: [Sample('shorthash1', func: samples.shorthash1)]), 155 | Topic('Password hashing', 156 | description: 157 | 'Provides an Argon2 password hashing scheme implementation.', 158 | url: 159 | 'https://libsodium.gitbook.io/doc/password_hashing/the_argon2i_function', 160 | samples: [ 161 | Sample('pwhash1', func: samples.pwhash1), 162 | Sample('pwhash2', func: samples.pwhash2), 163 | Sample('pwhash3', funcAsync: samples.pwhash3), 164 | ]), 165 | Section('Key functions'), 166 | Topic('Key derivation', 167 | description: 'Derive secret subkeys from a single master key.', 168 | url: 'https://libsodium.gitbook.io/doc/key_derivation/', 169 | samples: [Sample('kdf1', func: samples.kdf1)]), 170 | Topic('Key exchange', 171 | description: 'Securely compute a set of shared keys.', 172 | url: 'https://libsodium.gitbook.io/doc/key_exchange/', 173 | samples: [Sample('kx1', func: samples.kx1)]), 174 | Section('Advanced'), 175 | Topic('SHA-2', 176 | description: 'SHA-512 hash functions', 177 | url: 'https://libsodium.gitbook.io/doc/advanced/sha-2_hash_function', 178 | samples: [Sample('hash1', func: samples.hash1)]), 179 | Topic('Diffie-Hellman', 180 | description: 'Perform scalar multiplication of elliptic curve points', 181 | url: 'https://libsodium.gitbook.io/doc/advanced/scalar_multiplication', 182 | samples: [ 183 | Sample('scalarmult1', funcAsync: samples.scalarmult1) 184 | ]), 185 | Topic('One-time authentication', 186 | description: 'Secret-key single-message authentication using Poly1305', 187 | url: 'https://libsodium.gitbook.io/doc/advanced/poly1305', 188 | samples: [ 189 | Sample('onetime1', func: samples.onetime1), 190 | Sample('onetime2', funcAsync: samples.onetime2) 191 | ]), 192 | Topic('Stream ciphers', 193 | description: 'Generate pseudo-random data from a key', 194 | url: 'https://libsodium.gitbook.io/doc/advanced/stream_ciphers', 195 | samples: [Sample('stream1', func: samples.stream1)]), 196 | Topic('Ed25519 To Curve25519', 197 | description: 198 | 'Ed25519 keys can be converted to X25519 keys, so that the same key pair can be used both for authenticated encryption (crypto_box) and for signatures (crypto_sign).', 199 | url: 'https://download.libsodium.org/doc/advanced/ed25519-curve25519', 200 | samples: [Sample('sign5', func: samples.sign5)]) 201 | ]; 202 | 203 | // load asset samples.dart for code snippets 204 | final src = 205 | await DefaultAssetBundle.of(context).loadString('lib/samples.dart'); 206 | 207 | // iterate all samples in the toc, and parse title, description and code snippet 208 | for (var topic in toc) { 209 | if (topic.samples != null) { 210 | for (var sample in topic.samples!) { 211 | final beginTag = '// BEGIN ${sample.name}:'; 212 | final begin = src.indexOf(beginTag); 213 | assert(begin != -1); 214 | 215 | // parse title 216 | final beginTitle = begin + beginTag.length; 217 | final endTitle = src.indexOf(':', beginTitle); 218 | assert(endTitle != -1); 219 | sample.title = src.substring(beginTitle, endTitle).trim(); 220 | 221 | // parse description 222 | final endDescription = src.indexOf('\n', endTitle); 223 | assert(endDescription != -1); 224 | sample.description = src.substring(endTitle + 1, endDescription).trim(); 225 | 226 | final end = src.indexOf('// END ${sample.name}', endDescription); 227 | assert(end != -1); 228 | 229 | sample.code = _formatCode(src.substring(endDescription, end)); 230 | } 231 | } 232 | } 233 | 234 | return toc; 235 | } 236 | 237 | String _formatCode(String code) { 238 | final result = StringBuffer(); 239 | final lines = LineSplitter.split(code).toList(); 240 | int indent = -1; 241 | for (var i = 0; i < lines.length; i++) { 242 | String line = lines[i]; 243 | // skip empty first and last lines 244 | if (line.trim().length == 0 && (i == 0 || i == lines.length - 1)) { 245 | continue; 246 | } 247 | // determine indent 248 | if (indent == -1) { 249 | for (indent = 0; indent < line.length; indent++) { 250 | if (line[indent] != ' ') { 251 | break; 252 | } 253 | } 254 | } 255 | 256 | // remove indent from line 257 | if (line.startsWith(' ' * indent)) { 258 | line = line.substring(indent); 259 | } 260 | 261 | if (result.isNotEmpty) { 262 | result.writeln(); 263 | } 264 | result.write(line); 265 | } 266 | return result.toString(); 267 | } 268 | -------------------------------------------------------------------------------- /example/lib/topic_page.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:flutter_sodium_example/sample_widget.dart'; 3 | import 'package:url_launcher/url_launcher.dart'; 4 | import 'toc.dart'; 5 | 6 | class TopicPage extends StatelessWidget { 7 | final Topic topic; 8 | 9 | TopicPage(this.topic); 10 | 11 | @override 12 | Widget build(BuildContext context) { 13 | return Scaffold( 14 | appBar: AppBar( 15 | title: Text(topic.title), 16 | ), 17 | body: SafeArea( 18 | child: SingleChildScrollView( 19 | child: Container( 20 | padding: EdgeInsets.all(15.0), 21 | child: Column( 22 | crossAxisAlignment: CrossAxisAlignment.start, 23 | children: [ 24 | // description 25 | if (topic.description != null) 26 | Padding( 27 | padding: EdgeInsets.only(bottom: 16.0), 28 | child: Text(topic.description!)), 29 | // more info button 30 | if (topic.url != null) 31 | Padding( 32 | padding: EdgeInsets.only(bottom: 16.0), 33 | child: InkWell( 34 | child: Text( 35 | 'More information', 36 | style: TextStyle( 37 | color: Theme.of(context).accentColor), 38 | ), 39 | onTap: () => launch(topic.url!)), 40 | ), 41 | // 0..n samples 42 | if (topic.samples != null) 43 | for (var sample in topic.samples!) 44 | Padding( 45 | padding: EdgeInsets.only(bottom: 16.0), 46 | child: SampleWidget(sample)) 47 | ], 48 | ))))); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /example/macos/Podfile: -------------------------------------------------------------------------------- 1 | platform :osx, '10.11' 2 | 3 | # CocoaPods analytics sends network stats synchronously affecting flutter build latency. 4 | ENV['COCOAPODS_DISABLE_STATS'] = 'true' 5 | 6 | project 'Runner', { 7 | 'Debug' => :debug, 8 | 'Profile' => :release, 9 | 'Release' => :release, 10 | } 11 | 12 | def parse_KV_file(file, separator='=') 13 | file_abs_path = File.expand_path(file) 14 | if !File.exists? file_abs_path 15 | return []; 16 | end 17 | pods_ary = [] 18 | skip_line_start_symbols = ["#", "/"] 19 | File.foreach(file_abs_path) { |line| 20 | next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ } 21 | plugin = line.split(pattern=separator) 22 | if plugin.length == 2 23 | podname = plugin[0].strip() 24 | path = plugin[1].strip() 25 | podpath = File.expand_path("#{path}", file_abs_path) 26 | pods_ary.push({:name => podname, :path => podpath}); 27 | else 28 | puts "Invalid plugin specification: #{line}" 29 | end 30 | } 31 | return pods_ary 32 | end 33 | 34 | def pubspec_supports_macos(file) 35 | file_abs_path = File.expand_path(file) 36 | if !File.exists? file_abs_path 37 | return false; 38 | end 39 | File.foreach(file_abs_path) { |line| 40 | return true if line =~ /^\s*macos:/ 41 | } 42 | return false 43 | end 44 | 45 | target 'Runner' do 46 | use_frameworks! 47 | use_modular_headers! 48 | 49 | # Prepare symlinks folder. We use symlinks to avoid having Podfile.lock 50 | # referring to absolute paths on developers' machines. 51 | ephemeral_dir = File.join('Flutter', 'ephemeral') 52 | symlink_dir = File.join(ephemeral_dir, '.symlinks') 53 | symlink_plugins_dir = File.join(symlink_dir, 'plugins') 54 | system("rm -rf #{symlink_dir}") 55 | system("mkdir -p #{symlink_plugins_dir}") 56 | 57 | # Flutter Pods 58 | generated_xcconfig = parse_KV_file(File.join(ephemeral_dir, 'Flutter-Generated.xcconfig')) 59 | if generated_xcconfig.empty? 60 | puts "Flutter-Generated.xcconfig must exist. If you're running pod install manually, make sure flutter packages get is executed first." 61 | end 62 | generated_xcconfig.map { |p| 63 | if p[:name] == 'FLUTTER_FRAMEWORK_DIR' 64 | symlink = File.join(symlink_dir, 'flutter') 65 | File.symlink(File.dirname(p[:path]), symlink) 66 | pod 'FlutterMacOS', :path => File.join(symlink, File.basename(p[:path])) 67 | end 68 | } 69 | 70 | # Plugin Pods 71 | plugin_pods = parse_KV_file('../.flutter-plugins') 72 | plugin_pods.map { |p| 73 | symlink = File.join(symlink_plugins_dir, p[:name]) 74 | File.symlink(p[:path], symlink) 75 | if pubspec_supports_macos(File.join(symlink, 'pubspec.yaml')) 76 | pod p[:name], :path => File.join(symlink, 'macos') 77 | end 78 | } 79 | end 80 | 81 | # Prevent Cocoapods from embedding a second Flutter framework and causing an error with the new Xcode build system. 82 | install! 'cocoapods', :disable_input_output_paths => true 83 | -------------------------------------------------------------------------------- /example/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: flutter_sodium_example 2 | version: 2.0.0 3 | description: Demonstrates how to use the flutter_sodium plugin. 4 | publish_to: 'none' 5 | 6 | environment: 7 | sdk: ">=2.12.0 <3.0.0" 8 | 9 | dependencies: 10 | flutter: 11 | sdk: flutter 12 | url_launcher: ^6.0.2 13 | 14 | dev_dependencies: 15 | flutter_test: 16 | sdk: flutter 17 | 18 | flutter_sodium: 19 | path: ../ 20 | 21 | flutter: 22 | uses-material-design: true 23 | assets: 24 | - lib/samples.dart 25 | fonts: 26 | - family: RobotoMono 27 | fonts: 28 | - asset: assets/fonts/RobotoMono-Regular.ttf -------------------------------------------------------------------------------- /example/test/widget_test.dart: -------------------------------------------------------------------------------- 1 | // This is a basic Flutter widget test. 2 | // 3 | // To perform an interaction with a widget in your test, use the WidgetTester 4 | // utility that Flutter provides. For example, you can send tap and scroll 5 | // gestures. You can also use WidgetTester to find child widgets in the widget 6 | // tree, read text, and verify that the values of widget properties are correct. 7 | 8 | void main() {} 9 | -------------------------------------------------------------------------------- /flutter_sodium.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /ios/.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .vagrant/ 3 | .sconsign.dblite 4 | .svn/ 5 | 6 | .DS_Store 7 | *.swp 8 | profile 9 | 10 | DerivedData/ 11 | build/ 12 | GeneratedPluginRegistrant.h 13 | GeneratedPluginRegistrant.m 14 | 15 | .generated/ 16 | 17 | *.pbxuser 18 | *.mode1v3 19 | *.mode2v3 20 | *.perspectivev3 21 | 22 | !default.pbxuser 23 | !default.mode1v3 24 | !default.mode2v3 25 | !default.perspectivev3 26 | 27 | xcuserdata 28 | 29 | *.moved-aside 30 | 31 | *.pyc 32 | *sync/ 33 | Icon? 34 | .tags* 35 | 36 | /Flutter/Generated.xcconfig 37 | /Flutter/flutter_export_environment.sh -------------------------------------------------------------------------------- /ios/Classes/FlutterSodiumPlugin.h: -------------------------------------------------------------------------------- 1 | #import 2 | 3 | @interface FlutterSodiumPlugin : NSObject 4 | @end -------------------------------------------------------------------------------- /ios/Classes/FlutterSodiumPlugin.m: -------------------------------------------------------------------------------- 1 | #import "FlutterSodiumPlugin.h" 2 | 3 | @implementation FlutterSodiumPlugin 4 | + (void)registerWithRegistrar:(NSObject*)registrar { 5 | } 6 | @end -------------------------------------------------------------------------------- /ios/flutter_sodium.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html. 3 | # Run `pod lib lint flutter_sodium.podspec' to validate before publishing. 4 | # 5 | Pod::Spec.new do |s| 6 | s.name = 'flutter_sodium' 7 | s.version = '0.0.1' 8 | s.summary = 'A new flutter plugin project.' 9 | s.description = <<-DESC 10 | A new flutter plugin project. 11 | DESC 12 | s.homepage = 'http://example.com' 13 | s.license = { :file => '../LICENSE' } 14 | s.author = { 'Your Company' => 'email@example.com' } 15 | s.source = { :path => '.' } 16 | s.public_header_files = 'Classes**/*.h' 17 | s.source_files = 'Classes/**/*' 18 | s.vendored_libraries = "**/*.a" 19 | s.dependency 'Flutter' 20 | s.platform = :ios, '8.0' 21 | 22 | # Flutter.framework does not contain a i386 slice. Only x86_64 simulators are supported. 23 | s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'VALID_ARCHS[sdk=iphonesimulator*]' => 'x86_64' } 24 | s.swift_version = '5.0' 25 | s.xcconfig = { 'OTHER_LDFLAGS' => '-force_load "${PODS_ROOT}/../.symlinks/plugins/flutter_sodium/ios/libsodium.a"'} 26 | end 27 | -------------------------------------------------------------------------------- /ios/libsodium.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/73171341a43ebb9041b15c6fe43e620d0e281d93/ios/libsodium.a -------------------------------------------------------------------------------- /lib/flutter_sodium.dart: -------------------------------------------------------------------------------- 1 | library flutter_sodium; 2 | 3 | export 'src/cha_cha20_poly1305_ietf.dart'; 4 | export 'src/cha_cha20_poly1305.dart'; 5 | export 'src/crypto_auth.dart'; 6 | export 'src/crypto_box.dart'; 7 | export 'src/crypto_sign.dart'; 8 | export 'src/crypto_stream.dart'; 9 | export 'src/detached_cipher.dart'; 10 | export 'src/generic_hash.dart'; 11 | export 'src/hash.dart'; 12 | export 'src/key_derivation.dart'; 13 | export 'src/key_exchange.dart'; 14 | export 'src/key_pair.dart'; 15 | export 'src/onetime_auth.dart'; 16 | export 'src/password_hash.dart'; 17 | export 'src/random_bytes.dart'; 18 | export 'src/scalar_mult.dart'; 19 | export 'src/secret_box.dart'; 20 | export 'src/sealed_box.dart'; 21 | export 'src/session_keys.dart'; 22 | export 'src/short_hash.dart'; 23 | export 'src/sodium.dart'; 24 | export 'src/sodium_exception.dart'; 25 | export 'src/x_cha_cha20_poly1305_ietf.dart'; 26 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_aead_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'libsodium.dart'; 3 | 4 | // ignore_for_file: non_constant_identifier_names 5 | 6 | class CryptoAeadBindings { 7 | final String name; 8 | final int Function() keybytes; 9 | final int Function() nsecbytes; 10 | final int Function() npubbytes; 11 | final int Function() abytes; 12 | final int Function() messagebytes_max; 13 | 14 | final int Function( 15 | Pointer c, 16 | Pointer clen_p, 17 | Pointer m, 18 | int mlen, 19 | Pointer ad, 20 | int adlen, 21 | Pointer nsec, 22 | Pointer npub, 23 | Pointer k) encrypt; 24 | 25 | final int Function( 26 | Pointer m, 27 | Pointer mlen_p, 28 | Pointer nsec, 29 | Pointer c, 30 | int clen, 31 | Pointer ad, 32 | int adlen, 33 | Pointer npub, 34 | Pointer k) decrypt; 35 | 36 | final int Function( 37 | Pointer c, 38 | Pointer mac, 39 | Pointer maclen_p, 40 | Pointer m, 41 | int mlen, 42 | Pointer ad, 43 | int adlen, 44 | Pointer nsec, 45 | Pointer npub, 46 | Pointer k) encrypt_detached; 47 | 48 | final int Function( 49 | Pointer m, 50 | Pointer nsec, 51 | Pointer c, 52 | int clen, 53 | Pointer mac, 54 | Pointer ad, 55 | int adlen, 56 | Pointer npub, 57 | Pointer k) decrypt_detached; 58 | 59 | final void Function(Pointer k) keygen; 60 | 61 | CryptoAeadBindings(this.name) 62 | : keybytes = libsodium.lookupSizet('${name}_keybytes'), 63 | nsecbytes = libsodium.lookupSizet('${name}_nsecbytes'), 64 | npubbytes = libsodium.lookupSizet('${name}_npubbytes'), 65 | abytes = libsodium.lookupSizet('${name}_abytes'), 66 | messagebytes_max = libsodium.lookupSizet('${name}_messagebytes_max'), 67 | encrypt = libsodium 68 | .lookup< 69 | NativeFunction< 70 | Int32 Function( 71 | Pointer, 72 | Pointer, 73 | Pointer, 74 | Uint64, 75 | Pointer, 76 | Uint64, 77 | Pointer, 78 | Pointer, 79 | Pointer)>>('${name}_encrypt') 80 | .asFunction(), 81 | decrypt = libsodium 82 | .lookup< 83 | NativeFunction< 84 | Int32 Function( 85 | Pointer, 86 | Pointer, 87 | Pointer, 88 | Pointer, 89 | Uint64, 90 | Pointer, 91 | Uint64, 92 | Pointer, 93 | Pointer)>>('${name}_decrypt') 94 | .asFunction(), 95 | encrypt_detached = libsodium 96 | .lookup< 97 | NativeFunction< 98 | Int32 Function( 99 | Pointer, 100 | Pointer, 101 | Pointer, 102 | Pointer, 103 | Uint64, 104 | Pointer, 105 | Uint64, 106 | Pointer, 107 | Pointer, 108 | Pointer)>>('${name}_encrypt_detached') 109 | .asFunction(), 110 | decrypt_detached = libsodium 111 | .lookup< 112 | NativeFunction< 113 | Int32 Function( 114 | Pointer, 115 | Pointer, 116 | Pointer, 117 | Uint64, 118 | Pointer, 119 | Pointer, 120 | Uint64, 121 | Pointer, 122 | Pointer)>>('${name}_decrypt_detached') 123 | .asFunction(), 124 | keygen = libsodium 125 | .lookup)>>( 126 | '${name}_keygen') 127 | .asFunction(); 128 | } 129 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_auth_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class CryptoAuthBindings { 8 | final int Function() crypto_auth_bytes = 9 | libsodium.lookupSizet('crypto_auth_bytes'); 10 | 11 | final int Function() crypto_auth_keybytes = 12 | libsodium.lookupSizet('crypto_auth_keybytes'); 13 | 14 | final Pointer Function() crypto_auth_primitive = libsodium 15 | .lookup Function()>>('crypto_auth_primitive') 16 | .asFunction(); 17 | 18 | final int Function( 19 | Pointer out, Pointer i, int inlen, Pointer k) 20 | crypto_auth = libsodium 21 | .lookup< 22 | NativeFunction< 23 | Int32 Function(Pointer, Pointer, Uint64, 24 | Pointer)>>('crypto_auth') 25 | .asFunction(); 26 | 27 | final int Function( 28 | Pointer h, Pointer i, int inlen, Pointer k) 29 | crypto_auth_verify = libsodium 30 | .lookup< 31 | NativeFunction< 32 | Int32 Function(Pointer, Pointer, Uint64, 33 | Pointer)>>('crypto_auth_verify') 34 | .asFunction(); 35 | 36 | final void Function(Pointer k) crypto_auth_keygen = libsodium 37 | .lookup)>>( 38 | 'crypto_auth_keygen') 39 | .asFunction(); 40 | } 41 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_box_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class CryptoBoxBindings { 8 | final int Function() crypto_box_seedbytes = 9 | libsodium.lookupSizet('crypto_box_seedbytes'); 10 | 11 | final int Function() crypto_box_publickeybytes = 12 | libsodium.lookupSizet('crypto_box_publickeybytes'); 13 | 14 | final int Function() crypto_box_secretkeybytes = 15 | libsodium.lookupSizet('crypto_box_secretkeybytes'); 16 | 17 | final int Function() crypto_box_noncebytes = 18 | libsodium.lookupSizet('crypto_box_noncebytes'); 19 | 20 | final int Function() crypto_box_macbytes = 21 | libsodium.lookupSizet('crypto_box_macbytes'); 22 | 23 | final int Function() crypto_box_messagebytes_max = 24 | libsodium.lookupSizet('crypto_box_messagebytes_max'); 25 | 26 | final int Function() crypto_box_sealbytes = 27 | libsodium.lookupSizet('crypto_box_sealbytes'); 28 | 29 | final int Function() crypto_box_beforenmbytes = 30 | libsodium.lookupSizet('crypto_box_beforenmbytes'); 31 | 32 | final Pointer Function() crypto_box_primitive = libsodium 33 | .lookup Function()>>('crypto_box_primitive') 34 | .asFunction(); 35 | 36 | final int Function(Pointer pk, Pointer sk, Pointer seed) 37 | crypto_box_seed_keypair = libsodium 38 | .lookup< 39 | NativeFunction< 40 | Int32 Function(Pointer, Pointer, 41 | Pointer)>>('crypto_box_seed_keypair') 42 | .asFunction(); 43 | 44 | final int Function( 45 | Pointer pk, 46 | Pointer 47 | sk) crypto_box_keypair = libsodium 48 | .lookup, Pointer)>>( 49 | 'crypto_box_keypair') 50 | .asFunction(); 51 | 52 | final int Function(Pointer c, Pointer m, int mlen, 53 | Pointer n, Pointer pk, Pointer sk) 54 | crypto_box_easy = libsodium 55 | .lookup< 56 | NativeFunction< 57 | Int32 Function( 58 | Pointer, 59 | Pointer, 60 | Uint64, 61 | Pointer, 62 | Pointer, 63 | Pointer)>>('crypto_box_easy') 64 | .asFunction(); 65 | 66 | final int Function(Pointer m, Pointer c, int clen, 67 | Pointer n, Pointer pk, Pointer sk) 68 | crypto_box_open_easy = libsodium 69 | .lookup< 70 | NativeFunction< 71 | Int32 Function( 72 | Pointer, 73 | Pointer, 74 | Uint64, 75 | Pointer, 76 | Pointer, 77 | Pointer)>>('crypto_box_open_easy') 78 | .asFunction(); 79 | 80 | final int Function(Pointer c, Pointer mac, Pointer m, 81 | int mlen, Pointer n, Pointer pk, Pointer sk) 82 | crypto_box_detached = libsodium 83 | .lookup< 84 | NativeFunction< 85 | Int32 Function( 86 | Pointer, 87 | Pointer, 88 | Pointer, 89 | Uint64, 90 | Pointer, 91 | Pointer, 92 | Pointer)>>('crypto_box_detached') 93 | .asFunction(); 94 | 95 | final int Function(Pointer m, Pointer c, Pointer mac, 96 | int clen, Pointer n, Pointer pk, Pointer sk) 97 | crypto_box_open_detached = libsodium 98 | .lookup< 99 | NativeFunction< 100 | Int32 Function( 101 | Pointer, 102 | Pointer, 103 | Pointer, 104 | Uint64, 105 | Pointer, 106 | Pointer, 107 | Pointer)>>('crypto_box_open_detached') 108 | .asFunction(); 109 | 110 | final int Function( 111 | Pointer c, Pointer m, int mlen, Pointer pk) 112 | crypto_box_seal = libsodium 113 | .lookup< 114 | NativeFunction< 115 | Int32 Function(Pointer, Pointer, Uint64, 116 | Pointer)>>('crypto_box_seal') 117 | .asFunction(); 118 | 119 | final int Function(Pointer m, Pointer c, int clen, 120 | Pointer pk, Pointer sk) crypto_box_seal_open = 121 | libsodium 122 | .lookup< 123 | NativeFunction< 124 | Int32 Function(Pointer, Pointer, Uint64, 125 | Pointer, Pointer)>>('crypto_box_seal_open') 126 | .asFunction(); 127 | 128 | final int Function(Pointer k, Pointer pk, Pointer sk) 129 | crypto_box_beforenm = libsodium 130 | .lookup< 131 | NativeFunction< 132 | Int32 Function(Pointer, Pointer, 133 | Pointer)>>('crypto_box_beforenm') 134 | .asFunction(); 135 | 136 | final int Function(Pointer c, Pointer m, int mlen, 137 | Pointer n, Pointer k) crypto_box_easy_afternm = 138 | libsodium 139 | .lookup< 140 | NativeFunction< 141 | Int32 Function( 142 | Pointer, 143 | Pointer, 144 | Uint64, 145 | Pointer, 146 | Pointer)>>('crypto_box_easy_afternm') 147 | .asFunction(); 148 | 149 | final int Function(Pointer m, Pointer c, int clen, 150 | Pointer n, Pointer k) crypto_box_open_easy_afternm = 151 | libsodium 152 | .lookup< 153 | NativeFunction< 154 | Int32 Function( 155 | Pointer, 156 | Pointer, 157 | Uint64, 158 | Pointer, 159 | Pointer)>>('crypto_box_open_easy_afternm') 160 | .asFunction(); 161 | 162 | final int Function(Pointer c, Pointer mac, Pointer m, 163 | int mlen, Pointer n, Pointer k) 164 | crypto_box_detached_afternm = libsodium 165 | .lookup< 166 | NativeFunction< 167 | Int32 Function( 168 | Pointer, 169 | Pointer, 170 | Pointer, 171 | Uint64, 172 | Pointer, 173 | Pointer)>>('crypto_box_detached_afternm') 174 | .asFunction(); 175 | 176 | final int Function(Pointer m, Pointer c, Pointer mac, 177 | int clen, Pointer n, Pointer k) 178 | crypto_box_open_detached_afternm = libsodium 179 | .lookup< 180 | NativeFunction< 181 | Int32 Function( 182 | Pointer, 183 | Pointer, 184 | Pointer, 185 | Uint64, 186 | Pointer, 187 | Pointer)>>('crypto_box_open_detached_afternm') 188 | .asFunction(); 189 | } 190 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_core_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'libsodium.dart'; 3 | 4 | // ignore_for_file: non_constant_identifier_names 5 | 6 | class CryptoCoreBindings { 7 | final int Function() crypto_core_hchacha20_outputbytes = 8 | libsodium.lookupSizet('crypto_core_hchacha20_outputbytes'); 9 | 10 | final int Function() crypto_core_hchacha20_inputbytes = 11 | libsodium.lookupSizet('crypto_core_hchacha20_inputbytes'); 12 | 13 | final int Function() crypto_core_hchacha20_keybytes = 14 | libsodium.lookupSizet('crypto_core_hchacha20_keybytes'); 15 | 16 | final int Function() crypto_core_hchacha20_constbytes = 17 | libsodium.lookupSizet('crypto_core_hchacha20_constbytes'); 18 | 19 | final int Function(Pointer out, Pointer in_, Pointer k, 20 | Pointer c) crypto_core_hchacha20 = 21 | libsodium 22 | .lookup< 23 | NativeFunction< 24 | Int32 Function(Pointer, Pointer, Pointer, 25 | Pointer)>>('crypto_core_hchacha20') 26 | .asFunction(); 27 | 28 | final int Function() crypto_core_hsalsa20_outputbytes = 29 | libsodium.lookupSizet('crypto_core_hsalsa20_outputbytes'); 30 | 31 | final int Function() crypto_core_hsalsa20_inputbytes = 32 | libsodium.lookupSizet('crypto_core_hsalsa20_inputbytes'); 33 | 34 | final int Function() crypto_core_hsalsa20_keybytes = 35 | libsodium.lookupSizet('crypto_core_hsalsa20_keybytes'); 36 | 37 | final int Function() crypto_core_hsalsa20_constbytes = 38 | libsodium.lookupSizet('crypto_core_hsalsa20_constbytes'); 39 | 40 | final int Function(Pointer out, Pointer in_, Pointer k, 41 | Pointer c) crypto_core_hsalsa20 = 42 | libsodium 43 | .lookup< 44 | NativeFunction< 45 | Int32 Function(Pointer, Pointer, Pointer, 46 | Pointer)>>('crypto_core_hsalsa20') 47 | .asFunction(); 48 | } 49 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_generichash_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class CryptoGenerichashBindings { 8 | final int Function() crypto_generichash_bytes_min = 9 | libsodium.lookupSizet('crypto_generichash_bytes_min'); 10 | 11 | final int Function() crypto_generichash_bytes_max = 12 | libsodium.lookupSizet('crypto_generichash_bytes_max'); 13 | 14 | final int Function() crypto_generichash_bytes = 15 | libsodium.lookupSizet('crypto_generichash_bytes'); 16 | 17 | final int Function() crypto_generichash_keybytes_min = 18 | libsodium.lookupSizet('crypto_generichash_keybytes_min'); 19 | 20 | final int Function() crypto_generichash_keybytes_max = 21 | libsodium.lookupSizet('crypto_generichash_keybytes_max'); 22 | 23 | final int Function() crypto_generichash_keybytes = 24 | libsodium.lookupSizet('crypto_generichash_keybytes'); 25 | 26 | final Pointer Function() crypto_generichash_primitive = libsodium 27 | .lookup Function()>>( 28 | 'crypto_generichash_primitive') 29 | .asFunction(); 30 | 31 | final int Function() crypto_generichash_statebytes = 32 | libsodium.lookupSizet('crypto_generichash_statebytes'); 33 | 34 | final int Function(Pointer out, int outlen, Pointer i, 35 | int inlen, Pointer key, int keylen) crypto_generichash = 36 | libsodium 37 | .lookup< 38 | NativeFunction< 39 | Int32 Function(Pointer, IntPtr, Pointer, Uint64, 40 | Pointer, IntPtr)>>('crypto_generichash') 41 | .asFunction(); 42 | 43 | final int Function( 44 | Pointer state, Pointer key, int keylen, int outlen) 45 | crypto_generichash_init = libsodium 46 | .lookup< 47 | NativeFunction< 48 | Int32 Function(Pointer, Pointer, IntPtr, 49 | IntPtr)>>('crypto_generichash_init') 50 | .asFunction(); 51 | 52 | final int Function(Pointer state, Pointer i, int inlen) 53 | crypto_generichash_update = libsodium 54 | .lookup< 55 | NativeFunction< 56 | Int32 Function( 57 | Pointer, 58 | Pointer, 59 | Uint64, 60 | )>>('crypto_generichash_update') 61 | .asFunction(); 62 | 63 | final int Function(Pointer state, Pointer out, int outlen) 64 | crypto_generichash_final = libsodium 65 | .lookup< 66 | NativeFunction< 67 | Int32 Function( 68 | Pointer, 69 | Pointer, 70 | IntPtr, 71 | )>>('crypto_generichash_final') 72 | .asFunction(); 73 | 74 | final void Function(Pointer k) crypto_generichash_keygen = libsodium 75 | .lookup)>>( 76 | 'crypto_generichash_keygen') 77 | .asFunction(); 78 | } 79 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_hash_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class CryptoHashBindings { 8 | final String name; 9 | final int Function() bytes; 10 | final Pointer Function() primitive; 11 | 12 | final int Function(Pointer out, Pointer i, int inlen) hash; 13 | 14 | CryptoHashBindings(this.name) 15 | : bytes = libsodium.lookupSizet('${name}_bytes'), 16 | primitive = libsodium 17 | .lookup Function()>>( 18 | '${name}_primitive') 19 | .asFunction(), 20 | hash = libsodium 21 | .lookup< 22 | NativeFunction< 23 | Int32 Function( 24 | Pointer, Pointer, Uint64)>>(name) 25 | .asFunction(); 26 | } 27 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_kdf_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class CryptoKdfBindings { 8 | final int Function() crypto_kdf_bytes_min = 9 | libsodium.lookupSizet('crypto_kdf_bytes_min'); 10 | 11 | final int Function() crypto_kdf_bytes_max = 12 | libsodium.lookupSizet('crypto_kdf_bytes_max'); 13 | 14 | final int Function() crypto_kdf_contextbytes = 15 | libsodium.lookupSizet('crypto_kdf_contextbytes'); 16 | 17 | final int Function() crypto_kdf_keybytes = 18 | libsodium.lookupSizet('crypto_kdf_keybytes'); 19 | 20 | final Pointer Function() crypto_kdf_primitive = libsodium 21 | .lookup Function()>>('crypto_kdf_primitive') 22 | .asFunction(); 23 | 24 | final int Function(Pointer subkey, int subkey_len, int subkey_id, 25 | Pointer ctx, Pointer key) crypto_kdf_derive_from_key = 26 | libsodium 27 | .lookup< 28 | NativeFunction< 29 | Int32 Function(Pointer, IntPtr, Uint64, Pointer, 30 | Pointer)>>('crypto_kdf_derive_from_key') 31 | .asFunction(); 32 | 33 | final void Function(Pointer k) crypto_kdf_keygen = libsodium 34 | .lookup)>>( 35 | 'crypto_kdf_keygen') 36 | .asFunction(); 37 | } 38 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_kx_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class CryptoKxBindings { 8 | final int Function() crypto_kx_publickeybytes = 9 | libsodium.lookupSizet('crypto_kx_publickeybytes'); 10 | 11 | final int Function() crypto_kx_secretkeybytes = 12 | libsodium.lookupSizet('crypto_kx_secretkeybytes'); 13 | 14 | final int Function() crypto_kx_seedbytes = 15 | libsodium.lookupSizet('crypto_kx_seedbytes'); 16 | 17 | final int Function() crypto_kx_sessionkeybytes = 18 | libsodium.lookupSizet('crypto_kx_sessionkeybytes'); 19 | 20 | final Pointer Function() crypto_kx_primitive = libsodium 21 | .lookup Function()>>('crypto_kx_primitive') 22 | .asFunction(); 23 | 24 | final int Function(Pointer pk, Pointer sk, Pointer seed) 25 | crypto_kx_seed_keypair = libsodium 26 | .lookup< 27 | NativeFunction< 28 | Int32 Function(Pointer, Pointer, 29 | Pointer)>>('crypto_kx_seed_keypair') 30 | .asFunction(); 31 | 32 | final int Function( 33 | Pointer pk, 34 | Pointer 35 | sk) crypto_kx_keypair = libsodium 36 | .lookup, Pointer)>>( 37 | 'crypto_kx_keypair') 38 | .asFunction(); 39 | 40 | final int Function( 41 | Pointer rx, 42 | Pointer tx, 43 | Pointer client_pk, 44 | Pointer client_sk, 45 | Pointer server_pk) crypto_kx_client_session_keys = 46 | libsodium 47 | .lookup< 48 | NativeFunction< 49 | Int32 Function( 50 | Pointer, 51 | Pointer, 52 | Pointer, 53 | Pointer, 54 | Pointer)>>('crypto_kx_client_session_keys') 55 | .asFunction(); 56 | 57 | final int Function( 58 | Pointer rx, 59 | Pointer tx, 60 | Pointer server_pk, 61 | Pointer server_sk, 62 | Pointer client_pk) crypto_kx_server_session_keys = 63 | libsodium 64 | .lookup< 65 | NativeFunction< 66 | Int32 Function( 67 | Pointer, 68 | Pointer, 69 | Pointer, 70 | Pointer, 71 | Pointer)>>('crypto_kx_server_session_keys') 72 | .asFunction(); 73 | } 74 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_onetimeauth_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class CryptoOnetimeauthBindings { 8 | final int Function() crypto_onetimeauth_statebytes = 9 | libsodium.lookupSizet('crypto_onetimeauth_statebytes'); 10 | 11 | final int Function() crypto_onetimeauth_bytes = 12 | libsodium.lookupSizet('crypto_onetimeauth_bytes'); 13 | 14 | final int Function() crypto_onetimeauth_keybytes = 15 | libsodium.lookupSizet('crypto_onetimeauth_keybytes'); 16 | 17 | final Pointer Function() crypto_onetimeauth_primitive = libsodium 18 | .lookup Function()>>( 19 | 'crypto_onetimeauth_primitive') 20 | .asFunction(); 21 | 22 | final int Function( 23 | Pointer out, Pointer i, int inlen, Pointer k) 24 | crypto_onetimeauth = libsodium 25 | .lookup< 26 | NativeFunction< 27 | Int32 Function(Pointer, Pointer, Uint64, 28 | Pointer)>>('crypto_onetimeauth') 29 | .asFunction(); 30 | 31 | final int Function( 32 | Pointer h, Pointer i, int inlen, Pointer k) 33 | crypto_onetimeauth_verify = libsodium 34 | .lookup< 35 | NativeFunction< 36 | Int32 Function(Pointer, Pointer, Uint64, 37 | Pointer)>>('crypto_onetimeauth_verify') 38 | .asFunction(); 39 | 40 | final int Function( 41 | Pointer state, 42 | Pointer 43 | key) crypto_onetimeauth_init = libsodium 44 | .lookup, Pointer)>>( 45 | 'crypto_onetimeauth_init') 46 | .asFunction(); 47 | 48 | final int Function(Pointer state, Pointer i, int inlen) 49 | crypto_onetimeauth_update = libsodium 50 | .lookup< 51 | NativeFunction< 52 | Int32 Function(Pointer, Pointer, 53 | Uint64)>>('crypto_onetimeauth_update') 54 | .asFunction(); 55 | 56 | final int Function( 57 | Pointer state, 58 | Pointer 59 | out) crypto_onetimeauth_final = libsodium 60 | .lookup, Pointer)>>( 61 | 'crypto_onetimeauth_final') 62 | .asFunction(); 63 | 64 | final void Function(Pointer k) crypto_onetimeauth_keygen = libsodium 65 | .lookup)>>( 66 | 'crypto_onetimeauth_keygen') 67 | .asFunction(); 68 | } 69 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_pwhash_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class CryptoPwhashBindings { 8 | final int Function() crypto_pwhash_alg_argon2i13 = libsodium 9 | .lookup>('crypto_pwhash_alg_argon2i13') 10 | .asFunction(); 11 | 12 | final int Function() crypto_pwhash_alg_argon2id13 = libsodium 13 | .lookup>('crypto_pwhash_alg_argon2id13') 14 | .asFunction(); 15 | 16 | final int Function() crypto_pwhash_alg_default = libsodium 17 | .lookup>('crypto_pwhash_alg_default') 18 | .asFunction(); 19 | 20 | final int Function() crypto_pwhash_bytes_min = 21 | libsodium.lookupSizet('crypto_pwhash_bytes_min'); 22 | 23 | final int Function() crypto_pwhash_bytes_max = 24 | libsodium.lookupSizet('crypto_pwhash_bytes_max'); 25 | 26 | final int Function() crypto_pwhash_passwd_min = 27 | libsodium.lookupSizet('crypto_pwhash_passwd_min'); 28 | 29 | final int Function() crypto_pwhash_passwd_max = 30 | libsodium.lookupSizet('crypto_pwhash_passwd_max'); 31 | 32 | final int Function() crypto_pwhash_saltbytes = 33 | libsodium.lookupSizet('crypto_pwhash_saltbytes'); 34 | 35 | final int Function() crypto_pwhash_strbytes = 36 | libsodium.lookupSizet('crypto_pwhash_strbytes'); 37 | 38 | final Pointer Function() crypto_pwhash_strprefix = libsodium 39 | .lookup Function()>>( 40 | 'crypto_pwhash_strprefix') 41 | .asFunction(); 42 | 43 | final int Function() crypto_pwhash_opslimit_min = 44 | libsodium.lookupSizet('crypto_pwhash_opslimit_min'); 45 | 46 | final int Function() crypto_pwhash_opslimit_max = 47 | libsodium.lookupSizet('crypto_pwhash_opslimit_max'); 48 | 49 | final int Function() crypto_pwhash_memlimit_min = 50 | libsodium.lookupSizet('crypto_pwhash_memlimit_min'); 51 | 52 | final int Function() crypto_pwhash_memlimit_max = 53 | libsodium.lookupSizet('crypto_pwhash_memlimit_max'); 54 | 55 | final int Function() crypto_pwhash_argon2i_opslimit_min = 56 | libsodium.lookupSizet('crypto_pwhash_argon2i_opslimit_min'); 57 | 58 | final int Function() crypto_pwhash_argon2i_opslimit_max = 59 | libsodium.lookupSizet('crypto_pwhash_argon2i_opslimit_max'); 60 | 61 | final int Function() crypto_pwhash_argon2i_memlimit_min = 62 | libsodium.lookupSizet('crypto_pwhash_argon2i_memlimit_min'); 63 | 64 | final int Function() crypto_pwhash_argon2i_memlimit_max = 65 | libsodium.lookupSizet('crypto_pwhash_argon2i_memlimit_max'); 66 | 67 | final int Function() crypto_pwhash_opslimit_interactive = 68 | libsodium.lookupSizet('crypto_pwhash_opslimit_interactive'); 69 | 70 | final int Function() crypto_pwhash_memlimit_interactive = 71 | libsodium.lookupSizet('crypto_pwhash_memlimit_interactive'); 72 | 73 | final int Function() crypto_pwhash_opslimit_moderate = 74 | libsodium.lookupSizet('crypto_pwhash_opslimit_moderate'); 75 | 76 | final int Function() crypto_pwhash_memlimit_moderate = 77 | libsodium.lookupSizet('crypto_pwhash_memlimit_moderate'); 78 | 79 | final int Function() crypto_pwhash_opslimit_sensitive = 80 | libsodium.lookupSizet('crypto_pwhash_opslimit_sensitive'); 81 | 82 | final int Function() crypto_pwhash_memlimit_sensitive = 83 | libsodium.lookupSizet('crypto_pwhash_memlimit_sensitive'); 84 | 85 | final Pointer Function() crypto_pwhash_primitive = libsodium 86 | .lookup Function()>>( 87 | 'crypto_pwhash_primitive') 88 | .asFunction(); 89 | 90 | final int Function( 91 | Pointer out, 92 | int, 93 | Pointer passwd, 94 | int passwdlen, 95 | Pointer salt, 96 | int opslimit, 97 | int memlimit, 98 | int alg) crypto_pwhash = 99 | libsodium 100 | .lookup< 101 | NativeFunction< 102 | Int32 Function(Pointer, Uint64, Pointer, Uint64, 103 | Pointer, Uint64, IntPtr, Int32)>>('crypto_pwhash') 104 | .asFunction(); 105 | 106 | final int Function(Pointer out, Pointer passwd, int passwdlen, 107 | int opslimit, int memlimit) crypto_pwhash_str = 108 | libsodium 109 | .lookup< 110 | NativeFunction< 111 | Int32 Function(Pointer, Pointer, Uint64, Uint64, 112 | IntPtr)>>('crypto_pwhash_str') 113 | .asFunction(); 114 | 115 | final int Function(Pointer out, Pointer passwd, int passwdlen, 116 | int opslimit, int memlimit, int alg) crypto_pwhash_str_alg = 117 | libsodium 118 | .lookup< 119 | NativeFunction< 120 | Int32 Function(Pointer, Pointer, Uint64, Uint64, 121 | IntPtr, Int32)>>('crypto_pwhash_str_alg') 122 | .asFunction(); 123 | 124 | final int Function(Pointer str, Pointer passwd, int passwdlen) 125 | crypto_pwhash_str_verify = libsodium 126 | .lookup< 127 | NativeFunction< 128 | Int32 Function(Pointer, Pointer, 129 | Uint64)>>('crypto_pwhash_str_verify') 130 | .asFunction(); 131 | 132 | final int Function( 133 | Pointer str, 134 | int opslimit, 135 | int 136 | memlimit) crypto_pwhash_str_needs_rehash = libsodium 137 | .lookup, Uint64, IntPtr)>>( 138 | 'crypto_pwhash_str_needs_rehash') 139 | .asFunction(); 140 | } 141 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_scalarmult_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class CryptoScalarmultBindings { 8 | final int Function() crypto_scalarmult_bytes = 9 | libsodium.lookupSizet('crypto_scalarmult_bytes'); 10 | 11 | final int Function() crypto_scalarmult_scalarbytes = 12 | libsodium.lookupSizet('crypto_scalarmult_scalarbytes'); 13 | 14 | final int Function() crypto_scalarmult_curve25519_bytes = 15 | libsodium.lookupSizet('crypto_scalarmult_curve25519_bytes'); 16 | 17 | final Pointer Function() crypto_scalarmult_primitive = libsodium 18 | .lookup Function()>>( 19 | 'crypto_scalarmult_primitive') 20 | .asFunction(); 21 | 22 | final int Function( 23 | Pointer q, 24 | Pointer 25 | n) crypto_scalarmult_base = libsodium 26 | .lookup, Pointer)>>( 27 | 'crypto_scalarmult_base') 28 | .asFunction(); 29 | 30 | final int Function(Pointer q, Pointer n, Pointer p) 31 | crypto_scalarmult = libsodium 32 | .lookup< 33 | NativeFunction< 34 | Int32 Function(Pointer, Pointer, 35 | Pointer)>>('crypto_scalarmult') 36 | .asFunction(); 37 | } 38 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_secretbox_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class CryptoSecretboxBindings { 8 | final int Function() crypto_secretbox_keybytes = 9 | libsodium.lookupSizet('crypto_secretbox_keybytes'); 10 | 11 | final int Function() crypto_secretbox_noncebytes = 12 | libsodium.lookupSizet('crypto_secretbox_noncebytes'); 13 | 14 | final int Function() crypto_secretbox_macbytes = 15 | libsodium.lookupSizet('crypto_secretbox_macbytes'); 16 | 17 | final int Function() crypto_secretbox_messagebytes_max = 18 | libsodium.lookupSizet('crypto_secretbox_messagebytes_max'); 19 | 20 | final Pointer Function() crypto_secretbox_primitive = libsodium 21 | .lookup Function()>>( 22 | 'crypto_secretbox_primitive') 23 | .asFunction(); 24 | 25 | final int Function(Pointer c, Pointer m, int mlen, 26 | Pointer n, Pointer k) crypto_secretbox_easy = 27 | libsodium 28 | .lookup< 29 | NativeFunction< 30 | Int32 Function(Pointer, Pointer, Uint64, 31 | Pointer, Pointer)>>('crypto_secretbox_easy') 32 | .asFunction(); 33 | 34 | final int Function(Pointer m, Pointer c, int clen, 35 | Pointer n, Pointer k) crypto_secretbox_open_easy = 36 | libsodium 37 | .lookup< 38 | NativeFunction< 39 | Int32 Function( 40 | Pointer, 41 | Pointer, 42 | Uint64, 43 | Pointer, 44 | Pointer)>>('crypto_secretbox_open_easy') 45 | .asFunction(); 46 | 47 | final int Function(Pointer c, Pointer mac, Pointer m, 48 | int mlen, Pointer n, Pointer k) 49 | crypto_secretbox_detached = libsodium 50 | .lookup< 51 | NativeFunction< 52 | Int32 Function( 53 | Pointer, 54 | Pointer, 55 | Pointer, 56 | Uint64, 57 | Pointer, 58 | Pointer)>>('crypto_secretbox_detached') 59 | .asFunction(); 60 | 61 | final int Function(Pointer m, Pointer c, Pointer mac, 62 | int clen, Pointer n, Pointer k) 63 | crypto_secretbox_open_detached = libsodium 64 | .lookup< 65 | NativeFunction< 66 | Int32 Function( 67 | Pointer, 68 | Pointer, 69 | Pointer, 70 | Uint64, 71 | Pointer, 72 | Pointer)>>('crypto_secretbox_open_detached') 73 | .asFunction(); 74 | 75 | final void Function(Pointer k) crypto_secretbox_keygen = libsodium 76 | .lookup)>>( 77 | 'crypto_secretbox_keygen') 78 | .asFunction(); 79 | } 80 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_secretstream_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'libsodium.dart'; 3 | 4 | // ignore_for_file: non_constant_identifier_names 5 | 6 | class CryptoSecretstreamBindings { 7 | final int Function() crypto_secretstream_xchacha20poly1305_abytes = 8 | libsodium.lookupSizet('crypto_secretstream_xchacha20poly1305_abytes'); 9 | 10 | final int Function() crypto_secretstream_xchacha20poly1305_headerbytes = 11 | libsodium 12 | .lookupSizet('crypto_secretstream_xchacha20poly1305_headerbytes'); 13 | 14 | final int Function() crypto_secretstream_xchacha20poly1305_keybytes = 15 | libsodium.lookupSizet('crypto_secretstream_xchacha20poly1305_keybytes'); 16 | 17 | final int Function() crypto_secretstream_xchacha20poly1305_messagebytes_max = 18 | libsodium.lookupSizet( 19 | 'crypto_secretstream_xchacha20poly1305_messagebytes_max'); 20 | 21 | final int Function() crypto_secretstream_xchacha20poly1305_tag_message = 22 | libsodium 23 | .lookup>( 24 | 'crypto_secretstream_xchacha20poly1305_tag_message') 25 | .asFunction(); 26 | 27 | final int Function() crypto_secretstream_xchacha20poly1305_tag_push = 28 | libsodium 29 | .lookup>( 30 | 'crypto_secretstream_xchacha20poly1305_tag_push') 31 | .asFunction(); 32 | 33 | final int Function() crypto_secretstream_xchacha20poly1305_tag_rekey = 34 | libsodium 35 | .lookup>( 36 | 'crypto_secretstream_xchacha20poly1305_tag_rekey') 37 | .asFunction(); 38 | 39 | final int Function() crypto_secretstream_xchacha20poly1305_tag_final = 40 | libsodium 41 | .lookup>( 42 | 'crypto_secretstream_xchacha20poly1305_tag_final') 43 | .asFunction(); 44 | 45 | final int Function() crypto_secretstream_xchacha20poly1305_statebytes = 46 | libsodium.lookupSizet('crypto_secretstream_xchacha20poly1305_statebytes'); 47 | 48 | final void Function(Pointer k) 49 | crypto_secretstream_xchacha20poly1305_keygen = libsodium 50 | .lookup)>>( 51 | 'crypto_secretstream_xchacha20poly1305_keygen') 52 | .asFunction(); 53 | 54 | final int Function( 55 | Pointer state, Pointer header, Pointer k) 56 | crypto_secretstream_xchacha20poly1305_init_push = libsodium 57 | .lookup< 58 | NativeFunction< 59 | Int32 Function( 60 | Pointer, Pointer, Pointer)>>( 61 | 'crypto_secretstream_xchacha20poly1305_init_push') 62 | .asFunction(); 63 | 64 | final int Function( 65 | Pointer state, 66 | Pointer c, 67 | Pointer clen_p, 68 | Pointer m, 69 | int mlen, 70 | Pointer ad, 71 | int adlen, 72 | int tag) crypto_secretstream_xchacha20poly1305_push = 73 | libsodium 74 | .lookup< 75 | NativeFunction< 76 | Int32 Function( 77 | Pointer, 78 | Pointer, 79 | Pointer, 80 | Pointer, 81 | Uint64, 82 | Pointer, 83 | Uint64, 84 | Uint8)>>('crypto_secretstream_xchacha20poly1305_push') 85 | .asFunction(); 86 | 87 | final int Function( 88 | Pointer state, Pointer header, Pointer k) 89 | crypto_secretstream_xchacha20poly1305_init_pull = libsodium 90 | .lookup< 91 | NativeFunction< 92 | Int32 Function( 93 | Pointer, Pointer, Pointer)>>( 94 | 'crypto_secretstream_xchacha20poly1305_init_pull') 95 | .asFunction(); 96 | 97 | final int Function( 98 | Pointer state, 99 | Pointer m, 100 | Pointer mlen_p, 101 | Pointer tag_p, 102 | Pointer c, 103 | int clen, 104 | Pointer ad, 105 | int adlen) crypto_secretstream_xchacha20poly1305_pull = 106 | libsodium 107 | .lookup< 108 | NativeFunction< 109 | Int32 Function( 110 | Pointer, 111 | Pointer, 112 | Pointer, 113 | Pointer, 114 | Pointer, 115 | Uint64, 116 | Pointer, 117 | Uint64)>>('crypto_secretstream_xchacha20poly1305_pull') 118 | .asFunction(); 119 | 120 | final void Function(Pointer state) 121 | crypto_secretstream_xchacha20poly1305_rekey = libsodium 122 | .lookup)>>( 123 | 'crypto_secretstream_xchacha20poly1305_rekey') 124 | .asFunction(); 125 | } 126 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_shorthash_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class CryptoShorthashBindings { 8 | final int Function() crypto_shorthash_bytes = 9 | libsodium.lookupSizet('crypto_shorthash_bytes'); 10 | 11 | final int Function() crypto_shorthash_keybytes = 12 | libsodium.lookupSizet('crypto_shorthash_keybytes'); 13 | 14 | final Pointer Function() crypto_shorthash_primitive = libsodium 15 | .lookup Function()>>( 16 | 'crypto_shorthash_primitive') 17 | .asFunction(); 18 | 19 | final int Function( 20 | Pointer out, Pointer i, int inlen, Pointer k) 21 | crypto_shorthash = libsodium 22 | .lookup< 23 | NativeFunction< 24 | Int32 Function(Pointer, Pointer, Uint64, 25 | Pointer)>>('crypto_shorthash') 26 | .asFunction(); 27 | 28 | final void Function(Pointer k) crypto_shorthash_keygen = libsodium 29 | .lookup)>>( 30 | 'crypto_shorthash_keygen') 31 | .asFunction(); 32 | } 33 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_sign_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class CryptoSignBindings { 8 | final int Function() crypto_sign_statebytes = 9 | libsodium.lookupSizet('crypto_sign_statebytes'); 10 | 11 | final int Function() crypto_sign_bytes = 12 | libsodium.lookupSizet('crypto_sign_bytes'); 13 | 14 | final int Function() crypto_sign_seedbytes = 15 | libsodium.lookupSizet('crypto_sign_seedbytes'); 16 | 17 | final int Function() crypto_sign_publickeybytes = 18 | libsodium.lookupSizet('crypto_sign_publickeybytes'); 19 | 20 | final int Function() crypto_sign_secretkeybytes = 21 | libsodium.lookupSizet('crypto_sign_secretkeybytes'); 22 | 23 | final int Function() crypto_sign_messagebytes_max = 24 | libsodium.lookupSizet('crypto_sign_messagebytes_max'); 25 | 26 | final int Function() crypto_sign_ed25519_publickeybytes = 27 | libsodium.lookupSizet('crypto_sign_ed25519_publickeybytes'); 28 | 29 | final int Function() crypto_sign_ed25519_secretkeybytes = 30 | libsodium.lookupSizet('crypto_sign_ed25519_secretkeybytes'); 31 | 32 | final Pointer Function() crypto_sign_primitive = libsodium 33 | .lookup Function()>>('crypto_sign_primitive') 34 | .asFunction(); 35 | 36 | final int Function(Pointer pk, Pointer sk, Pointer seed) 37 | crypto_sign_seed_keypair = libsodium 38 | .lookup< 39 | NativeFunction< 40 | Int32 Function(Pointer, Pointer, 41 | Pointer)>>('crypto_sign_seed_keypair') 42 | .asFunction(); 43 | 44 | final int Function( 45 | Pointer pk, 46 | Pointer 47 | sk) crypto_sign_keypair = libsodium 48 | .lookup, Pointer)>>( 49 | 'crypto_sign_keypair') 50 | .asFunction(); 51 | 52 | final int Function(Pointer sm, Pointer smlen_p, 53 | Pointer m, int mlen, Pointer sk) crypto_sign = 54 | libsodium 55 | .lookup< 56 | NativeFunction< 57 | Int32 Function(Pointer, Pointer, 58 | Pointer, Uint64, Pointer)>>('crypto_sign') 59 | .asFunction(); 60 | 61 | final int Function(Pointer m, Pointer mlen_p, 62 | Pointer sm, int smlen, Pointer pk) crypto_sign_open = 63 | libsodium 64 | .lookup< 65 | NativeFunction< 66 | Int32 Function( 67 | Pointer, 68 | Pointer, 69 | Pointer, 70 | Uint64, 71 | Pointer)>>('crypto_sign_open') 72 | .asFunction(); 73 | 74 | final int Function(Pointer sig, Pointer siglen_p, 75 | Pointer m, int mlen, Pointer sk) crypto_sign_detached = 76 | libsodium 77 | .lookup< 78 | NativeFunction< 79 | Int32 Function( 80 | Pointer, 81 | Pointer, 82 | Pointer, 83 | Uint64, 84 | Pointer)>>('crypto_sign_detached') 85 | .asFunction(); 86 | 87 | final int Function( 88 | Pointer sig, Pointer m, int mlen, Pointer pk) 89 | crypto_sign_verify_detached = libsodium 90 | .lookup< 91 | NativeFunction< 92 | Int32 Function(Pointer, Pointer, Uint64, 93 | Pointer)>>('crypto_sign_verify_detached') 94 | .asFunction(); 95 | 96 | final int Function(Pointer state) crypto_sign_init = libsodium 97 | .lookup)>>( 98 | 'crypto_sign_init') 99 | .asFunction(); 100 | 101 | final int Function(Pointer state, Pointer m, int mlen) 102 | crypto_sign_update = libsodium 103 | .lookup< 104 | NativeFunction< 105 | Int32 Function(Pointer, Pointer, 106 | Uint64)>>('crypto_sign_update') 107 | .asFunction(); 108 | 109 | final int Function(Pointer state, Pointer sig, 110 | Pointer siglen_p, Pointer sk) 111 | crypto_sign_final_create = libsodium 112 | .lookup< 113 | NativeFunction< 114 | Int32 Function( 115 | Pointer, 116 | Pointer, 117 | Pointer, 118 | Pointer)>>('crypto_sign_final_create') 119 | .asFunction(); 120 | 121 | final int Function( 122 | Pointer state, Pointer sig, Pointer pk) 123 | crypto_sign_final_verify = libsodium 124 | .lookup< 125 | NativeFunction< 126 | Int32 Function(Pointer, Pointer, 127 | Pointer)>>('crypto_sign_final_verify') 128 | .asFunction(); 129 | 130 | final int Function( 131 | Pointer seed, 132 | Pointer 133 | sk) crypto_sign_ed25519_sk_to_seed = libsodium 134 | .lookup, Pointer)>>( 135 | 'crypto_sign_ed25519_sk_to_seed') 136 | .asFunction(); 137 | 138 | final int Function( 139 | Pointer pk, 140 | Pointer 141 | sk) crypto_sign_ed25519_sk_to_pk = libsodium 142 | .lookup, Pointer)>>( 143 | 'crypto_sign_ed25519_sk_to_pk') 144 | .asFunction(); 145 | 146 | final int Function( 147 | Pointer curve25519_pk, 148 | Pointer 149 | ed25519_pk) crypto_sign_ed25519_pk_to_curve25519 = libsodium 150 | .lookup, Pointer)>>( 151 | 'crypto_sign_ed25519_pk_to_curve25519') 152 | .asFunction(); 153 | 154 | final int Function( 155 | Pointer curve25519_sk, 156 | Pointer 157 | ed25519_sk) crypto_sign_ed25519_sk_to_curve25519 = libsodium 158 | .lookup, Pointer)>>( 159 | 'crypto_sign_ed25519_sk_to_curve25519') 160 | .asFunction(); 161 | } 162 | -------------------------------------------------------------------------------- /lib/src/bindings/crypto_stream_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class CryptoStreamBindings { 8 | final int Function() crypto_stream_keybytes = 9 | libsodium.lookupSizet('crypto_stream_keybytes'); 10 | 11 | final int Function() crypto_stream_noncebytes = 12 | libsodium.lookupSizet('crypto_stream_noncebytes'); 13 | 14 | final int Function() crypto_stream_messagebytes_max = 15 | libsodium.lookupSizet('crypto_stream_messagebytes_max'); 16 | 17 | final Pointer Function() crypto_stream_primitive = libsodium 18 | .lookup Function()>>( 19 | 'crypto_stream_primitive') 20 | .asFunction(); 21 | 22 | final int Function( 23 | Pointer c, int clen, Pointer n, Pointer k) 24 | crypto_stream = libsodium 25 | .lookup< 26 | NativeFunction< 27 | Int32 Function(Pointer, Uint64, Pointer, 28 | Pointer)>>('crypto_stream') 29 | .asFunction(); 30 | 31 | final int Function(Pointer c, Pointer m, int mlen, 32 | Pointer n, Pointer k) crypto_stream_xor = 33 | libsodium 34 | .lookup< 35 | NativeFunction< 36 | Int32 Function(Pointer, Pointer, Uint64, 37 | Pointer, Pointer)>>('crypto_stream_xor') 38 | .asFunction(); 39 | 40 | final void Function(Pointer k) crypto_stream_keygen = libsodium 41 | .lookup)>>( 42 | 'crypto_stream_keygen') 43 | .asFunction(); 44 | } 45 | -------------------------------------------------------------------------------- /lib/src/bindings/libsodium.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'dart:io'; 3 | 4 | import 'package:flutter_sodium/flutter_sodium.dart'; 5 | 6 | final libsodium = _load(); 7 | 8 | DynamicLibrary _load() { 9 | if (Platform.isAndroid) { 10 | return DynamicLibrary.open('libsodium.so'); 11 | } 12 | if (Platform.isIOS) { 13 | return DynamicLibrary.process(); 14 | } 15 | if (Platform.isMacOS) { 16 | // assuming user installed libsodium as per the installation instructions 17 | // see also https://libsodium.gitbook.io/doc/installation 18 | return DynamicLibrary.open('/usr/local/lib/libsodium.dylib'); 19 | } 20 | if (Platform.isLinux) { 21 | // assuming user installed libsodium as per the installation instructions 22 | // see also https://libsodium.gitbook.io/doc/installation 23 | return DynamicLibrary.open('libsodium.so.23'); 24 | } 25 | if (Platform.isWindows) { 26 | // assuming user installed libsodium as per the installation instructions 27 | // see also https://py-ipv8.readthedocs.io/en/latest/preliminaries/install_libsodium/ 28 | return DynamicLibrary.open('C:\\Windows\\System32\\libsodium.dll'); 29 | } 30 | throw SodiumException('platform not supported'); 31 | } 32 | 33 | // Extension helper for functions returning size_t 34 | // this is a workaround for size_t not being properly supported in ffi. IntPtr 35 | // almost works, but is sign extended. 36 | extension Bindings on DynamicLibrary { 37 | int Function() lookupSizet(String symbolName) => sizeOf() == 4 38 | ? this.lookup>(symbolName).asFunction() 39 | : this.lookup>(symbolName).asFunction(); 40 | } 41 | -------------------------------------------------------------------------------- /lib/src/bindings/randombytes_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class RandombytesBindings { 8 | final int Function() randombytes_seedbytes = libsodium 9 | .lookup>('randombytes_seedbytes') 10 | .asFunction(); 11 | 12 | final void Function(Pointer buf, int size) randombytes_buf = libsodium 13 | .lookup, IntPtr)>>( 14 | 'randombytes_buf') 15 | .asFunction(); 16 | 17 | final void Function(Pointer buf, int size, Pointer seed) 18 | randombytes_buf_deterministic = libsodium 19 | .lookup< 20 | NativeFunction< 21 | Void Function(Pointer, IntPtr, 22 | Pointer)>>('randombytes_buf_deterministic') 23 | .asFunction(); 24 | 25 | final int Function() randombytes_random = libsodium 26 | .lookup>('randombytes_random') 27 | .asFunction(); 28 | 29 | final int Function(int upper_bound) randombytes_uniform = libsodium 30 | .lookup>('randombytes_uniform') 31 | .asFunction(); 32 | 33 | final void Function() randombytes_stir = libsodium 34 | .lookup>('randombytes_stir') 35 | .asFunction(); 36 | 37 | final void Function() randombytes_close = libsodium 38 | .lookup>('randombytes_close') 39 | .asFunction(); 40 | 41 | final Pointer Function() randombytes_implementation_name = libsodium 42 | .lookup Function()>>( 43 | 'randombytes_implementation_name') 44 | .asFunction(); 45 | } 46 | -------------------------------------------------------------------------------- /lib/src/bindings/sodium_bindings.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'package:ffi/ffi.dart'; 3 | import 'libsodium.dart'; 4 | 5 | // ignore_for_file: non_constant_identifier_names 6 | 7 | class SodiumBindings { 8 | final int Function() sodium_init = libsodium 9 | .lookup>('sodium_init') 10 | .asFunction(); 11 | final Pointer Function() sodium_version_string = libsodium 12 | .lookup Function()>>('sodium_version_string') 13 | .asFunction(); 14 | final int Function() sodium_library_version_major = libsodium 15 | .lookup>('sodium_library_version_major') 16 | .asFunction(); 17 | final int Function() sodium_library_version_minor = libsodium 18 | .lookup>('sodium_library_version_minor') 19 | .asFunction(); 20 | final int Function() sodium_library_minimal = libsodium 21 | .lookup>('sodium_library_minimal') 22 | .asFunction(); 23 | 24 | final int Function() sodium_runtime_has_neon = libsodium 25 | .lookup>('sodium_runtime_has_neon') 26 | .asFunction(); 27 | final int Function() sodium_runtime_has_sse2 = libsodium 28 | .lookup>('sodium_runtime_has_sse2') 29 | .asFunction(); 30 | final int Function() sodium_runtime_has_sse3 = libsodium 31 | .lookup>('sodium_runtime_has_sse3') 32 | .asFunction(); 33 | final int Function() sodium_runtime_has_ssse3 = libsodium 34 | .lookup>('sodium_runtime_has_ssse3') 35 | .asFunction(); 36 | final int Function() sodium_runtime_has_sse41 = libsodium 37 | .lookup>('sodium_runtime_has_sse41') 38 | .asFunction(); 39 | final int Function() sodium_runtime_has_avx = libsodium 40 | .lookup>('sodium_runtime_has_avx') 41 | .asFunction(); 42 | final int Function() sodium_runtime_has_avx2 = libsodium 43 | .lookup>('sodium_runtime_has_avx2') 44 | .asFunction(); 45 | final int Function() sodium_runtime_has_avx512f = libsodium 46 | .lookup>('sodium_runtime_has_avx512f') 47 | .asFunction(); 48 | final int Function() sodium_runtime_has_pclmul = libsodium 49 | .lookup>('sodium_runtime_has_pclmul') 50 | .asFunction(); 51 | final int Function() sodium_runtime_has_aesni = libsodium 52 | .lookup>('sodium_runtime_has_aesni') 53 | .asFunction(); 54 | final int Function() sodium_runtime_has_rdrand = libsodium 55 | .lookup>('sodium_runtime_has_rdrand') 56 | .asFunction(); 57 | 58 | final Pointer Function( 59 | Pointer hex, int hex_maxlen, Pointer bin, int bin_len) 60 | sodium_bin2hex = libsodium 61 | .lookup< 62 | NativeFunction< 63 | Pointer Function(Pointer, IntPtr, Pointer, 64 | IntPtr)>>('sodium_bin2hex') 65 | .asFunction(); 66 | 67 | final int Function( 68 | Pointer bin, 69 | int bin_maxlen, 70 | Pointer hex, 71 | int hex_len, 72 | Pointer ignore, 73 | Pointer bin_len, 74 | Pointer hex_end) sodium_hex2bin = 75 | libsodium 76 | .lookup< 77 | NativeFunction< 78 | Int32 Function( 79 | Pointer, 80 | IntPtr, 81 | Pointer, 82 | IntPtr, 83 | Pointer, 84 | Pointer, 85 | Pointer)>>('sodium_hex2bin') 86 | .asFunction(); 87 | 88 | final int Function(int bin_len, int variant) sodium_base64_encoded_len = 89 | libsodium 90 | .lookup>( 91 | 'sodium_base64_encoded_len') 92 | .asFunction(); 93 | 94 | final Pointer Function(Pointer b64, int b64_maxlen, 95 | Pointer bin, int bin_len, int variant) sodium_bin2base64 = 96 | libsodium 97 | .lookup< 98 | NativeFunction< 99 | Pointer Function(Pointer, IntPtr, Pointer, 100 | IntPtr, Int32)>>('sodium_bin2base64') 101 | .asFunction(); 102 | 103 | final int Function( 104 | Pointer bin, 105 | int bin_maxlen, 106 | Pointer b64, 107 | int b64_len, 108 | Pointer ignore, 109 | Pointer bin_len, 110 | Pointer b64_end, 111 | int variant) sodium_base642bin = 112 | libsodium 113 | .lookup< 114 | NativeFunction< 115 | Int32 Function( 116 | Pointer, 117 | IntPtr, 118 | Pointer, 119 | IntPtr, 120 | Pointer, 121 | Pointer, 122 | Pointer, 123 | Int32)>>('sodium_base642bin') 124 | .asFunction(); 125 | 126 | final int Function(Pointer b1_, Pointer b2_, int len) 127 | sodium_memcmp = libsodium 128 | .lookup< 129 | NativeFunction< 130 | Int32 Function( 131 | Pointer, Pointer, IntPtr)>>('sodium_memcmp') 132 | .asFunction(); 133 | 134 | final int Function(Pointer padded_buflen_p, Pointer buf, 135 | int unpadded_buflen, int blocksize, int max_buflen) sodium_pad = 136 | libsodium 137 | .lookup< 138 | NativeFunction< 139 | Int32 Function(Pointer, Pointer, IntPtr, 140 | IntPtr, IntPtr)>>('sodium_pad') 141 | .asFunction(); 142 | 143 | final int Function(Pointer unpadded_buflen_p, Pointer buf, 144 | int padded_buflen, int blocksize) sodium_unpad = 145 | libsodium 146 | .lookup< 147 | NativeFunction< 148 | Int32 Function(Pointer, Pointer, IntPtr, 149 | IntPtr)>>('sodium_unpad') 150 | .asFunction(); 151 | } 152 | -------------------------------------------------------------------------------- /lib/src/cha_cha20_poly1305.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'dart:convert'; 3 | import 'detached_cipher.dart'; 4 | import 'sodium.dart'; 5 | 6 | /// The original ChaCha20-Poly1305 construction 7 | class ChaCha20Poly1305 { 8 | /// Generates a random key for use with the ChaCha20-Poly1305 construction. 9 | static Uint8List randomKey() => Sodium.cryptoAeadChacha20poly1305Keygen(); 10 | 11 | /// Generates a random nonce for use with the ChaCha20-Poly1305 construction. 12 | static Uint8List randomNonce() => 13 | Sodium.randombytesBuf(Sodium.cryptoAeadChacha20poly1305Npubbytes); 14 | 15 | /// Encrypts a message with optional additional data, a key and a nonce. 16 | static Uint8List encrypt(Uint8List value, Uint8List nonce, Uint8List key, 17 | {Uint8List? additionalData}) => 18 | Sodium.cryptoAeadChacha20poly1305Encrypt( 19 | value, additionalData, null, nonce, key); 20 | 21 | /// Verifies and decrypts a cipher text produced by encrypt. 22 | static Uint8List decrypt(Uint8List cipherText, Uint8List nonce, Uint8List key, 23 | {Uint8List? additionalData}) => 24 | Sodium.cryptoAeadChacha20poly1305Decrypt( 25 | null, cipherText, additionalData, nonce, key); 26 | 27 | /// Encrypts a string message with optional additional data, a key and a nonce. 28 | static Uint8List encryptString(String value, Uint8List nonce, Uint8List key, 29 | {String? additionalData}) => 30 | encrypt(utf8.encoder.convert(value), nonce, key, 31 | additionalData: additionalData != null 32 | ? utf8.encoder.convert(additionalData) 33 | : null); 34 | 35 | /// Verifies and decrypts a cipher text produced by encrypt. 36 | static String decryptString( 37 | Uint8List cipherText, Uint8List nonce, Uint8List key, 38 | {String? additionalData}) { 39 | final m = decrypt(cipherText, nonce, key, 40 | additionalData: additionalData != null 41 | ? utf8.encoder.convert(additionalData) 42 | : null); 43 | return utf8.decode(m); 44 | } 45 | 46 | /// Encrypts a message with optional additional data, a key and a nonce. Returns a detached cipher text and mac. 47 | static DetachedCipher encryptDetached( 48 | Uint8List value, Uint8List nonce, Uint8List key, 49 | {Uint8List? additionalData}) => 50 | Sodium.cryptoAeadChacha20poly1305EncryptDetached( 51 | value, additionalData, null, nonce, key); 52 | 53 | /// Verifies and decrypts a cipher text and mac produced by encrypt detached. 54 | static Uint8List decryptDetached( 55 | Uint8List cipher, Uint8List mac, Uint8List nonce, Uint8List key, 56 | {Uint8List? additionalData}) => 57 | Sodium.cryptoAeadChacha20poly1305DecryptDetached( 58 | null, cipher, mac, additionalData, nonce, key); 59 | 60 | /// Encrypts a string message with optional additional data, a key and a nonce. Returns a detached cipher text and mac. 61 | static DetachedCipher encryptStringDetached( 62 | String value, Uint8List nonce, Uint8List key, 63 | {String? additionalData}) => 64 | encryptDetached(utf8.encoder.convert(value), nonce, key, 65 | additionalData: additionalData != null 66 | ? utf8.encoder.convert(additionalData) 67 | : null); 68 | 69 | /// Verifies and decrypts a cipher text and mac produced by encrypt detached. 70 | static String decryptStringDetached( 71 | Uint8List cipher, Uint8List mac, Uint8List nonce, Uint8List key, 72 | {String? additionalData}) { 73 | final m = decryptDetached(cipher, mac, nonce, key, 74 | additionalData: additionalData != null 75 | ? utf8.encoder.convert(additionalData) 76 | : null); 77 | return utf8.decode(m); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /lib/src/cha_cha20_poly1305_ietf.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'dart:convert'; 3 | import 'detached_cipher.dart'; 4 | import 'sodium.dart'; 5 | 6 | /// The IETF variant of the ChaCha20-Poly1305 construction 7 | class ChaCha20Poly1305Ietf { 8 | /// Generates a random key for use with the IETF ChaCha20-Poly1305 construction. 9 | static Uint8List randomKey() => Sodium.cryptoAeadChacha20poly1305IetfKeygen(); 10 | 11 | /// Generates a random nonce for use with the IETF ChaCha20-Poly1305 construction. 12 | static Uint8List randomNonce() => 13 | Sodium.randombytesBuf(Sodium.cryptoAeadChacha20poly1305IetfNpubbytes); 14 | 15 | /// Encrypts a message with optional additional data, a key and a nonce. 16 | static Uint8List encrypt(Uint8List value, Uint8List nonce, Uint8List key, 17 | {Uint8List? additionalData}) => 18 | Sodium.cryptoAeadChacha20poly1305IetfEncrypt( 19 | value, additionalData, null, nonce, key); 20 | 21 | /// Verifies and decrypts a cipher text produced by encrypt. 22 | static Uint8List decrypt(Uint8List cipherText, Uint8List nonce, Uint8List key, 23 | {Uint8List? additionalData}) => 24 | Sodium.cryptoAeadChacha20poly1305IetfDecrypt( 25 | null, cipherText, additionalData, nonce, key); 26 | 27 | /// Encrypts a string message with optional additional data, a key and a nonce. 28 | static Uint8List encryptString(String value, Uint8List nonce, Uint8List key, 29 | {String? additionalData}) => 30 | encrypt(utf8.encoder.convert(value), nonce, key, 31 | additionalData: additionalData != null 32 | ? utf8.encoder.convert(additionalData) 33 | : null); 34 | 35 | /// Verifies and decrypts a cipher text produced by encrypt. 36 | static String decryptString( 37 | Uint8List cipherText, Uint8List nonce, Uint8List key, 38 | {String? additionalData}) { 39 | final m = decrypt(cipherText, nonce, key, 40 | additionalData: additionalData != null 41 | ? utf8.encoder.convert(additionalData) 42 | : null); 43 | return utf8.decode(m); 44 | } 45 | 46 | /// Encrypts a message with optional additional data, a key and a nonce. Returns a detached cipher text and mac. 47 | static DetachedCipher encryptDetached( 48 | Uint8List value, Uint8List nonce, Uint8List key, 49 | {Uint8List? additionalData}) => 50 | Sodium.cryptoAeadChacha20poly1305IetfEncryptDetached( 51 | value, additionalData, null, nonce, key); 52 | 53 | /// Verifies and decrypts a cipher text and mac produced by encrypt detached. 54 | static Uint8List decryptDetached( 55 | Uint8List cipher, Uint8List mac, Uint8List nonce, Uint8List key, 56 | {Uint8List? additionalData}) => 57 | Sodium.cryptoAeadChacha20poly1305IetfDecryptDetached( 58 | null, cipher, mac, additionalData, nonce, key); 59 | 60 | /// Encrypts a string message with optional additional data, a key and a nonce. Returns a detached cipher text and mac. 61 | static DetachedCipher encryptStringDetached( 62 | String value, Uint8List nonce, Uint8List key, 63 | {String? additionalData}) => 64 | encryptDetached(utf8.encoder.convert(value), nonce, key, 65 | additionalData: additionalData != null 66 | ? utf8.encoder.convert(additionalData) 67 | : null); 68 | 69 | /// Verifies and decrypts a cipher text and mac produced by encrypt detached. 70 | static String decryptStringDetached( 71 | Uint8List cipher, Uint8List mac, Uint8List nonce, Uint8List key, 72 | {String? additionalData}) { 73 | final m = decryptDetached(cipher, mac, nonce, key, 74 | additionalData: additionalData != null 75 | ? utf8.encoder.convert(additionalData) 76 | : null); 77 | return utf8.decode(m); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /lib/src/crypto_auth.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'dart:convert'; 3 | import 'sodium.dart'; 4 | 5 | /// Computes an authentication tag for a message and a secret key, and provides a way to verify that a given tag is valid for a given message and a key. 6 | class CryptoAuth { 7 | /// Generates a random key for use with authentication. 8 | static Uint8List randomKey() => Sodium.cryptoAuthKeygen(); 9 | 10 | /// Computes a tag for given value and key. 11 | static Uint8List compute(Uint8List value, Uint8List key) => 12 | Sodium.cryptoAuth(value, key); 13 | 14 | /// Verifies that the tag is valid for given value and key. 15 | static bool verify(Uint8List tag, Uint8List value, Uint8List key) => 16 | Sodium.cryptoAuthVerify(tag, value, key); 17 | 18 | /// Computes a tag for given string value and key. 19 | static Uint8List computeString(String value, Uint8List key) => 20 | compute(utf8.encoder.convert(value), key); 21 | 22 | /// Verifies that the tag is valid for given string value and key. 23 | static bool verifyString(Uint8List tag, String value, Uint8List key) => 24 | verify(tag, utf8.encoder.convert(value), key); 25 | } 26 | -------------------------------------------------------------------------------- /lib/src/crypto_box.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'dart:convert'; 3 | import 'detached_cipher.dart'; 4 | import 'key_pair.dart'; 5 | import 'sodium.dart'; 6 | 7 | /// Public-key authenticated encryption 8 | class CryptoBox { 9 | /// Generates a random secret key and a corresponding public key. 10 | static KeyPair randomKeys() => Sodium.cryptoBoxKeypair(); 11 | 12 | /// Generates a random seed for use in seedKeys. 13 | static Uint8List randomSeed() => 14 | Sodium.randombytesBuf(Sodium.cryptoBoxSeedbytes); 15 | 16 | /// Generates a secret key and a corresponding public key using given seed. 17 | static KeyPair seedKeys(Uint8List seed) => Sodium.cryptoBoxSeedKeypair(seed); 18 | 19 | /// Generates a random nonce for use with public key-authenticated encryption. 20 | static Uint8List randomNonce() => 21 | Sodium.randombytesBuf(Sodium.cryptoBoxNoncebytes); 22 | 23 | /// Computes a shared secret key given a public key and a secret key for use in precalculation interface. 24 | static Uint8List sharedSecret(Uint8List pk, Uint8List sk) => 25 | Sodium.cryptoBoxBeforenm(pk, sk); 26 | 27 | /// Encrypts a message with a key and a nonce. 28 | static Uint8List encrypt(Uint8List value, Uint8List nonce, 29 | Uint8List publicKey, Uint8List secretKey) => 30 | Sodium.cryptoBoxEasy(value, nonce, publicKey, secretKey); 31 | 32 | /// Verifies and decrypts a cipher text produced by encrypt. 33 | static Uint8List decrypt(Uint8List cipherText, Uint8List nonce, 34 | Uint8List publicKey, Uint8List secretKey) => 35 | Sodium.cryptoBoxOpenEasy(cipherText, nonce, publicKey, secretKey); 36 | 37 | /// Encrypts a string message with a key and a nonce. 38 | static Uint8List encryptString(String value, Uint8List nonce, 39 | Uint8List publicKey, Uint8List secretKey) => 40 | encrypt(utf8.encoder.convert(value), nonce, publicKey, secretKey); 41 | 42 | /// Verifies and decrypts a cipher text produced by encrypt. 43 | static String decryptString(Uint8List cipherText, Uint8List nonce, 44 | Uint8List publicKey, Uint8List secretKey) { 45 | final m = decrypt(cipherText, nonce, publicKey, secretKey); 46 | return utf8.decode(m); 47 | } 48 | 49 | /// Encrypts a message with a key and a nonce, returning the encrypted message and authentication tag 50 | static DetachedCipher encryptDetached(Uint8List value, Uint8List nonce, 51 | Uint8List publicKey, Uint8List secretKey) => 52 | Sodium.cryptoBoxDetached(value, nonce, publicKey, secretKey); 53 | 54 | /// Verifies and decrypts a detached cipher text and tag. 55 | static Uint8List decryptDetached(Uint8List cipher, Uint8List mac, 56 | Uint8List nonce, Uint8List publicKey, Uint8List secretKey) => 57 | Sodium.cryptoBoxOpenDetached(cipher, mac, nonce, publicKey, secretKey); 58 | 59 | /// Encrypts a string message with a key and a nonce, returning the encrypted message and authentication tag 60 | static DetachedCipher encryptStringDetached(String value, Uint8List nonce, 61 | Uint8List publicKey, Uint8List secretKey) => 62 | encryptDetached(utf8.encoder.convert(value), nonce, publicKey, secretKey); 63 | 64 | /// Verifies and decrypts a detached cipher text and tag. 65 | static String decryptStringDetached(Uint8List cipher, Uint8List mac, 66 | Uint8List nonce, Uint8List publicKey, Uint8List secretKey) { 67 | final m = decryptDetached(cipher, mac, nonce, publicKey, secretKey); 68 | return utf8.decode(m); 69 | } 70 | 71 | /// Encrypts a message with a key and a nonce. 72 | static Uint8List encryptAfternm( 73 | Uint8List value, Uint8List nonce, Uint8List k) => 74 | Sodium.cryptoBoxEasyAfternm(value, nonce, k); 75 | 76 | /// Verifies and decrypts a cipher text produced by encrypt. 77 | static Uint8List decryptAfternm( 78 | Uint8List cipherText, Uint8List nonce, Uint8List k) => 79 | Sodium.cryptoBoxOpenEasyAfternm(cipherText, nonce, k); 80 | 81 | /// Encrypts a string message with a key and a nonce. 82 | static Uint8List encryptStringAfternm( 83 | String value, Uint8List nonce, Uint8List k) => 84 | encryptAfternm(utf8.encoder.convert(value), nonce, k); 85 | 86 | /// Verifies and decrypts a cipher text produced by encrypt. 87 | static String decryptStringAfternm( 88 | Uint8List cipherText, Uint8List nonce, Uint8List k) { 89 | final m = decryptAfternm(cipherText, nonce, k); 90 | return utf8.decode(m); 91 | } 92 | 93 | /// Encrypts a message with a key and a nonce, returning the encrypted message and authentication tag 94 | static DetachedCipher encryptDetachedAfternm( 95 | Uint8List value, Uint8List nonce, Uint8List k) => 96 | Sodium.cryptoBoxDetachedAfternm(value, nonce, k); 97 | 98 | /// Verifies and decrypts a detached cipher text and tag. 99 | static Uint8List decryptDetachedAfternm( 100 | Uint8List cipher, Uint8List mac, Uint8List nonce, Uint8List k) => 101 | Sodium.cryptoBoxOpenDetachedAfternm(cipher, mac, nonce, k); 102 | 103 | /// Encrypts a string message with a key and a nonce, returning the encrypted message and authentication tag 104 | static DetachedCipher encryptStringDetachedAfternm( 105 | String value, Uint8List nonce, Uint8List k) => 106 | encryptDetachedAfternm(utf8.encoder.convert(value), nonce, k); 107 | 108 | /// Verifies and decrypts a detached cipher text and tag. 109 | static String decryptStringDetachedAfternm( 110 | Uint8List cipher, Uint8List mac, Uint8List nonce, Uint8List k) { 111 | final m = decryptDetachedAfternm(cipher, mac, nonce, k); 112 | return utf8.decode(m); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /lib/src/crypto_sign.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'dart:convert'; 3 | import 'package:ffi/ffi.dart'; 4 | import 'key_pair.dart'; 5 | import 'sodium.dart'; 6 | 7 | /// Computes a signature for a message using a secret key, and provides verification using a public key. 8 | class CryptoSign { 9 | /// Generates a random key for use with public-key signatures. 10 | static KeyPair randomKeys() => Sodium.cryptoSignKeypair(); 11 | 12 | /// Generates a random seed for use in seedKeys. 13 | static Uint8List randomSeed() { 14 | return Sodium.randombytesBuf(Sodium.cryptoSignSeedbytes); 15 | } 16 | 17 | /// Generates a secret key and a corresponding public key for given seed. 18 | static KeyPair seedKeys(Uint8List seed) => Sodium.cryptoSignSeedKeypair(seed); 19 | 20 | /// Prepends a signature to specified message for given secret key. 21 | static Uint8List sign(Uint8List message, Uint8List secretKey) => 22 | Sodium.cryptoSign(message, secretKey); 23 | 24 | /// Prepends a signature to specified string message for given secret key. 25 | static Uint8List signString(String message, Uint8List secretKey) => 26 | sign(utf8.encoder.convert(message), secretKey); 27 | 28 | /// Checks the signed message using given public key and returns the message with the signature removed. 29 | static Uint8List open(Uint8List signedMessage, Uint8List publicKey) => 30 | Sodium.cryptoSignOpen(signedMessage, publicKey); 31 | 32 | /// Checks the signed message using given public key and returns the string message with the signature removed. 33 | static String openString(Uint8List signedMessage, Uint8List publicKey) { 34 | final m = open(signedMessage, publicKey); 35 | return utf8.decode(m); 36 | } 37 | 38 | /// Computes a signature for given message and secret key. 39 | static Uint8List signDetached(Uint8List message, Uint8List secretKey) => 40 | Sodium.cryptoSignDetached(message, secretKey); 41 | 42 | /// Computes a signature for given string message and secret key. 43 | static Uint8List signStringDetached(String message, Uint8List secretKey) => 44 | signDetached(utf8.encoder.convert(message), secretKey); 45 | 46 | /// Verifies whether the signature is valid for given message using the signer's public key. 47 | static bool verify( 48 | Uint8List signature, Uint8List message, Uint8List publicKey) => 49 | Sodium.cryptoSignVerifyDetached(signature, message, publicKey) == 0; 50 | 51 | /// Verifies whether the signature is valid for given string message using the signer's public key. 52 | static bool verifyString( 53 | Uint8List signature, String message, Uint8List publicKey) => 54 | verify(signature, utf8.encoder.convert(message), publicKey); 55 | 56 | /// Computes a signature for given stream and secret key. 57 | static Future signStream( 58 | Stream stream, Uint8List secretKey) async { 59 | final state = Sodium.cryptoSignInit(); 60 | try { 61 | await for (var value in stream) { 62 | Sodium.cryptoSignUpdate(state, value); 63 | } 64 | return Sodium.cryptoSignFinalCreate(state, secretKey); 65 | } finally { 66 | calloc.free(state); 67 | } 68 | } 69 | 70 | /// Verifies whether the signature is valid for given stream using the signer's public key. 71 | static Future verifyStream(Uint8List signature, 72 | Stream stream, Uint8List publicKey) async { 73 | final state = Sodium.cryptoSignInit(); 74 | try { 75 | await for (var value in stream) { 76 | Sodium.cryptoSignUpdate(state, value); 77 | } 78 | return Sodium.cryptoSignFinalVerify(state, signature, publicKey) == 0; 79 | } finally { 80 | calloc.free(state); 81 | } 82 | } 83 | 84 | /// Computes a signature for given stream of strings and secret key. 85 | static Future signStrings( 86 | Stream stream, Uint8List secretKey) async { 87 | final state = Sodium.cryptoSignInit(); 88 | try { 89 | await for (var value in stream) { 90 | Sodium.cryptoSignUpdate(state, utf8.encoder.convert(value)); 91 | } 92 | return Sodium.cryptoSignFinalCreate(state, secretKey); 93 | } finally { 94 | calloc.free(state); 95 | } 96 | } 97 | 98 | /// Verifies whether the signature is valid for given stream using the signer's public key. 99 | static Future verifyStrings( 100 | Uint8List signature, Stream stream, Uint8List publicKey) async { 101 | final state = Sodium.cryptoSignInit(); 102 | try { 103 | await for (var value in stream) { 104 | Sodium.cryptoSignUpdate(state, utf8.encoder.convert(value)); 105 | } 106 | return Sodium.cryptoSignFinalVerify(state, signature, publicKey) == 0; 107 | } finally { 108 | calloc.free(state); 109 | } 110 | } 111 | 112 | /// Extracts the seed from the secret key 113 | static Uint8List extractSeed(Uint8List secretKey) => 114 | Sodium.cryptoSignEd25519SkToSeed(secretKey); 115 | 116 | /// Extracts the public key from the secret key 117 | static Uint8List extractPublicKey(Uint8List secretKey) => 118 | Sodium.cryptoSignEd25519SkToPk(secretKey); 119 | } 120 | -------------------------------------------------------------------------------- /lib/src/crypto_stream.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'dart:convert'; 3 | import 'sodium.dart'; 4 | 5 | /// Generate deterministic streams of random data based off a secret key and random nonce. 6 | class CryptoStream { 7 | /// Generates a random key for use with crypto stream. 8 | static Uint8List randomKey() => Sodium.cryptoStreamKeygen(); 9 | 10 | /// Generates a random nonce for use with crypto stream. 11 | static Uint8List randomNonce() => 12 | Sodium.randombytesBuf(Sodium.cryptoStreamNoncebytes); 13 | 14 | /// Generates pseudo random bytes for given nonce and secret key. 15 | static Uint8List stream(int clen, Uint8List nonce, Uint8List key) => 16 | Sodium.cryptoStream(clen, nonce, key); 17 | 18 | /// Encrypts specified value using a nonce and a secret key. 19 | static Uint8List xor(Uint8List value, Uint8List nonce, Uint8List key) => 20 | Sodium.cryptoStreamXor(value, nonce, key); 21 | 22 | /// Encrypts specified string value using a nonce and a secret key. 23 | static Uint8List xorString(String value, Uint8List nonce, Uint8List key) => 24 | xor(utf8.encoder.convert(value), nonce, key); 25 | } 26 | -------------------------------------------------------------------------------- /lib/src/detached_cipher.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | 3 | /// Detached cipher and associated authentication tag. 4 | class DetachedCipher { 5 | final Uint8List c, mac; 6 | 7 | const DetachedCipher({required this.c, required this.mac}); 8 | } 9 | -------------------------------------------------------------------------------- /lib/src/extensions.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'dart:io'; 3 | import 'dart:typed_data'; 4 | import 'package:ffi/ffi.dart'; 5 | import 'sodium_exception.dart'; 6 | 7 | extension Uint8Pointer on Pointer { 8 | Uint8List toList(int length) { 9 | final builder = BytesBuilder(); 10 | for (var i = 0; i < length; i++) { 11 | builder.addByte(this[i]); 12 | } 13 | return builder.takeBytes(); 14 | } 15 | 16 | Uint8List toNullTerminatedList(int maxLength) { 17 | final builder = BytesBuilder(); 18 | for (var i = 0; i < maxLength; i++) { 19 | builder.addByte(this[i]); 20 | if (this[i] == 0) { 21 | break; 22 | } 23 | } 24 | return builder.takeBytes(); 25 | } 26 | } 27 | 28 | extension Uint8ListExtensions on Uint8List { 29 | Pointer toPointer({int? size}) { 30 | final p = calloc(size ?? this.length); 31 | p.asTypedList(size ?? this.length).setAll(0, this); 32 | return p; 33 | } 34 | 35 | Uint8List toNullTerminatedList({int? maxLength}) { 36 | if ((maxLength == null || this.length < maxLength) && this.last != 0) { 37 | return new Uint8List(this.length + 1)..setAll(0, this); 38 | } 39 | 40 | // return unchanged 41 | return this; 42 | } 43 | } 44 | 45 | extension Result on int { 46 | void mustSucceed(String funcName) { 47 | if (this != 0) { 48 | throw SodiumException('$funcName failed with $this'); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /lib/src/generic_hash.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'dart:convert'; 3 | import 'package:ffi/ffi.dart'; 4 | import 'sodium.dart'; 5 | 6 | /// Computes a fixed-length fingerprint for an arbitrary long message using the BLAKE2b algorithm. 7 | class GenericHash { 8 | /// Generates a random key for use with generic hashing. 9 | static Uint8List randomKey() => Sodium.cryptoGenerichashKeygen(); 10 | 11 | /// Computes a generic hash of specified length for given value and optional key. 12 | static Uint8List hash(Uint8List value, {Uint8List? key, int? outlen}) { 13 | outlen ??= Sodium.cryptoGenerichashBytes; 14 | return Sodium.cryptoGenerichash(outlen, value, key); 15 | } 16 | 17 | /// Computes a generic hash of specified length for given string value and optional key. 18 | static Uint8List hashString(String value, {Uint8List? key, int? outlen}) => 19 | hash(utf8.encoder.convert(value), key: key, outlen: outlen); 20 | 21 | /// Computes a generic hash of specified length for given stream of byte values and optional key. 22 | static Future hashStream(Stream stream, 23 | {Uint8List? key, int? outlen}) async { 24 | outlen ??= Sodium.cryptoGenerichashBytes; 25 | final state = Sodium.cryptoGenerichashInit(key, outlen); 26 | try { 27 | await for (var value in stream) { 28 | Sodium.cryptoGenerichashUpdate(state, value); 29 | } 30 | return Sodium.cryptoGenerichashFinal(state, outlen); 31 | } finally { 32 | calloc.free(state); 33 | } 34 | } 35 | 36 | /// Computes a generic hash of specified length for given stream of string values and optional key. 37 | static Future hashStrings(Stream stream, 38 | {Uint8List? key, int? outlen}) async { 39 | outlen ??= Sodium.cryptoGenerichashBytes; 40 | final state = Sodium.cryptoGenerichashInit(key, outlen); 41 | try { 42 | await for (var value in stream) { 43 | Sodium.cryptoGenerichashUpdate(state, utf8.encoder.convert(value)); 44 | } 45 | return Sodium.cryptoGenerichashFinal(state, outlen); 46 | } finally { 47 | calloc.free(state); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /lib/src/hash.dart: -------------------------------------------------------------------------------- 1 | import 'dart:convert'; 2 | import 'dart:typed_data'; 3 | import 'sodium.dart'; 4 | 5 | /// SHA-512 hash functions. 6 | class Hash { 7 | /// Computes a hash for given value. 8 | static Uint8List hash(Uint8List value) => Sodium.cryptoHash(value); 9 | 10 | /// Computes a hash for given string value. 11 | static Uint8List hashString(String value) => 12 | hash(utf8.encoder.convert(value)); 13 | } 14 | -------------------------------------------------------------------------------- /lib/src/init_push_result.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'dart:typed_data'; 3 | 4 | class InitPushResult { 5 | final Pointer state; 6 | final Uint8List header; 7 | 8 | const InitPushResult({required this.state, required this.header}); 9 | } 10 | -------------------------------------------------------------------------------- /lib/src/key_derivation.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'dart:convert'; 3 | import 'sodium.dart'; 4 | 5 | /// Derive secret subkeys from a single master key. 6 | class KeyDerivation { 7 | /// Generates a random master key for use with key derivation. 8 | static Uint8List randomKey() => Sodium.cryptoKdfKeygen(); 9 | 10 | /// Derives a subkey from given master key. 11 | static Uint8List derive(Uint8List masterKey, int subKeyId, 12 | {int? subKeyLength, String context = '00000000'}) { 13 | subKeyLength ??= Sodium.cryptoKdfBytesMin; 14 | return Sodium.cryptoKdfDeriveFromKey( 15 | subKeyLength, subKeyId, utf8.encoder.convert(context), masterKey); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /lib/src/key_exchange.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'key_pair.dart'; 3 | import 'session_keys.dart'; 4 | import 'sodium.dart'; 5 | 6 | /// Key exchange API, securely compute a set of shared keys. 7 | class KeyExchange { 8 | /// Generates a random secret key and a corresponding public key. 9 | static KeyPair randomKeys() => Sodium.cryptoKxKeypair(); 10 | 11 | /// Generates a random seed for use in seedKeys. 12 | static Uint8List randomSeed() => 13 | Sodium.randombytesBuf(Sodium.cryptoKxSeedbytes); 14 | 15 | /// Generates a secret key and a corresponding public key using given seed. 16 | static KeyPair seedKeys(Uint8List seed) => Sodium.cryptoKxSeedKeypair(seed); 17 | 18 | /// Computes a pair of shared keys using the client's public key, the client's secret key and the server's public key. 19 | static SessionKeys computeClientSessionKeys( 20 | KeyPair clientPair, Uint8List serverPublicKey) => 21 | Sodium.cryptoKxClientSessionKeys( 22 | clientPair.pk, clientPair.sk, serverPublicKey); 23 | 24 | /// Computes a pair of shared keys using the server's public key, the server's secret key and the client's public key. 25 | static SessionKeys computeServerSessionKeys( 26 | KeyPair serverPair, Uint8List clientPublicKey) => 27 | Sodium.cryptoKxServerSessionKeys( 28 | serverPair.pk, serverPair.sk, clientPublicKey); 29 | } 30 | -------------------------------------------------------------------------------- /lib/src/key_pair.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | 3 | /// Represents a secret key and a corresponding public key. 4 | class KeyPair { 5 | final Uint8List pk, sk; 6 | 7 | const KeyPair({required this.pk, required this.sk}); 8 | } 9 | -------------------------------------------------------------------------------- /lib/src/onetime_auth.dart: -------------------------------------------------------------------------------- 1 | import 'dart:convert'; 2 | import 'dart:typed_data'; 3 | import 'package:ffi/ffi.dart'; 4 | import 'sodium.dart'; 5 | 6 | /// Secret-key single-message authentication using Poly1305. 7 | class OnetimeAuth { 8 | /// Generates a random key for use in onetime authentication. 9 | static Uint8List randomKey() => Sodium.cryptoOnetimeauthKeygen(); 10 | 11 | /// Computes a tag for given value and key. 12 | static Uint8List compute(Uint8List value, Uint8List key) => 13 | Sodium.cryptoOnetimeauth(value, key); 14 | 15 | /// Verifies that the tag is valid for given value and key. 16 | static bool verify(Uint8List tag, Uint8List value, Uint8List key) => 17 | Sodium.cryptoOnetimeauthVerify(tag, value, key); 18 | 19 | /// Computes a tag for given string value and key. 20 | static Uint8List computeString(String value, Uint8List key) => 21 | compute(utf8.encoder.convert(value), key); 22 | 23 | /// Verifies that the tag is valid for given string value and key. 24 | static bool verifyString(Uint8List tag, String value, Uint8List key) => 25 | verify(tag, utf8.encoder.convert(value), key); 26 | 27 | // Computes a tag for given stream of strings and key. 28 | static Future computeStrings( 29 | Stream stream, Uint8List key) async { 30 | final state = Sodium.cryptoOnetimeauthInit(key); 31 | try { 32 | await for (var value in stream) { 33 | Sodium.cryptoOnetimeauthUpdate(state, utf8.encoder.convert(value)); 34 | } 35 | return Sodium.cryptoOnetimeauthFinal(state); 36 | } finally { 37 | calloc.free(state); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /lib/src/password_hash.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'dart:convert'; 3 | import 'sodium.dart'; 4 | 5 | /// Defines the supported password hashing algorithms. 6 | enum PasswordHashAlgorithm { 7 | /// The recommended algoritm. 8 | Default, 9 | 10 | /// Version 1.3 of the Argon2i algorithm. 11 | Argon2i13, 12 | 13 | /// Version 1.3 of the Argon2id algorithm. 14 | Argon2id13 15 | } 16 | 17 | /// Provides an Argon2 password hashing scheme implementation. 18 | class PasswordHash { 19 | static int _alg(PasswordHashAlgorithm alg) { 20 | switch (alg) { 21 | case PasswordHashAlgorithm.Argon2i13: 22 | return Sodium.cryptoPwhashAlgArgon2i13; 23 | case PasswordHashAlgorithm.Argon2id13: 24 | return Sodium.cryptoPwhashAlgArgon2id13; 25 | default: 26 | return Sodium.cryptoPwhashAlgDefault; 27 | } 28 | } 29 | 30 | /// Generates a random salt for use in password hashing. 31 | static Uint8List randomSalt() => 32 | Sodium.randombytesBuf(Sodium.cryptoPwhashSaltbytes); 33 | 34 | /// Derives a hash from given password and salt. 35 | static Uint8List hash(Uint8List password, Uint8List salt, 36 | {int? outlen, 37 | int? opslimit, 38 | int? memlimit, 39 | PasswordHashAlgorithm alg = PasswordHashAlgorithm.Default}) { 40 | outlen ??= Sodium.cryptoPwhashBytesMin; 41 | opslimit ??= Sodium.cryptoPwhashOpslimitInteractive; 42 | memlimit ??= Sodium.cryptoPwhashMemlimitInteractive; 43 | 44 | return Sodium.cryptoPwhash( 45 | outlen, password, salt, opslimit, memlimit, _alg(alg)); 46 | } 47 | 48 | /// Derives a hash from given string password and salt. 49 | static Uint8List hashString(String password, Uint8List salt, 50 | {int? outlen, 51 | int? opslimit, 52 | int? memlimit, 53 | PasswordHashAlgorithm alg = PasswordHashAlgorithm.Default}) => 54 | hash(utf8.encoder.convert(password), salt, 55 | outlen: outlen, opslimit: opslimit, memlimit: memlimit); 56 | 57 | /// Computes a password verification string for given password. 58 | static String hashStorage(Uint8List password, 59 | {int? opslimit, int? memlimit}) { 60 | opslimit ??= Sodium.cryptoPwhashOpslimitInteractive; 61 | memlimit ??= Sodium.cryptoPwhashMemlimitInteractive; 62 | 63 | final str = Sodium.cryptoPwhashStr(password, opslimit, memlimit); 64 | // ascii decode null-terminated string 65 | return ascii.decode(str.takeWhile((c) => c != 0).toList()); 66 | } 67 | 68 | /// Computes a password verification string for given string password. 69 | static String hashStringStorage(String password, 70 | {int? opslimit, int? memlimit}) => 71 | hashStorage(utf8.encoder.convert(password), 72 | opslimit: opslimit, memlimit: memlimit); 73 | 74 | /// Computes a password verification string for given password in moderate mode. 75 | static String hashStringStorageModerate(String password) => 76 | hashStringStorage(password, 77 | opslimit: Sodium.cryptoPwhashOpslimitModerate, 78 | memlimit: Sodium.cryptoPwhashMemlimitModerate); 79 | 80 | /// Computes a password verification string for given password in sensitive mode. 81 | static String hashStringStorageSensitive(String password) => 82 | hashStringStorage(password, 83 | opslimit: Sodium.cryptoPwhashOpslimitSensitive, 84 | memlimit: Sodium.cryptoPwhashMemlimitSensitive); 85 | 86 | /// Verifies that the storage is a valid password verification string for given password. 87 | static bool verifyStorage(String storage, String password) { 88 | final str = ascii.encode(storage); 89 | final passwd = utf8.encoder.convert(password); 90 | 91 | return Sodium.cryptoPwhashStrVerify(str, passwd) == 0; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /lib/src/pull_result.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | 3 | class PullResult { 4 | final Uint8List m; 5 | final int tag; 6 | 7 | const PullResult({required this.m, required this.tag}); 8 | } 9 | -------------------------------------------------------------------------------- /lib/src/random_bytes.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'sodium.dart'; 3 | 4 | /// Provides a set of functions to generate unpredictable data, suitable for creating secret keys 5 | class RandomBytes { 6 | /// Generates a random seed for use in bufferDeterministic. 7 | static Uint8List randomSeed() => 8 | Sodium.randombytesBuf(Sodium.randombytesSeedbytes); 9 | 10 | /// Generates an unpredictable value between 0 and 0xffffffff (included). 11 | static int random() => Sodium.randombytesRandom(); 12 | 13 | /// Generates an unpredictable value between 0 and upperBound (excluded). 14 | static int uniform(int upperBound) => Sodium.randombytesUniform(upperBound); 15 | 16 | /// Generates an unpredictable sequence of bytes of specified size. 17 | static Uint8List buffer(int size) => Sodium.randombytesBuf(size); 18 | 19 | /// Generates a sequence of bytes of specified size. For a given seed, this function will always output the same sequence. 20 | static Uint8List bufferDeterministic(int size, Uint8List seed) => 21 | Sodium.randombytesBufDeterministic(size, seed); 22 | } 23 | -------------------------------------------------------------------------------- /lib/src/scalar_mult.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'sodium.dart'; 3 | 4 | /// Performs scalar multiplication of elliptic curve points 5 | class ScalarMult { 6 | /// Generates a random secret key. 7 | static Uint8List randomSecretKey() => 8 | Sodium.randombytesBuf(Sodium.cryptoScalarmultScalarbytes); 9 | 10 | /// Computes a public key given specified secret key. 11 | static Uint8List computePublicKey(Uint8List secretKey) => 12 | Sodium.cryptoScalarmultBase(secretKey); 13 | 14 | /// Computes a shared secret given a user's secret key and another user's public key. 15 | static Uint8List computeSharedSecret( 16 | Uint8List secretKey, Uint8List publicKey) => 17 | Sodium.cryptoScalarmult(secretKey, publicKey); 18 | } 19 | -------------------------------------------------------------------------------- /lib/src/sealed_box.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'dart:convert'; 3 | import 'key_pair.dart'; 4 | import 'sodium.dart'; 5 | 6 | /// Anonymously send messages to a recipient given its public key. 7 | class SealedBox { 8 | /// Generates a random secret key and a corresponding public key. 9 | static KeyPair randomKeys() => Sodium.cryptoBoxKeypair(); 10 | 11 | /// Encrypts a value for a recipient having specified public key. 12 | static Uint8List seal(Uint8List value, Uint8List publicKey) => 13 | Sodium.cryptoBoxSeal(value, publicKey); 14 | 15 | /// Decrypts the ciphertext using given keypair. 16 | static Uint8List open(Uint8List cipher, KeyPair keys) => 17 | Sodium.cryptoBoxSealOpen(cipher, keys.pk, keys.sk); 18 | 19 | /// Encrypts a string message for a recipient having specified public key. 20 | static Uint8List sealString(String value, Uint8List publicKey) => 21 | seal(utf8.encoder.convert(value), publicKey); 22 | 23 | /// Decrypts the ciphertext using given keypair. 24 | static String openString(Uint8List cipher, KeyPair keys) { 25 | final m = open(cipher, keys); 26 | return utf8.decode(m); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /lib/src/secret_box.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'dart:convert'; 3 | import 'detached_cipher.dart'; 4 | import 'sodium.dart'; 5 | 6 | /// Encrypts a message with a key and a nonce and computes an authentication tag. 7 | class SecretBox { 8 | /// Generates a random key for use with secret key encryption. 9 | static Uint8List randomKey() => Sodium.cryptoSecretboxKeygen(); 10 | 11 | /// Generates a random nonce for use with secret key encryption. 12 | static Uint8List randomNonce() => 13 | Sodium.randombytesBuf(Sodium.cryptoSecretboxNoncebytes); 14 | 15 | /// Encrypts a message with a key and a nonce. 16 | static Uint8List encrypt(Uint8List value, Uint8List nonce, Uint8List key) => 17 | Sodium.cryptoSecretboxEasy(value, nonce, key); 18 | 19 | /// Verifies and decrypts a cipher text produced by encrypt. 20 | static Uint8List decrypt( 21 | Uint8List cipherText, Uint8List nonce, Uint8List key) => 22 | Sodium.cryptoSecretboxOpenEasy(cipherText, nonce, key); 23 | 24 | /// Encrypts a string message with a key and a nonce. 25 | static Uint8List encryptString( 26 | String value, Uint8List nonce, Uint8List key) => 27 | encrypt(utf8.encoder.convert(value), nonce, key); 28 | 29 | /// Verifies and decrypts a cipher text produced by encrypt. 30 | static String decryptString( 31 | Uint8List cipherText, Uint8List nonce, Uint8List key) { 32 | final m = decrypt(cipherText, nonce, key); 33 | return utf8.decode(m); 34 | } 35 | 36 | /// Encrypts a message with a key and a nonce, returning the encrypted message and authentication tag 37 | static DetachedCipher encryptDetached( 38 | Uint8List value, Uint8List nonce, Uint8List key) => 39 | Sodium.cryptoSecretboxDetached(value, nonce, key); 40 | 41 | /// Verifies and decrypts a detached cipher text and tag. 42 | static Uint8List decryptDetached( 43 | Uint8List cipher, Uint8List mac, Uint8List nonce, Uint8List key) => 44 | Sodium.cryptoSecretboxOpenDetached(cipher, mac, nonce, key); 45 | 46 | /// Encrypts a string message with a key and a nonce, returning the encrypted message and authentication tag 47 | static DetachedCipher encryptStringDetached( 48 | String value, Uint8List nonce, Uint8List key) => 49 | encryptDetached(utf8.encoder.convert(value), nonce, key); 50 | 51 | /// Verifies and decrypts a detached cipher text and tag. 52 | static String decryptStringDetached( 53 | Uint8List cipher, Uint8List mac, Uint8List nonce, Uint8List key) { 54 | final m = decryptDetached(cipher, mac, nonce, key); 55 | return utf8.decode(m); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /lib/src/session_keys.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | 3 | /// Represents a pair of shared keys. The shared secret key rx should be used 4 | /// by the client to receive data from the server, whereas tx should be used 5 | /// for data flowing in the opposite direction. 6 | class SessionKeys { 7 | final Uint8List rx, tx; 8 | 9 | const SessionKeys({required this.rx, required this.tx}); 10 | } 11 | -------------------------------------------------------------------------------- /lib/src/short_hash.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'dart:convert'; 3 | import 'sodium.dart'; 4 | 5 | /// Computes short hashes using the SipHash-2-4 algorithm. 6 | class ShortHash { 7 | /// Generates a random key for use with short hashing. 8 | static Uint8List randomKey() => Sodium.cryptoShorthashKeygen(); 9 | 10 | /// Computes a fixed-size fingerprint for given value and key. 11 | static Uint8List hash(Uint8List value, Uint8List key) => 12 | Sodium.cryptoShorthash(value, key); 13 | 14 | /// Computes a fixed-size fingerprint for given string value and key. 15 | static Uint8List hashString(String value, Uint8List key) => 16 | hash(utf8.encoder.convert(value), key); 17 | } 18 | -------------------------------------------------------------------------------- /lib/src/sodium_exception.dart: -------------------------------------------------------------------------------- 1 | /// Thrown when a sodium operation fails. 2 | class SodiumException { 3 | final String message; 4 | SodiumException(this.message); 5 | 6 | /// Returns a string representation of this object. 7 | String toString() => message; 8 | } 9 | -------------------------------------------------------------------------------- /lib/src/x_cha_cha20_poly1305_ietf.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'dart:convert'; 3 | import 'detached_cipher.dart'; 4 | import 'sodium.dart'; 5 | 6 | /// The XChaCha20-Poly1305 construction 7 | class XChaCha20Poly1305Ietf { 8 | /// Generates a random key for use with the XChaCha20-Poly1305 construction. 9 | static Uint8List randomKey() => 10 | Sodium.cryptoAeadXchacha20poly1305IetfKeygen(); 11 | 12 | /// Generates a random nonce for use with the XChaCha20-Poly1305 construction. 13 | static Uint8List randomNonce() => 14 | Sodium.randombytesBuf(Sodium.cryptoAeadXchacha20poly1305IetfNpubbytes); 15 | 16 | /// Encrypts a message with optional additional data, a key and a nonce. 17 | static Uint8List encrypt(Uint8List value, Uint8List nonce, Uint8List key, 18 | {Uint8List? additionalData}) => 19 | Sodium.cryptoAeadXchacha20poly1305IetfEncrypt( 20 | value, additionalData, null, nonce, key); 21 | 22 | /// Verifies and decrypts a cipher text produced by encrypt. 23 | static Uint8List decrypt(Uint8List cipherText, Uint8List nonce, Uint8List key, 24 | {Uint8List? additionalData}) => 25 | Sodium.cryptoAeadXchacha20poly1305IetfDecrypt( 26 | null, cipherText, additionalData, nonce, key); 27 | 28 | /// Encrypts a string message with optional additional data, a key and a nonce. 29 | static Uint8List encryptString(String value, Uint8List nonce, Uint8List key, 30 | {String? additionalData}) => 31 | encrypt(utf8.encoder.convert(value), nonce, key, 32 | additionalData: additionalData != null 33 | ? utf8.encoder.convert(additionalData) 34 | : null); 35 | 36 | /// Verifies and decrypts a cipher text produced by encrypt. 37 | static String decryptString( 38 | Uint8List cipherText, Uint8List nonce, Uint8List key, 39 | {String? additionalData}) { 40 | final m = decrypt(cipherText, nonce, key, 41 | additionalData: additionalData != null 42 | ? utf8.encoder.convert(additionalData) 43 | : null); 44 | return utf8.decode(m); 45 | } 46 | 47 | /// Encrypts a message with optional additional data, a key and a nonce. Returns a detached cipher text and mac. 48 | static DetachedCipher encryptDetached( 49 | Uint8List value, Uint8List nonce, Uint8List key, 50 | {Uint8List? additionalData}) => 51 | Sodium.cryptoAeadXchacha20poly1305IetfEncryptDetached( 52 | value, additionalData, null, nonce, key); 53 | 54 | /// Verifies and decrypts a cipher text and mac produced by encrypt detached. 55 | static Uint8List decryptDetached( 56 | Uint8List cipher, Uint8List mac, Uint8List nonce, Uint8List key, 57 | {Uint8List? additionalData}) => 58 | Sodium.cryptoAeadXchacha20poly1305IetfDecryptDetached( 59 | null, cipher, mac, additionalData, nonce, key); 60 | 61 | /// Encrypts a string message with optional additional data, a key and a nonce. Returns a detached cipher text and mac. 62 | static DetachedCipher encryptStringDetached( 63 | String value, Uint8List nonce, Uint8List key, 64 | {String? additionalData}) => 65 | encryptDetached(utf8.encoder.convert(value), nonce, key, 66 | additionalData: additionalData != null 67 | ? utf8.encoder.convert(additionalData) 68 | : null); 69 | 70 | /// Verifies and decrypts a cipher text and mac produced by encrypt detached. 71 | static String decryptStringDetached( 72 | Uint8List cipher, Uint8List mac, Uint8List nonce, Uint8List key, 73 | {String? additionalData}) { 74 | final m = decryptDetached(cipher, mac, nonce, key, 75 | additionalData: additionalData != null 76 | ? utf8.encoder.convert(additionalData) 77 | : null); 78 | return utf8.decode(m); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: flutter_sodium 2 | description: Flutter bindings for libsodium, a modern, easy-to-use crypto library. 3 | version: 0.2.0 4 | homepage: https://github.com/firstfloorsoftware/flutter_sodium 5 | 6 | environment: 7 | sdk: ">=2.12.0 <3.0.0" 8 | flutter: ">=1.12.0" 9 | 10 | dependencies: 11 | flutter: 12 | sdk: flutter 13 | ffi: ^1.0.0 14 | 15 | dev_dependencies: 16 | flutter_test: 17 | sdk: flutter 18 | 19 | flutter: 20 | plugin: 21 | platforms: 22 | android: 23 | package: com.firstfloorsoftware.flutter_sodium 24 | pluginClass: FlutterSodiumPlugin 25 | ios: 26 | pluginClass: FlutterSodiumPlugin -------------------------------------------------------------------------------- /test/flutter_sodium_test.dart: -------------------------------------------------------------------------------- 1 | import 'dart:convert'; 2 | import 'package:flutter_test/flutter_test.dart'; 3 | import 'package:flutter_sodium/flutter_sodium.dart'; 4 | 5 | void main() { 6 | setUp(() {}); 7 | 8 | tearDown(() {}); 9 | 10 | group('crypto_pwhash', () { 11 | test('consts', () { 12 | expect(Sodium.cryptoPwhashAlgArgon2i13, 1); 13 | expect(Sodium.cryptoPwhashAlgArgon2id13, 2); 14 | expect(Sodium.cryptoPwhashAlgDefault, 2); 15 | expect(Sodium.cryptoPwhashBytesMax, 4294967295); 16 | expect(Sodium.cryptoPwhashBytesMin, 16); 17 | expect(Sodium.cryptoPwhashMemlimitInteractive, 67108864); 18 | expect(Sodium.cryptoPwhashMemlimitMax, 4398046510080); 19 | expect(Sodium.cryptoPwhashMemlimitMin, 8192); 20 | expect(Sodium.cryptoPwhashMemlimitModerate, 268435456); 21 | expect(Sodium.cryptoPwhashMemlimitSensitive, 1073741824); 22 | expect(Sodium.cryptoPwhashOpslimitInteractive, 2); 23 | expect(Sodium.cryptoPwhashOpslimitMax, 4294967295); 24 | expect(Sodium.cryptoPwhashOpslimitMin, 1); 25 | expect(Sodium.cryptoPwhashOpslimitModerate, 3); 26 | expect(Sodium.cryptoPwhashOpslimitSensitive, 4); 27 | expect(Sodium.cryptoPwhashPasswdMax, 4294967295); 28 | expect(Sodium.cryptoPwhashPasswdMin, 0); 29 | expect(Sodium.cryptoPwhashPrimitive, 'argon2i'); 30 | expect(Sodium.cryptoPwhashSaltbytes, 16); 31 | expect(Sodium.cryptoPwhashStrbytes, 128); 32 | expect(Sodium.cryptoPwhashStrprefix, '\$argon2id\$'); 33 | }); 34 | 35 | test('pwhash', () { 36 | final outlen = 16; 37 | final passwd = utf8.encoder.convert('hello world'); 38 | final salt = Sodium.hex2bin('10fb7e754a23de756aacb30f810f23df'); 39 | final opslimit = 1; 40 | final memlimit = 8192; 41 | final alg = Sodium.cryptoPwhashAlgDefault; 42 | 43 | final hash = 44 | Sodium.cryptoPwhash(outlen, passwd, salt, opslimit, memlimit, alg); 45 | 46 | expect(hash, Sodium.hex2bin('5ac37aa9233b3bda0677c496ceea4bc0')); 47 | }); 48 | }); 49 | } 50 | --------------------------------------------------------------------------------