FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Digitally signing a document
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM

Digitally signing a document

Posted: Fri Apr 12, 2024 03:08 PM

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.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Re: Digital signing a document

Posted: Sat Apr 13, 2024 06:19 AM
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 (fw): Select all Collapse
#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
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM

Re: Digital signing a document

Posted: Sat Apr 13, 2024 02:00 PM

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. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 1387
Joined: Fri May 23, 2008 01:33 PM

Re: Digital signing a document

Posted: Sat Apr 13, 2024 03:04 PM
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.06
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Re: Digital signing a document

Posted: Sun Apr 14, 2024 09:14 AM
Much simpler :-)

Use this function:
Code (fw): Select all Collapse
#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
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM

Re: Digital signing a document

Posted: Sun Apr 14, 2024 11:24 AM

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. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM

Re: Digital signing a document

Posted: Sun Apr 14, 2024 10:15 PM

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. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Re: Digital signing a document

Posted: Mon Apr 15, 2024 05:23 AM

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
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM

Re: Digital signing a document

Posted: Mon Apr 15, 2024 07:06 AM

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. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Re: Digitally signing a document

Posted: Mon Apr 15, 2024 07:12 AM

Dear Michel,

I got it, thank you

Do you also need to know information about the signature ?

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM

Re: Digitally signing a document

Posted: Mon Apr 15, 2024 07:20 AM

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. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM

Re: Digitally signing a document

Posted: Mon Apr 15, 2024 08:12 AM

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. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Re: Digitally signing a document

Posted: Mon Apr 15, 2024 08:30 AM
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 (fw): Select all Collapse
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 (fw): Select all Collapse
pyinstaller --onefile signed.py
Here you have it:
https://wormhole.app/QoPx3#Ze8o1phu14n66ZQUwCH6TA

You can call it from FWH using:
Code (fw): Select all Collapse
 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
Posts: 182
Joined: Tue Oct 18, 2005 10:01 AM

Re: Digitally signing a document

Posted: Fri Apr 19, 2024 12:13 PM
Hi, maybe you can test with chilkat:

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

Regards,

Toninho.
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Re: Digitally signing a document

Posted: Fri Apr 19, 2024 12:28 PM

Great web for examples!

thank you

regards, saludos

Antonio Linares
www.fivetechsoft.com

Continue the discussion