sign method

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

Calculates signature for the message.

Implementation

@override
Future<Signature> sign(List<int> data, {required KeyPair keyPair}) async {
  if (usePlugin) {
    try {
      final rsaKeyPair = await keyPair.extract();
      if (rsaKeyPair is! RsaKeyPairData) {
        throw ArgumentError.value(
          keyPair,
          'keyPair',
        );
      }
      final result = await channel.invokeMethod(
        'rsa_ssa_pkcs1v15_sign',
        {
          'data': Uint8List.fromList(data),
          'hash': hashAlgorithmName,
          'd': Uint8List.fromList(rsaKeyPair.d),
          'e': Uint8List.fromList(rsaKeyPair.e),
          'n': Uint8List.fromList(rsaKeyPair.n),
          'p': Uint8List.fromList(rsaKeyPair.p),
          'q': Uint8List.fromList(rsaKeyPair.q),
        },
      );
      if (result is! Map) {
        throw StateError('Invalid output from plugin: $result');
      }
      final bytes = base64Decode(result['bytes'] as String);
      return Signature(bytes, publicKey: await rsaKeyPair.extractPublicKey());
    } catch (error) {
      usePlugin = false;
    }
  }
  return super.sign(data, keyPair: keyPair);
}