Digitally signing a document

Post Reply
User avatar
driessen
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Digitally signing a document

Post by driessen »

Hello,

A PDF-document can be signed digitally in Acrotbat Reader.

But does anyone know how I can check in my FWH-application if a PDF-document has been signed digitally?

Thank you in advance.
Last edited by driessen on Mon Apr 15, 2024 7:06 am, edited 1 time in total.
Regards,

Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
Antonio Linares
Site Admin
Posts: 42270
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Digital signing a document

Post by Antonio Linares »

Dear Michel,

Asking chatgpt:

you can utilize a C library that provides PDF parsing capabilities. One such library is Poppler, which is widely used for working with PDF files in C

You can download the pre-built Poppler library for Windows from the Poppler website: https://poppler.freedesktop.org/.

Here is the code to check for the signature:

Code: Select all | Expand

#include <stdio.h>
#include <poppler-document.h>

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <input_pdf>\n", argv[0]);
        return 1;
    }

    const char *input_pdf = argv[1];
    GError *error = NULL;

    // Open the PDF document
    PopplerDocument *document = poppler_document_new_from_file(input_pdf, NULL, &error);
    if (error != NULL) {
        fprintf(stderr, "Error opening PDF: %s\n", error->message);
        g_error_free(error);
        return 1;
    }

    // Check if the document is encrypted
    if (poppler_document_get_n_pages(document) == 0) {
        printf("The PDF is encrypted.\n");
    } else {
        printf("The PDF is not encrypted.\n");
    }

    // Check if the document is signed
    if (poppler_document_get_metadata(document, "signature") != NULL) {
        printf("The PDF is signed.\n");
    } else {
        printf("The PDF is not signed.\n");
    }

    // Free the document object
    g_object_unref(document);

    return 0;
}
 
We can easily adap that code to Harbour or simply build an EXE from it and run it
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
driessen
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Digital signing a document

Post by driessen »

Antonio,

I'm afraid that is somewhat to difficult for me.
I need to know if a document is digitally signed or not.
But I don't know how to implement your suggestion.
Regards,

Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
Horizon
Posts: 1323
Joined: Fri May 23, 2008 1:33 pm

Re: Digital signing a document

Post by Horizon »

driessen wrote:Antonio,

I'm afraid that is somewhat to difficult for me.
I need to know if a document is digitally signed or not.
But I don't know how to implement your suggestion.
Can you please send a link here a sample digitally signed pdf?
Regards,

Hakan ONEMLI

Harbour & MSVC 2022 & FWH 23.04
User avatar
Antonio Linares
Site Admin
Posts: 42270
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Digital signing a document

Post by Antonio Linares »

Much simpler :-)

Use this function:

Code: Select all | Expand

#include "FiveWin.ch"

function Main()

    MsgInfo( IsSigned( "fwintro.pdf" ) )

return nil 

function IsSigned( cPDFFileName )

return At( "/SigFlags", hb_memoRead( cPDFFileName ) ) != 0
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
driessen
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Digital signing a document

Post by driessen »

Antonio,

Thanks a lot for your help.

I'll test it tonight or tomorrow.

Have a fine Sunday.
Regards,

Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
driessen
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Digital signing a document

Post by driessen »

Antonio,

Unfortunately, I always get a .F. as result, whether it is signed or not.
Regards,

Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
Antonio Linares
Site Admin
Posts: 42270
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Digital signing a document

Post by Antonio Linares »

Dear Michel,

Then I guess your PDFs are encrypted

Could you please email me one of them ? thanks
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
driessen
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Digital signing a document

Post by driessen »

Antonio,

I just send you a PDF-file which I digitally signed.

The PDF-file was made by saving as a PDF-file in Word.

Thanks.
Regards,

Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
Antonio Linares
Site Admin
Posts: 42270
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Digitally signing a document

Post by Antonio Linares »

Dear Michel,

I got it, thank you

Do you also need to know information about the signature ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
driessen
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Digitally signing a document

Post by driessen »

No, I just need to know if the document is digitally signed or not.
Regards,

Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
driessen
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Digitally signing a document

Post by driessen »

FYI, Antonio,

If I use "Michel" in stead of "/SigFlags", I get a .T.
Regards,

Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
Antonio Linares
Site Admin
Posts: 42270
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Digitally signing a document

Post by Antonio Linares »

Dear Michel,

The simplest way I have found to build a tool to check if a PDF is signed is using python.

signed.py

Code: Select all | Expand

import sys, aspose.pdf as ap

pdfSign = ap.facades.PdfFileSignature()
pdfSign.bind_pdf(sys.argv[ 1 ])
signatures = pdfSign.get_sign_names( True )

if( signatures.length ) > 0 :
   print( "signed" )
else:
   print( "not signed" )

Using pyinstaller we can create a standalone EXE:

Code: Select all | Expand

pyinstaller --onefile signed.py
Here you have it:
https://wormhole.app/QoPx3#Ze8o1phu14n66ZQUwCH6TA

You can call it from FWH using:

Code: Select all | Expand

 WaitRun( "signed.exe " + cPdfFileName + " > result.txt" )
if hb_memoRead( "result.txt" ) == "signed"
   MsgInfo( "signed" )
else
   MsgInfo( "non signed" )
endif
 
Unfortunately signed.exe size is large but it works fine
regards, saludos

Antonio Linares
www.fivetechsoft.com
toninhofwi
Posts: 172
Joined: Tue Oct 18, 2005 10:01 am

Re: Digitally signing a document

Post by toninhofwi »

Hi, maybe you can test with chilkat:

https://www.example-code.com/foxpro/pdf_signatures.asp

Regards,

Toninho.
User avatar
Antonio Linares
Site Admin
Posts: 42270
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Digitally signing a document

Post by Antonio Linares »

Great web for examples!

thank you
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply