Skip to content

Commit a81cf84

Browse files
committed
Various updates in view to new SetaPDF and PHP version
- Changed all used class names to new namespaced variation of SetaPDF. - Small code optimizations.
1 parent c7c06f3 commit a81cf84

4 files changed

Lines changed: 58 additions & 60 deletions

File tree

examples/appearance-demo.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
use Aws\Kms\KmsClient;
44
use setasign\SetaPDF\Signer\Module\AwsKMS\Module;
5+
use setasign\SetaPDF2\Core\Document;
6+
use setasign\SetaPDF2\Core\Writer\FileWriter;
7+
use setasign\SetaPDF2\Signer\Signature\Appearance\Dynamic as DynamicAppearance;
8+
use setasign\SetaPDF2\Signer\SignatureField;
9+
use setasign\SetaPDF2\Signer\Signer;
10+
use setasign\SetaPDF2\Signer\ValidationRelatedInfo\IntegrityResult;
511

612
require_once __DIR__ . '/../vendor/autoload.php';
713

@@ -25,25 +31,31 @@
2531
$awsKmsModule->setSignatureAlgorithm($signatureAlgorithm);
2632

2733
// create a writer instance
28-
$writer = new SetaPDF_Core_Writer_File($resultPath);
34+
$writer = new FileWriter($resultPath);
2935
// create the document instance
30-
$document = SetaPDF_Core_Document::loadByFilename($fileToSign, $writer);
36+
$document = Document::loadByFilename($fileToSign, $writer);
3137

3238
// create the signer instance
33-
$signer = new SetaPDF_Signer($document);
39+
$signer = new Signer($document);
3440

3541
$field = $signer->addSignatureField(
3642
'Signature',
3743
1,
38-
SetaPDF_Signer_SignatureField::POSITION_RIGHT_TOP,
44+
SignatureField::POSITION_RIGHT_TOP,
3945
['x' => -160, 'y' => -100],
4046
180,
4147
60
4248
);
4349

44-
$signer->setSignatureFieldName($field->getQualifiedName());
50+
$fieldName = $field->getQualifiedName();
51+
$signer->setSignatureFieldName($fieldName);
4552

46-
$appearance = new SetaPDF_Signer_Signature_Appearance_Dynamic($awsKmsModule);
53+
$appearance = new DynamicAppearance($awsKmsModule);
4754
$signer->setAppearance($appearance);
4855

4956
$signer->sign($awsKmsModule);
57+
58+
// verify the integrity to check if e.g. both private key and public key in the certificate match:
59+
$document = Document::loadByFilename($resultPath);
60+
$integrityResult = IntegrityResult::create($document, $fieldName);
61+
var_dump($integrityResult->isValid() ? 'Valid' : 'Not Valid! Double check that the Certificate matches the private key!');

examples/demo.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
use Aws\Kms\KmsClient;
44
use setasign\SetaPDF\Signer\Module\AwsKMS\Module;
5+
use setasign\SetaPDF2\Core\Document;
6+
use setasign\SetaPDF2\Core\Writer\FileWriter;
7+
use setasign\SetaPDF2\Signer\Signer;
8+
use setasign\SetaPDF2\Signer\ValidationRelatedInfo\IntegrityResult;
59

610
require_once __DIR__ . '/../vendor/autoload.php';
711

@@ -25,10 +29,17 @@
2529
$awsKmsModule->setSignatureAlgorithm($signatureAlgorithm);
2630

2731
// create a writer instance
28-
$writer = new SetaPDF_Core_Writer_File($resultPath);
32+
$writer = new FileWriter($resultPath);
2933
// create the document instance
30-
$document = SetaPDF_Core_Document::loadByFilename($fileToSign, $writer);
34+
$document = Document::loadByFilename($fileToSign, $writer);
3135

3236
// create the signer instance
33-
$signer = new SetaPDF_Signer($document);
37+
$signer = new Signer($document);
38+
$fieldName = $signer->addSignatureField()->getQualifiedName();
39+
$signer->setSignatureFieldName($fieldName);
3440
$signer->sign($awsKmsModule);
41+
42+
// verify the integrity to check if e.g. both private key and public key in the certificate match:
43+
$document = Document::loadByFilename($resultPath);
44+
$integrityResult = IntegrityResult::create($document, $fieldName);
45+
var_dump($integrityResult->isValid() ? 'Valid' : 'Not Valid! Double check that the Certificate matches the private key!');

src/Exception.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<?php
22

33
/**
4-
* @copyright Copyright (c) 2021 Setasign GmbH & Co. KG (https://www.setasign.com)
4+
* @copyright Copyright (c) 2026 Setasign GmbH & Co. KG (https://www.setasign.com)
55
* @license http://opensource.org/licenses/mit-license The MIT License
66
*/
77

88
namespace setasign\SetaPDF\Signer\Module\AwsKMS;
99

10-
use SetaPDF_Signer_Exception;
11-
12-
class Exception extends SetaPDF_Signer_Exception
10+
class Exception extends \setasign\SetaPDF2\Signer\Exception
1311
{
1412

1513
}

src/Module.php

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,32 @@
11
<?php
22

33
/**
4-
* @copyright Copyright (c) 2021 Setasign GmbH & Co. KG (https://www.setasign.com)
4+
* @copyright Copyright (c) 2026 Setasign GmbH & Co. KG (https://www.setasign.com)
55
* @license http://opensource.org/licenses/mit-license The MIT License
66
*/
77

88
namespace setasign\SetaPDF\Signer\Module\AwsKMS;
99

1010
use Aws\Kms\KmsClient;
11-
use SetaPDF_Core_Reader_FilePath;
12-
use SetaPDF_Signer_Asn1_Element as Asn1Element;
13-
use SetaPDF_Signer_Asn1_Oid as Asn1Oid;
14-
use SetaPDF_Signer_Digest as Digest;
15-
use SetaPDF_Signer_Signature_DictionaryInterface;
16-
use SetaPDF_Signer_Signature_DocumentInterface;
17-
use SetaPDF_Signer_Signature_Module_ModuleInterface;
18-
use SetaPDF_Signer_Signature_Module_PadesProxyTrait;
11+
use setasign\SetaPDF2\Core\Reader\FilePath;
12+
use setasign\SetaPDF2\Signer\Asn1\Element as Asn1Element;
13+
use setasign\SetaPDF2\Signer\Asn1\Oid as Asn1Oid;
14+
use setasign\SetaPDF2\Signer\Digest;
15+
use setasign\SetaPDF2\Signer\Signature\Module\DictionaryInterface;
16+
use setasign\SetaPDF2\Signer\Signature\Module\DocumentInterface;
17+
use setasign\SetaPDF2\Signer\Signature\Module\ModuleInterface;
18+
use setasign\SetaPDF2\Signer\Signature\Module\PadesProxyTrait;
1919

2020
class Module implements
21-
SetaPDF_Signer_Signature_Module_ModuleInterface,
22-
SetaPDF_Signer_Signature_DictionaryInterface,
23-
SetaPDF_Signer_Signature_DocumentInterface
21+
ModuleInterface,
22+
DictionaryInterface,
23+
DocumentInterface
2424
{
25-
use SetaPDF_Signer_Signature_Module_PadesProxyTrait;
25+
use PadesProxyTrait;
2626

27-
/**
28-
* @var KmsClient
29-
*/
30-
protected $kmsClient;
31-
32-
/**
33-
* @var string
34-
*/
35-
protected $keyId;
36-
37-
/**
38-
* @var string|null
39-
*/
40-
protected $signatureAlgorithm;
27+
protected KmsClient $kmsClient;
28+
protected string $keyId;
29+
protected ?string $signatureAlgorithm;
4130

4231
/**
4332
* Module constructor.
@@ -53,28 +42,16 @@ public function __construct($keyId, KmsClient $kmsClient)
5342

5443
/**
5544
* @param string $signatureAlgorithm
45+
* @throws Exception
5646
*/
5747
public function setSignatureAlgorithm($signatureAlgorithm)
5848
{
59-
switch ($signatureAlgorithm) {
60-
case 'RSASSA_PKCS1_V1_5_SHA_256':
61-
case 'RSASSA_PSS_SHA_256':
62-
case 'ECDSA_SHA_256':
63-
$digest = Digest::SHA_256;
64-
break;
65-
case 'RSASSA_PKCS1_V1_5_SHA_384':
66-
case 'RSASSA_PSS_SHA_384':
67-
case 'ECDSA_SHA_384':
68-
$digest = Digest::SHA_384;
69-
break;
70-
case 'RSASSA_PKCS1_V1_5_SHA_512':
71-
case 'RSASSA_PSS_SHA_512':
72-
case 'ECDSA_SHA_512':
73-
$digest = Digest::SHA_512;
74-
break;
75-
default:
76-
throw new Exception('Unknown algorithm "%s".', $signatureAlgorithm);
77-
}
49+
$digest = match ($signatureAlgorithm) {
50+
'RSASSA_PKCS1_V1_5_SHA_256', 'RSASSA_PSS_SHA_256', 'ECDSA_SHA_256' => Digest::SHA_256,
51+
'RSASSA_PKCS1_V1_5_SHA_384', 'RSASSA_PSS_SHA_384', 'ECDSA_SHA_384' => Digest::SHA_384,
52+
'RSASSA_PKCS1_V1_5_SHA_512', 'RSASSA_PSS_SHA_512', 'ECDSA_SHA_512' => Digest::SHA_512,
53+
default => throw new Exception('Unknown algorithm "%s".', $signatureAlgorithm),
54+
};
7855

7956
$this->signatureAlgorithm = $signatureAlgorithm;
8057
$this->_getPadesModule()->setDigest($digest);
@@ -91,7 +68,7 @@ public function getSignatureAlgorithm()
9168
/**
9269
* @inheritDoc
9370
*/
94-
public function createSignature(SetaPDF_Core_Reader_FilePath $tmpPath)
71+
public function createSignature(FilePath $tmpPath)
9572
{
9673
// ensure certificate
9774
$certificate = $this->getCertificate();

0 commit comments

Comments
 (0)