Digitally signing a document

Digitally signing a document

Postby driessen » Fri Apr 12, 2024 3: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.
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
driessen
 
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Digital signing a document

Postby Antonio Linares » Sat Apr 13, 2024 6: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: Select all  Expand view
#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
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Digital signing a document

Postby driessen » Sat Apr 13, 2024 2: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. 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

Postby Horizon » Sat Apr 13, 2024 3: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.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Re: Digital signing a document

Postby Antonio Linares » Sun Apr 14, 2024 9:14 am

Much simpler :-)

Use this function:
Code: Select all  Expand view
#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
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Digital signing a document

Postby driessen » 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. 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

Postby driessen » 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. 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

Postby Antonio Linares » Mon Apr 15, 2024 5: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
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Digital signing a document

Postby driessen » Mon Apr 15, 2024 7: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. 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

Postby Antonio Linares » Mon Apr 15, 2024 7: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
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Digitally signing a document

Postby driessen » Mon Apr 15, 2024 7: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. 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

Postby driessen » Mon Apr 15, 2024 8: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. 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

Postby Antonio Linares » Mon Apr 15, 2024 8: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: Select all  Expand view
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 view
pyinstaller --onefile signed.py

Here you have it:
https://wormhole.app/QoPx3#Ze8o1phu14n66ZQUwCH6TA

You can call it from FWH using:
Code: Select all  Expand view
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
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Digitally signing a document

Postby toninhofwi » 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.
toninhofwi
 
Posts: 172
Joined: Tue Oct 18, 2005 10:01 am

Re: Digitally signing a document

Postby Antonio Linares » Fri Apr 19, 2024 12:28 pm

Great web for examples!

thank you
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot], Natter and 63 guests