verify method

  1. @override
Future<bool> verify(
  1. List<int> message, {
  2. required Signature signature,
})

Verifies whether bytes was signed with signature.

Example

In this example, we use Ed25519:

import 'package:cryptography/cryptography.dart';

Future<void> main() async {
  final signedMessage = [1,2,3];

  final ed25519 = Ed25519();
  final keyPair = await ed25519.newKeyPair();
  final signature = await ed25519.sign(
    signedMessage,
    keyPair: keyPair,
  );

  // ...

  final isRealSignature = await ed25519.verify(
    signedMessage,
    signature: signature,
  );

  print('Signature verification result: $isRealSignature');
}

Implementation

@override
Future<bool> verify(List<int> message, {required Signature signature}) async {
  if (isSupportedPlatform) {
    final publicKey = signature.publicKey;
    if (publicKey is! EcPublicKey) {
      throw ArgumentError.value(
        signature,
        'signature',
      );
    }
    Map result;
    if (isCupertino) {
      try {
        result = await invokeMethod(
          'Ecdsa.verify',
          {
            if (isAndroid) 'androidProvider': androidCryptoProvider,
            'curve': _curveName,
            'data': asUint8List(message),
            'signature': asUint8List(signature.bytes),
            'der': publicKey.toDer(),
          },
        );
      } on PlatformException {
        rethrow;
      }
    } else {
      result = await invokeMethod(
        'Ecdsa.verify',
        {
          if (isAndroid) 'androidProvider': androidCryptoProvider,
          'curve': _curveName,
          'data': asUint8List(message),
          'signature': asUint8List(signature.bytes),
          'x': asUint8List(publicKey.x),
          'y': asUint8List(publicKey.y),
        },
      );
    }
    final error = result['error'];
    if (error != null) {
      throw StateError(
        '"package:cryptography_flutter": $runtimeType.verify failed: $error',
      );
    }
    return result['result'] as bool;
  }
  final fallback = this.fallback;
  if (fallback == null) {
    throw UnsupportedError('Unsupported and no fallback implementation');
  }
  return await fallback.verify(
    message,
    signature: signature,
  );
}