Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions cryptography_flutter/lib/src/_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> bytes) {
Expand All @@ -40,18 +35,6 @@ Uint8List asUint8List(List<int> bytes) {
: Uint8List.fromList(bytes);
}

final Future<bool> _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
Expand All @@ -61,7 +44,7 @@ Future<Map> invokeMethod(String name, Map<String, Object?> arguments,
if (kIsWeb) {
throw UnsupportedError('Running in a browser.');
}
final isPluginAvailable = await _isPluginAvailable;
final isPluginAvailable = FlutterCryptography.isPluginPresent;
if (!isPluginAvailable) {
throw UnsupportedError('Unsupported platform.');
}
Expand All @@ -78,10 +61,6 @@ Future<Map> invokeMethod(String name, Map<String, Object?> 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(
Expand Down
23 changes: 0 additions & 23 deletions cryptography_flutter/lib/src/_internal_impl_browser.dart

This file was deleted.

28 changes: 0 additions & 28 deletions cryptography_flutter/lib/src/_internal_impl_non_browser.dart

This file was deleted.

3 changes: 2 additions & 1 deletion cryptography_flutter/lib/src/flutter/flutter_ecdh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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:
Expand Down
34 changes: 17 additions & 17 deletions cryptography_flutter/lib/src/flutter_cryptography.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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 registerWith() has been called.
static bool get isPluginPresent => !kIsWeb && _hasInitializedPlugin;

Chacha20? _chacha20Poly1305Aead;
Ed25519? _ed25519;
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -247,6 +246,7 @@ class FlutterCryptography extends BrowserCryptography {

/// Called by Flutter when the plugin is registered.
static void registerWith() {
_hasInitializedPlugin = true;
Cryptography.instance = defaultInstance;
}
}
4 changes: 0 additions & 4 deletions cryptography_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ flutter:
ios:
pluginClass: CryptographyFlutterPlugin
dartPluginClass: FlutterCryptography
linux:
dartPluginClass: FlutterCryptography
macos:
pluginClass: CryptographyFlutterPlugin
dartPluginClass: FlutterCryptography
windows:
dartPluginClass: FlutterCryptography
Loading