Skip to content

Commit a932ce5

Browse files
authored
Merge pull request #206 from dint-dev/fix/platforms
[cryptography_flutter] Fix platform conditions
2 parents b2faf86 + 63a8d40 commit a932ce5

File tree

6 files changed

+23
-98
lines changed

6 files changed

+23
-98
lines changed

cryptography_flutter/lib/src/_internal.dart

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,13 @@ import 'package:flutter/foundation.dart';
1818
import 'package:flutter/services.dart';
1919

2020
import '../cryptography_flutter.dart';
21-
import '_internal_impl_non_browser.dart'
22-
if (dart.library.html) '_internal_impl_browser.dart';
23-
24-
export '_internal_impl_non_browser.dart'
25-
if (dart.library.html) '_internal_impl_browser.dart';
2621

2722
const MethodChannel _methodChannel = MethodChannel('cryptography_flutter');
2823

29-
bool hasSeenMissingPluginException = false;
24+
bool get isAndroid => defaultTargetPlatform == TargetPlatform.android;
3025

31-
/// True if the platform is iOS and Mac OS X.
32-
bool get isCupertino => isIOS || isMacOS;
26+
bool get isCupertino => (defaultTargetPlatform == TargetPlatform.iOS ||
27+
defaultTargetPlatform == TargetPlatform.macOS);
3328

3429
/// Returns the bytes as [Uint8List].
3530
Uint8List asUint8List(List<int> bytes) {
@@ -40,18 +35,6 @@ Uint8List asUint8List(List<int> bytes) {
4035
: Uint8List.fromList(bytes);
4136
}
4237

43-
final Future<bool> _isPluginAvailable = () async {
44-
try {
45-
await _methodChannel.invokeMethod('encrypt', {});
46-
return true;
47-
} on MissingPluginException {
48-
hasSeenMissingPluginException = true;
49-
return false;
50-
} catch (e) {
51-
return true;
52-
}
53-
}();
54-
5538
/// Invokes plugin method.
5639
///
5740
/// Throws [CryptographyUnsupportedError] if the platform is web or plugin is
@@ -61,7 +44,7 @@ Future<Map> invokeMethod(String name, Map<String, Object?> arguments,
6144
if (kIsWeb) {
6245
throw UnsupportedError('Running in a browser.');
6346
}
64-
final isPluginAvailable = await _isPluginAvailable;
47+
final isPluginAvailable = FlutterCryptography.isPluginPresent;
6548
if (!isPluginAvailable) {
6649
throw UnsupportedError('Unsupported platform.');
6750
}
@@ -78,10 +61,6 @@ Future<Map> invokeMethod(String name, Map<String, Object?> arguments,
7861
}
7962
try {
8063
return await _methodChannel.invokeMethod(name, arguments) as Map;
81-
} on MissingPluginException catch (error, stackTrace) {
82-
// Update the top-level variable
83-
hasSeenMissingPluginException = true;
84-
throw UnsupportedError('Caught: $error\n$stackTrace');
8564
} on PlatformException catch (error) {
8665
if (error.code == 'UNSUPPORTED_ALGORITHM') {
8766
throw UnsupportedError(

cryptography_flutter/lib/src/_internal_impl_browser.dart

Lines changed: 0 additions & 23 deletions
This file was deleted.

cryptography_flutter/lib/src/_internal_impl_non_browser.dart

Lines changed: 0 additions & 28 deletions
This file was deleted.

cryptography_flutter/lib/src/flutter/flutter_ecdh.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'package:cryptography/cryptography.dart';
1616
import 'package:flutter/foundation.dart';
1717
import 'package:flutter/services.dart';
1818

19+
import '../../cryptography_flutter.dart';
1920
import '../_flutter_cryptography_implementation.dart';
2021
import '../_internal.dart';
2122

@@ -57,8 +58,8 @@ class FlutterEcdh extends Ecdh implements PlatformCryptographicAlgorithm {
5758
super.constructor();
5859

5960
@override
60-
bool get isSupportedPlatform => isAndroid || isCupertino;
6161
bool get isSupportedPlatform => FlutterCryptography.isPluginPresent && (isAndroid || isCupertino);
62+
6263
String get _curveName {
6364
switch (keyPairType) {
6465
case KeyPairType.p256:

cryptography_flutter/lib/src/flutter_cryptography.dart

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import 'package:cryptography_flutter/src/flutter/flutter_hmac.dart';
2020
import 'package:flutter/foundation.dart';
2121

2222
import '../cryptography_flutter.dart';
23-
import '_internal.dart';
2423

2524
/// An implementation [Cryptography] that uses native operating system APIs.
2625
///
@@ -41,14 +40,14 @@ import '_internal.dart';
4140
class FlutterCryptography extends BrowserCryptography {
4241
/// Either [FlutterCryptography] or [BrowserCryptography] depending on
4342
/// [FlutterCryptography.isPluginPresent].
44-
static final Cryptography defaultInstance =
45-
kIsWeb ? BrowserCryptography.defaultInstance : FlutterCryptography();
43+
static final Cryptography defaultInstance = isPluginPresent
44+
? FlutterCryptography()
45+
: BrowserCryptography.defaultInstance;
4646

47-
/// Tells whether the current platform has a plugin.
48-
///
49-
/// Only Android, iOS, and Mac OS X are supported at the moment.
50-
static bool get isPluginPresent =>
51-
!kIsWeb && !hasSeenMissingPluginException && (isAndroid || isCupertino);
47+
static bool _hasInitializedPlugin = false;
48+
49+
/// Tells whether registerWith() has been called.
50+
static bool get isPluginPresent => !kIsWeb && _hasInitializedPlugin;
5251

5352
Chacha20? _chacha20Poly1305Aead;
5453
Ed25519? _ed25519;
@@ -125,15 +124,6 @@ class FlutterCryptography extends BrowserCryptography {
125124
return super.ecdsaP256(hashAlgorithm);
126125
}
127126

128-
@override
129-
Hmac hmac(HashAlgorithm hashAlgorithm) {
130-
final impl = FlutterHmac(hashAlgorithm);
131-
if (impl.isSupportedPlatform) {
132-
return impl;
133-
}
134-
return super.hmac(hashAlgorithm);
135-
}
136-
137127
@override
138128
Ecdsa ecdsaP384(HashAlgorithm hashAlgorithm) {
139129
final impl = FlutterEcdsa.p384(hashAlgorithm);
@@ -160,6 +150,15 @@ class FlutterCryptography extends BrowserCryptography {
160150
return _ed25519 ??= _chooseEd25519();
161151
}
162152

153+
@override
154+
Hmac hmac(HashAlgorithm hashAlgorithm) {
155+
final impl = FlutterHmac(hashAlgorithm);
156+
if (impl.isSupportedPlatform) {
157+
return impl;
158+
}
159+
return super.hmac(hashAlgorithm);
160+
}
161+
163162
@override
164163
Pbkdf2 pbkdf2({
165164
required MacAlgorithm macAlgorithm,
@@ -247,6 +246,7 @@ class FlutterCryptography extends BrowserCryptography {
247246

248247
/// Called by Flutter when the plugin is registered.
249248
static void registerWith() {
249+
_hasInitializedPlugin = true;
250250
Cryptography.instance = defaultInstance;
251251
}
252252
}

cryptography_flutter/pubspec.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ flutter:
4242
ios:
4343
pluginClass: CryptographyFlutterPlugin
4444
dartPluginClass: FlutterCryptography
45-
linux:
46-
dartPluginClass: FlutterCryptography
4745
macos:
4846
pluginClass: CryptographyFlutterPlugin
4947
dartPluginClass: FlutterCryptography
50-
windows:
51-
dartPluginClass: FlutterCryptography

0 commit comments

Comments
 (0)