sign method
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 (isSupportedPlatform) {
final keyPairData = await keyPair.extract();
if (keyPairData is! EcKeyPairData) {
throw ArgumentError.value(
keyPairData,
'keyPair',
);
}
Map result;
if (isCupertino) {
result = await invokeMethod(
'Ecdsa.sign',
{
if (isAndroid) 'androidProvider': androidCryptoProvider,
'curve': _curveName,
'data': Uint8List.fromList(message),
'der': keyPairData.toDer(),
},
);
} else {
result = await invokeMethod(
'Ecdsa.sign',
{
if (isAndroid) 'androidProvider': androidCryptoProvider,
'curve': _curveName,
'data': Uint8List.fromList(message),
'd': asUint8List(keyPairData.d),
'x': asUint8List(keyPairData.x),
'y': asUint8List(keyPairData.y),
},
);
}
final error = result['error'] as String?;
if (error != null) {
throw StateError(
'"package:cryptography_flutter": $runtimeType.sign failed: $error',
);
}
final signature = result['signature'] as Uint8List;
final publicKey = await keyPairData.extractPublicKey();
return Signature(signature, publicKey: publicKey);
}
final fallback = this.fallback;
if (fallback == null) {
throw UnsupportedError('Unsupported and no fallback implementation');
}
return await fallback.sign(
message,
keyPair: keyPair,
);
}