From 858f5d71877ff73cd11b2bdf445c13bf96d21c09 Mon Sep 17 00:00:00 2001 From: terrier989 Date: Fri, 21 Nov 2025 07:18:48 +0000 Subject: [PATCH 1/2] [cryptography_flutter] Fix platform conditions --- cryptography_flutter/lib/src/_internal.dart | 29 +++------------- .../lib/src/_internal_impl_browser.dart | 23 ------------- .../lib/src/_internal_impl_non_browser.dart | 28 --------------- .../lib/src/flutter/flutter_ecdh.dart | 3 +- .../lib/src/flutter_cryptography.dart | 34 +++++++++---------- cryptography_flutter/pubspec.yaml | 4 --- 6 files changed, 23 insertions(+), 98 deletions(-) delete mode 100644 cryptography_flutter/lib/src/_internal_impl_browser.dart delete mode 100644 cryptography_flutter/lib/src/_internal_impl_non_browser.dart diff --git a/cryptography_flutter/lib/src/_internal.dart b/cryptography_flutter/lib/src/_internal.dart index 47c279ab..6cc24d68 100644 --- a/cryptography_flutter/lib/src/_internal.dart +++ b/cryptography_flutter/lib/src/_internal.dart @@ -18,18 +18,13 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import '../cryptography_flutter.dart'; -import '_internal_impl_non_browser.dart' - if (dart.library.html) '_internal_impl_browser.dart'; - -export '_internal_impl_non_browser.dart' - if (dart.library.html) '_internal_impl_browser.dart'; const MethodChannel _methodChannel = MethodChannel('cryptography_flutter'); -bool hasSeenMissingPluginException = false; +bool get isAndroid => defaultTargetPlatform == TargetPlatform.android; -/// True if the platform is iOS and Mac OS X. -bool get isCupertino => isIOS || isMacOS; +bool get isCupertino => (defaultTargetPlatform == TargetPlatform.iOS || + defaultTargetPlatform == TargetPlatform.macOS); /// Returns the bytes as [Uint8List]. Uint8List asUint8List(List bytes) { @@ -40,18 +35,6 @@ Uint8List asUint8List(List bytes) { : Uint8List.fromList(bytes); } -final Future _isPluginAvailable = () async { - try { - await _methodChannel.invokeMethod('encrypt', {}); - return true; - } on MissingPluginException { - hasSeenMissingPluginException = true; - return false; - } catch (e) { - return true; - } -}(); - /// Invokes plugin method. /// /// Throws [CryptographyUnsupportedError] if the platform is web or plugin is @@ -61,7 +44,7 @@ Future invokeMethod(String name, Map arguments, if (kIsWeb) { throw UnsupportedError('Running in a browser.'); } - final isPluginAvailable = await _isPluginAvailable; + final isPluginAvailable = FlutterCryptography.isPluginPresent; if (!isPluginAvailable) { throw UnsupportedError('Unsupported platform.'); } @@ -78,10 +61,6 @@ Future invokeMethod(String name, Map arguments, } try { return await _methodChannel.invokeMethod(name, arguments) as Map; - } on MissingPluginException catch (error, stackTrace) { - // Update the top-level variable - hasSeenMissingPluginException = true; - throw UnsupportedError('Caught: $error\n$stackTrace'); } on PlatformException catch (error) { if (error.code == 'UNSUPPORTED_ALGORITHM') { throw UnsupportedError( diff --git a/cryptography_flutter/lib/src/_internal_impl_browser.dart b/cryptography_flutter/lib/src/_internal_impl_browser.dart deleted file mode 100644 index ba5c1e93..00000000 --- a/cryptography_flutter/lib/src/_internal_impl_browser.dart +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2019-2020 Gohilla. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -bool get isAndroid => false; - -bool get isIOS => false; - -bool get isMacOS => false; - -String get operatingSystemNameAndVersion => 'browser'; - -String get operatingSystemName => 'browser'; diff --git a/cryptography_flutter/lib/src/_internal_impl_non_browser.dart b/cryptography_flutter/lib/src/_internal_impl_non_browser.dart deleted file mode 100644 index 6ebde606..00000000 --- a/cryptography_flutter/lib/src/_internal_impl_non_browser.dart +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2019-2020 Gohilla. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -import 'dart:io'; - -final bool isAndroid = Platform.isAndroid; - -final bool isIOS = Platform.isIOS; - -final bool isMacOS = Platform.isMacOS; - -/// For example, "iOS 13.3.1". -String get operatingSystemNameAndVersion => - '${Platform.operatingSystem} ${Platform.operatingSystemVersion}'; - -/// For example, "iOS". -String get operatingSystemName => Platform.operatingSystem; diff --git a/cryptography_flutter/lib/src/flutter/flutter_ecdh.dart b/cryptography_flutter/lib/src/flutter/flutter_ecdh.dart index 6c6f89fc..500713af 100644 --- a/cryptography_flutter/lib/src/flutter/flutter_ecdh.dart +++ b/cryptography_flutter/lib/src/flutter/flutter_ecdh.dart @@ -16,6 +16,7 @@ import 'package:cryptography/cryptography.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; +import '../../cryptography_flutter.dart'; import '../_flutter_cryptography_implementation.dart'; import '../_internal.dart'; @@ -57,8 +58,8 @@ class FlutterEcdh extends Ecdh implements PlatformCryptographicAlgorithm { super.constructor(); @override - bool get isSupportedPlatform => isAndroid || isCupertino; bool get isSupportedPlatform => FlutterCryptography.isPluginPresent && (isAndroid || isCupertino); + String get _curveName { switch (keyPairType) { case KeyPairType.p256: diff --git a/cryptography_flutter/lib/src/flutter_cryptography.dart b/cryptography_flutter/lib/src/flutter_cryptography.dart index 3d66748c..55442ef5 100644 --- a/cryptography_flutter/lib/src/flutter_cryptography.dart +++ b/cryptography_flutter/lib/src/flutter_cryptography.dart @@ -20,7 +20,6 @@ import 'package:cryptography_flutter/src/flutter/flutter_hmac.dart'; import 'package:flutter/foundation.dart'; import '../cryptography_flutter.dart'; -import '_internal.dart'; /// An implementation [Cryptography] that uses native operating system APIs. /// @@ -41,14 +40,14 @@ import '_internal.dart'; class FlutterCryptography extends BrowserCryptography { /// Either [FlutterCryptography] or [BrowserCryptography] depending on /// [FlutterCryptography.isPluginPresent]. - static final Cryptography defaultInstance = - kIsWeb ? BrowserCryptography.defaultInstance : FlutterCryptography(); + static final Cryptography defaultInstance = isPluginPresent + ? FlutterCryptography() + : BrowserCryptography.defaultInstance; - /// Tells whether the current platform has a plugin. - /// - /// Only Android, iOS, and Mac OS X are supported at the moment. - static bool get isPluginPresent => - !kIsWeb && !hasSeenMissingPluginException && (isAndroid || isCupertino); + static bool _hasInitializedPlugin = false; + + /// Tells whether the plugin has been loaded. + static bool get isPluginPresent => !kIsWeb && _hasInitializedPlugin; Chacha20? _chacha20Poly1305Aead; Ed25519? _ed25519; @@ -125,15 +124,6 @@ class FlutterCryptography extends BrowserCryptography { return super.ecdsaP256(hashAlgorithm); } - @override - Hmac hmac(HashAlgorithm hashAlgorithm) { - final impl = FlutterHmac(hashAlgorithm); - if (impl.isSupportedPlatform) { - return impl; - } - return super.hmac(hashAlgorithm); - } - @override Ecdsa ecdsaP384(HashAlgorithm hashAlgorithm) { final impl = FlutterEcdsa.p384(hashAlgorithm); @@ -160,6 +150,15 @@ class FlutterCryptography extends BrowserCryptography { return _ed25519 ??= _chooseEd25519(); } + @override + Hmac hmac(HashAlgorithm hashAlgorithm) { + final impl = FlutterHmac(hashAlgorithm); + if (impl.isSupportedPlatform) { + return impl; + } + return super.hmac(hashAlgorithm); + } + @override Pbkdf2 pbkdf2({ required MacAlgorithm macAlgorithm, @@ -247,6 +246,7 @@ class FlutterCryptography extends BrowserCryptography { /// Called by Flutter when the plugin is registered. static void registerWith() { + _hasInitializedPlugin = true; Cryptography.instance = defaultInstance; } } diff --git a/cryptography_flutter/pubspec.yaml b/cryptography_flutter/pubspec.yaml index 7f3fcbc8..b4c46a05 100644 --- a/cryptography_flutter/pubspec.yaml +++ b/cryptography_flutter/pubspec.yaml @@ -42,10 +42,6 @@ flutter: ios: pluginClass: CryptographyFlutterPlugin dartPluginClass: FlutterCryptography - linux: - dartPluginClass: FlutterCryptography macos: pluginClass: CryptographyFlutterPlugin dartPluginClass: FlutterCryptography - windows: - dartPluginClass: FlutterCryptography From 63a8d40f87575eb8d61b7129909c8a6eb18c3ad4 Mon Sep 17 00:00:00 2001 From: "Teemu S." Date: Fri, 21 Nov 2025 07:53:07 +0000 Subject: [PATCH 2/2] Update cryptography_flutter/lib/src/flutter_cryptography.dart Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- cryptography_flutter/lib/src/flutter_cryptography.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cryptography_flutter/lib/src/flutter_cryptography.dart b/cryptography_flutter/lib/src/flutter_cryptography.dart index 55442ef5..643c6f88 100644 --- a/cryptography_flutter/lib/src/flutter_cryptography.dart +++ b/cryptography_flutter/lib/src/flutter_cryptography.dart @@ -46,7 +46,7 @@ class FlutterCryptography extends BrowserCryptography { static bool _hasInitializedPlugin = false; - /// Tells whether the plugin has been loaded. + /// Tells whether registerWith() has been called. static bool get isPluginPresent => !kIsWeb && _hasInitializedPlugin; Chacha20? _chacha20Poly1305Aead;