sign method

  1. @override
Future<Signature> sign(
  1. List<int> message, {
  2. required KeyPair keyPair,
})

Signs bytes.

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<Signature> sign(
  List<int> message, {
  required KeyPair keyPair,
}) async {
  if (!kIsWeb) {
    if (keyPair is SimpleKeyPair) {
      if (isSupportedPlatform) {
        final privateKeyBytes = await keyPair.extractPrivateKeyBytes();
        final publicKey = await keyPair.extractPublicKey();
        final publicKeyBytes = Uint8List.fromList(
          publicKey.bytes,
        );
        final result = await invokeMethod(
          'Ed25519.sign',
          {
            'data': asUint8List(message),
            'privateKey': asUint8List(privateKeyBytes),
            'publicKey': asUint8List(publicKeyBytes),
          },
        );
        final error = result['error'];
        if (error is String) {
          throw StateError('error in "package:cryptography_flutter": $error');
        }
        final bytes = result['signature'] as Uint8List;
        return Signature(
          bytes,
          publicKey: publicKey,
        );
      }
    }
  }
  return await fallback.sign(message, keyPair: keyPair);
}