Skip to content

Commit

Permalink
fix update errors, add initNative to SodiumInit.init
Browse files Browse the repository at this point in the history
  • Loading branch information
Skycoder42 committed Aug 17, 2021
1 parent c33fb35 commit dd0f824
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 17 deletions.
9 changes: 8 additions & 1 deletion packages/sodium_libs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.0] - 2021-08-17
### Added
- `SodiumInit.init` can now be called with `initNative: false` to disable
initialization of the native library, in case it has already been initialized
### Changed
- Updated minimum required `sodium` version to `1.1.0`

## [1.0.1] - 2021-07-13
### Fixed
- Make links in README secure (pub.dev score)
Expand All @@ -23,4 +30,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Deprecated
### Removed
### Fixed
### Security
### Security
6 changes: 4 additions & 2 deletions packages/sodium_libs/lib/src/platforms/sodium_android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import '../sodium_platform.dart';
@internal
class SodiumAndroid extends SodiumPlatform {
@override
Future<Sodium> loadSodium() =>
SodiumInit.init(DynamicLibrary.open('libsodium.so'));
Future<Sodium> loadSodium({bool initNative = true}) => SodiumInit.init(
DynamicLibrary.open('libsodium.so'),
initNative: initNative,
);
}
10 changes: 8 additions & 2 deletions packages/sodium_libs/lib/src/platforms/sodium_ios.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class _SodiumIos implements Sodium {
@override
SecureKey secureRandom(int length) => _sodium.secureRandom(length);

@override
SecureKey secureHandle(dynamic nativeHandle) =>
_sodium.secureHandle(nativeHandle);

@override
Uint8List unpad(Uint8List buf, int blocksize) =>
_sodium.unpad(buf, blocksize);
Expand All @@ -39,6 +43,8 @@ class _SodiumIos implements Sodium {
@internal
class SodiumIos extends SodiumPlatform {
@override
Future<Sodium> loadSodium() => SodiumInit.init(DynamicLibrary.process())
.then((sodium) => _SodiumIos(sodium));
Future<Sodium> loadSodium({bool initNative = true}) => SodiumInit.init(
DynamicLibrary.process(),
initNative: initNative,
).then((sodium) => _SodiumIos(sodium));
}
5 changes: 4 additions & 1 deletion packages/sodium_libs/lib/src/platforms/sodium_linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import '../sodium_platform.dart';
@internal
class SodiumLinux extends SodiumPlatform {
@override
Future<Sodium> loadSodium() => SodiumInit.init(DynamicLibrary.process());
Future<Sodium> loadSodium({bool initNative = true}) => SodiumInit.init(
DynamicLibrary.process(),
initNative: initNative,
);

@override
String get updateHint =>
Expand Down
10 changes: 8 additions & 2 deletions packages/sodium_libs/lib/src/platforms/sodium_macos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class _SodiumMacos implements Sodium {
@override
SecureKey secureRandom(int length) => _sodium.secureRandom(length);

@override
SecureKey secureHandle(dynamic nativeHandle) =>
_sodium.secureHandle(nativeHandle);

@override
Uint8List unpad(Uint8List buf, int blocksize) =>
_sodium.unpad(buf, blocksize);
Expand All @@ -39,6 +43,8 @@ class _SodiumMacos implements Sodium {
@internal
class SodiumMacos extends SodiumPlatform {
@override
Future<Sodium> loadSodium() => SodiumInit.init(DynamicLibrary.process())
.then((sodium) => _SodiumMacos(sodium));
Future<Sodium> loadSodium({bool initNative = true}) => SodiumInit.init(
DynamicLibrary.process(),
initNative: initNative,
).then((sodium) => _SodiumMacos(sodium));
}
7 changes: 5 additions & 2 deletions packages/sodium_libs/lib/src/platforms/sodium_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SodiumWeb extends SodiumPlatform {
}

@override
Future<Sodium> loadSodium() async {
Future<Sodium> loadSodium({bool initNative = true}) async {
final completer = Completer<dynamic>();

setProperty(
Expand All @@ -45,7 +45,10 @@ class SodiumWeb extends SodiumPlatform {
..src = 'sodium.js';
document.head!.append(script);

return SodiumInit.init(await completer.future);
return SodiumInit.init(
await completer.future,
initNative: initNative,
);
}

@override
Expand Down
6 changes: 4 additions & 2 deletions packages/sodium_libs/lib/src/platforms/sodium_windows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import '../sodium_platform.dart';
@internal
class SodiumWindows extends SodiumPlatform {
@override
Future<Sodium> loadSodium() =>
SodiumInit.init(DynamicLibrary.open('libsodium.dll'));
Future<Sodium> loadSodium({bool initNative = true}) => SodiumInit.init(
DynamicLibrary.open('libsodium.dll'),
initNative: initNative,
);

@override
String get updateHint => 'Please run `flutter clean` and rebuild the project '
Expand Down
9 changes: 7 additions & 2 deletions packages/sodium_libs/lib/src/sodium_init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ abstract class SodiumInit {
///
/// **Note:** Calling this method multiple times will always return the same
/// instance.
static Future<sodium.Sodium> init() => _instanceLock.synchronized(() async {
static Future<sodium.Sodium> init({
bool initNative = true,
}) =>
_instanceLock.synchronized(() async {
if (_instance != null) {
return _instance!;
}
WidgetsFlutterBinding.ensureInitialized();
ensurePlatformRegistered();
_instance = await SodiumPlatform.instance.loadSodium();
_instance = await SodiumPlatform.instance.loadSodium(
initNative: initNative,
);
if (!kReleaseMode) {
if (_instance!.version < _expectedVersion) {
// ignore: avoid_print
Expand Down
2 changes: 1 addition & 1 deletion packages/sodium_libs/lib/src/sodium_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class SodiumPlatform extends PlatformInterface {
///
/// Check out the sodium package documentation for details on how to obtain
/// a native instance on each platform.
Future<Sodium> loadSodium();
Future<Sodium> loadSodium({bool initNative = true});

/// A hint for the user if an outdated version of libsodium is detected.
///
Expand Down
4 changes: 2 additions & 2 deletions packages/sodium_libs/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sodium_libs
version: 1.0.1
version: 1.1.0
description: Flutter companion package to sodium that provides the low-level libsodium binaries for easy use.
homepage: https://github.com/Skycoder42/libsodium_dart_bindings

Expand All @@ -15,7 +15,7 @@ dependencies:
js: ^0.6.3
meta: ^1.3.0
plugin_platform_interface: ^2.0.1
sodium: ^1.0.0
sodium: ^1.1.0
synchronized: ^3.0.0

dev_dependencies:
Expand Down

0 comments on commit dd0f824

Please sign in to comment.