sign method

  1. @override
Future<Uint8List> sign(
  1. Uint8List data
)
override

Signs the given data

Implementation

@override
Future<Uint8List> sign(Uint8List data) async {
  try {
    // Create the signer
    final signer = ECDSASigner(SHA256Digest());
    signer.init(true, PrivateKeyParameter<ECPrivateKey>(_key));

    // Hash the data
    final digest = SHA256Digest();
    final hash = Uint8List(digest.digestSize);
    digest.update(data, 0, data.length);
    digest.doFinal(hash, 0);

    // Sign the hash
    final signature = signer.generateSignature(hash) as ECSignature;

    // Encode the signature in ASN.1 DER format
    final asn1Sequence = pc.ASN1Sequence();
    asn1Sequence.add(pc.ASN1Integer(signature.r));
    asn1Sequence.add(pc.ASN1Integer(signature.s));

    return Uint8List.fromList(asn1Sequence.encode());
  } catch (e) {
    throw ECDSAKeyException('Failed to sign data: ${e.toString()}');
  }
}