5
votes

How can I sign a pdf file using digital signature in perl? I'm able to extract the content of pdf and to generate signature string via

# $content -> pdf content to be signed

my $private_key Crypt::OpenSSL::RSA->new_private_key($key_string);
$private_key->use_md5_hash();
my $signature = $private_key->sign($content);

Consequently, the $signature can be converted into hex format, but I don't know how to add it into PDF file. I know that the signature in hex form should be placed in /Contents field and the range of signed text is given by /ByteRange array.

Is there any module for this purpose? Or can you give me any advice how to do it? Thanks

2

2 Answers

2
votes

you may find this script interesting ... http://mschuette.name/files/pdfsign.pl it uses PDF::API2 and Crypt::OpenSSL todo its magic ... it could probably serve as a basis for creating something of your own.

0
votes
use Crypt::OpenSSL::RSA;
use File::Slurp;

my $keystring = read_file('private_key.pem');
my $private_key = Crypt::OpenSSL::RSA->new_private_key($keystring);
$private_key->use_md5_hash();
my $signature = $private_key->sign($plaintext);