Sign document with pointycastle

Hi, I want to sign document using ecdsa generated by pointycastle. I found a thread and doing similar but i got an error:  RegistryFactoryException (RegistryFactoryException: No algorithm registered of type SecureRandom with name: ) . Any help is appreciated. Here is the thread:  PDF digital signature usig PointCasttle | Flutter Forums | Syncfusion. And here is my code:

class PdfExternalSigner implements spdf.IPdfExternalSigner {
  final pointycastle.ECPrivateKey privKey;

  PdfExternalSigner({required this.privKey});
  //Hash algorithm.
  @override
  spdf.DigestAlgorithm get hashAlgorithm => spdf.DigestAlgorithm.sha256;

  //Sign message digest.
  @override
  spdf.SignerResult sign(List<int> message) {
    var signer = pointycastle.ECDSASigner(
      pointycastle.SHA256Digest(),
    );
    signer.init(true,
        pointycastle.PrivateKeyParameter<pointycastle.ECPrivateKey>(privKey));
    return spdf.SignerResult(Uint8List.fromList(
        signer.generateSignature(Uint8List.fromList(message)) as List<int>));
  }
}
Future<void> modifyPDf() async {
    final spdf.PdfDocument document =
        spdf.PdfDocument(inputBytes: File(filePath!).readAsBytesSync());
    final Uint8List imageData = fSignPath!.readAsBytesSync();
    //Sign pdf with key
    String publicCert =
        'MIIEKzCCAxOgAwIBAgIUI3a1/5ob69Lp0RlhpFzyPtVxUOcwDQYJKoZIhvcNAQELBQAwgaQxCzAJBgNVBAYTAlZOMRYwFAYDVQQIDA1Ib0NoaU1pbmhDaXR5MRYwFAYDVQQHDA1Ib0NoaU1pbmhDaXR5MREwDwYDVQQKDAhRUlZlcmlmeTERMA8GA1UECwwIUVJWZXJpZnkxGzAZBgNVBAMMElFSVmVyaWZ5Rmx1dHRlckFwcDEiMCAGCSqGSIb3DQEJARYTdGhhbnZpZWxlQGdtYWlsLmNvbTAeFw0yMzA1MjcxMzEwNThaFw0yODA1MjUxMzEwNThaMIGkMQswCQYDVQQGEwJWTjEWMBQGA1UECAwNSG9DaGlNaW5oQ2l0eTEWMBQGA1UEBwwNSG9DaGlNaW5oQ2l0eTERMA8GA1UECgwIUVJWZXJpZnkxETAPBgNVBAsMCFFSVmVyaWZ5MRswGQYDVQQDDBJRUlZlcmlmeUZsdXR0ZXJBcHAxIjAgBgkqhkiG9w0BCQEWE3RoYW52aWVsZUBnbWFpbC5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDdhq3udWR6acQZjs5I1wv4yEOL2kglyahqO5ceXcha2NITcAqSW+x4+4VQc+wWgijyfEtI5UBk76E/e5idJyqf8kHNZTjiKZ7HToYzaSeO44lp+0/Nto6AbxOeyoVtvU6ITtx7NU1iFQRgcicBkNV/LIGSLLuD2r5d4i+TIi77Z3qU8vazBbNcHxKAadw9BEbQRCKhAKuaRYxpUY/bKYXZUqofI54aSs8mPH14duSoSFQQ9rGmb8RUOWjm6g+KiUGiY1vsqM3wun4xcwpRf8uo53P1saJgrGeF+ofYfTKKZC+Q370zA501RxqPafaMRy4rk0yFyocGynS6rPysLq5zAgMBAAGjUzBRMB0GA1UdDgQWBBRc+YVewlxNL4ZpJ0AmoxPAY52hQTAfBgNVHSMEGDAWgBRc+YVewlxNL4ZpJ0AmoxPAY52hQTAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQDEk3LeUfC/5J6CaWeNonOjcrSp5reZpaiGuOJmjHYcfcLDm/7qyNXb1SXRDQIWLnJLx85wT4V3CkAg2XVmMi38Oo6FAyeCyWnmPMWqsyxsXFlzEJCMC/ln0fRwmkXvFlBqBYO5QaUbuLy1GeFEUB5lI1ig4iISrZESKHGgxCf0/u+Xya5CJBsyWKsphgJw3rYn8T085Th8vBZplFqFWtmfAEpEpHdbOW1uP25xz9ftdFLXdkZOP/m89QS5UTCYqE+Mx4av/ia+CXlOXHywYAEcDIBFwyraNgSdak/ps644L3sn0bN4UhPYW3HjFiHOcPGBKKxiIkR2/2T0ONvBlIJv';
    late final currentUser = AuthService.firebase().currentUser!;
    // Load the private key from the PEM file
    File filekey =
        File('${await getFilePath()}/${currentUser.email}.privatekey.pem');
    final pem = filekey.readAsStringSync();
    final prikey = ECPrivateKey(pem);
    final spdf.PdfSignature sign = spdf.PdfSignature(
        cryptographicStandard: spdf.CryptographicStandard.cades,
        digestAlgorithm: spdf.DigestAlgorithm.sha256);
    final spdf.IPdfExternalSigner externalSignature =
        PdfExternalSigner(privKey: prikey.key);
    sign.addExternalSigner(
        externalSignature, <List<int>>[base64.decode(publicCert)]);
    sign.contactInfo = currentUser.email;
    spdf.PdfPage page = document.pages.add();
    spdf.PdfSignatureField field = spdf.PdfSignatureField(page, 'signature');

    field.signature = sign;

    spdf.PdfGraphics? graphics = field.appearance.normal.graphics;
    //Load the image using PdfBitmap.
    final spdf.PdfBitmap image = spdf.PdfBitmap(imageData);
    //Draw the image to the PDF page.
    graphics?.drawImage(image, const Rect.fromLTWH(0, 0, 380, 300));
    // Save the document.
    document.form.fields.add(field);
    final expath = await createFolderInAppDocDir('pdf-signed-storage');
    File file = File('$expath/${filetype!.name}-signed.pdf');
    await File(file.path).writeAsBytes(await document.save());
    document.dispose();

    OpenFile.open(file.path);
  }

2 Replies

SN Santhiya Narayanan Syncfusion Team May 29, 2023 04:07 PM UTC

We were able to reproduce the reported behavior from our side. Currently we analyzing on this. We will provide further analysis details on May 31, 2023.



AG Anantha Gokula Raman Jeyaraman Syncfusion Team May 31, 2023 04:42 PM UTC

We were unable to externally sign the ECDSA signature in our library using pointycastle package. Currently we are analyzing the possibilities for achieving your requirement and eventually we will provide you the details.


Loader.
Up arrow icon