gpt4 book ai didi

perl - 如何修复 Perl 脚本中 strftime 的使用错误

转载 作者:行者123 更新时间:2023-12-02 14:47:52 27 4
gpt4 key购买 nike

我正在编写 Perl 脚本 signature.pl 来构建 PDF 字典和签名字典对象这在声明的变量中有 PEM 和 PDF。我已经将 POSIX 用于 strftime

use strict;
use warnings;
use PDF::API2;
use PDF::API2::Basic::PDF::Utils;
use File::Slurp;
use POSIX;

use Crypt::OpenSSL::RSA;
use Crypt::OpenSSL::X509 qw/FORMAT_UNDEF FORMAT_ASN1 FORMAT_TEXT FORMAT_PEM/;
use Digest::SHA1 qw(sha1 sha1_hex);

my $add_mdp = 0;
my $input_filename = "signature.pdf";
my $tempfilename = 'tmp.pdf';
my $outfilename = 'tmp.pdf';

my $sig_algorithm = 'pkcs7';
my $sig_length = 20480;

my $cacert_filename = "signature.pem";
my $x509_filename = "signature.pem";
my $priv_key_filename = "signature.pem";

my $cacert = Crypt::OpenSSL::X509->new_from_file($cacert_filename);
my $x509 = Crypt::OpenSSL::X509->new_from_file($x509_filename);
my $priv_key = read_file($priv_key_filename);

my $timestamp = localtime;
my $tz = POSIX::strftime("%z", $timestamp);
$tz =~ s/([\+\-])(\d\d)(\d\d)/$1$2'$3'/;
my $timestring = POSIX::strftime("D:%Y%m%d%H%M%S$tz", $timestamp);

my $location = `hostname`;
chomp $location;

my $contact_info = PDFStr($x509->email());

my $signer_name = PDFStr($x509->subject_name->as_string);

my $pdf = PDF::API2->open($input_filename);
my $p = $pdf->{catalog}->{' parent'};

my $sigdict = PDF::API2::Basic::PDF::Dict->new();
$sigdict->{Type} = PDFName("Sig");
$sigdict->{Filter} = PDFName("Adobe.PPKLite");
$sigdict->{Reason} = PDFStr("Testing my PDF Signature Demo Tool");
$sigdict->{Name} = $signer_name;
$sigdict->{ContactInfo} = $contact_info;
$sigdict->{Location} = $location;
$sigdict->{M} = $timestring;

if ($sig_algorithm eq 'rsa') {
$sigdict->{SubFilter} = PDFName('adbe.x509.rsa.sha1');
my @certs;
push @certs, PDFStr $x509->as_string(FORMAT_ASN1);
push @certs, PDFStr $cacert->as_string(FORMAT_ASN1);
$sigdict->{Cert} = PDFArray @certs if ($sig_algorithm eq 'rsa');
} else {
$sigdict->{SubFilter} = PDFName('adbe.pkcs7.detached');
}

$sigdict->{Contents} = PDFStrHex("\0" x $sig_length);
$sigdict->{ByteRange} = PDF::API2::Basic::PDF::Literal->new("[0 00000000 00000000 00000000]");

if ($add_mdp) {
my $sigrefdict = PDF::API2::Basic::PDF::Dict->new();
$sigrefdict->{Type} = PDFName("SigRef");
$sigrefdict->{TransformMethod} = PDFName("DocMDP");
$sigrefdict = $p->new_obj($sigrefdict);
$sigdict->{Reference} = PDFArray($sigrefdict);
}

$sigdict = $p->new_obj($sigdict);

my $sigannotdict = PDF::API2::Basic::PDF::Dict->new();
my $sigformdict = PDF::API2::Basic::PDF::Dict->new();

$sigannotdict->{Type} = PDFName("Annot");
$sigannotdict->{Subtype} = PDFName("Widget");
$sigannotdict->{F} = PDFNum(4);
$sigannotdict->{Parent} = $sigformdict;
$sigannotdict->{Rect} = PDF::API2::Basic::PDF::Literal->new("[0 0 0 0]");
$sigannotdict->{P} = $pdf->openpage(1);
$sigannotdict->{H} = PDFName("N");
$sigannotdict = $p->new_obj($sigannotdict);

$sigformdict->{FT} = PDFName("Sig");
$sigformdict->{T} = PDFStr("Demo Signature");
$sigformdict->{V} = $sigdict;
$sigformdict->{Kids} = PDFArray($sigannotdict);
$sigformdict = $p->new_obj($sigformdict);

if ($add_mdp) {
my $permdict = PDF::API2::Basic::PDF::Dict->new();
$permdict->{DocMDP} = $sigdict;
$permdict = $p->new_obj($permdict);
$pdf->{catalog}->{'Perm'} = $permdict;
}

my @formarray;
push @formarray, $sigformdict;
my $acroformdict = PDF::API2::Basic::PDF::Dict->new();
$acroformdict->{Fields} = PDFArray @formarray;
$acroformdict->{SigFlags} = PDFNum(3);
$acroformdict = $p->new_obj($acroformdict);

$pdf->{catalog}->{'AcroForm'} = $acroformdict;
$pdf->{pdf}->out_obj($pdf->{catalog});


$pdf->saveas($tempfilename);
print "added AcroForm: $input_filename --> $tempfilename";

我收到如下警告:

Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1) at signature.pl line 29

我使用了 https://mschuette.name/files/pdfsign.pl 中的代码和 https://www.perlmonks.org/?node_id=1171455

有人可以帮忙吗?提前致谢

最佳答案

在列表上下文中,localtime返回 9 个元素的列表。然而,

In scalar context, localtime returns the ctime(3) value:

如果你打印$timestamp,你会得到如下内容:

Tue Sep 10 15:37:22 2019

strftime ,正如您的错误消息所说(并且如文档所示)需要 7 个参数。因此,您应该将 localtime 在列表上下文而不是标量上下文中返回的内容传递给它。也就是说,使用:

my @localtime  = localtime;
my $tz = POSIX::strftime("%z", @localtime);
$tz =~ s/([\+\-])(\d\d)(\d\d)/$1$2'$3'/;
my $timestring = POSIX::strftime("D:%Y%m%d%H%M%S$tz", @localtime);

举一个小例子来重现我在回答中所说的内容:

my $time = localtime;
say strftime "%Y", $time;

会崩溃,而

my @time = localtime;
say strftime "%Y", @time;

将正常工作。

关于perl - 如何修复 Perl 脚本中 strftime 的使用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57871230/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com