|
THotPDF.SignPDFWithPFX
|
上一頁 方法 AddPubKeyRecipient |
|
Signs an existing PDF placeholder using a PFX / PKCS#12 file, building a CMS SignedData container and writing the signed PDF in one call.
Delphi syntax (file overload): class function SignPDFWithPFX( const InputPDFPath: string; const OutputPDFPath: string; const PFXFilePath: string; const Password: AnsiString): boolean; overload; static;
Delphi syntax (stream overload): class function SignPDFWithPFX( InputStream: TStream; OutputStream: TStream; const PFXFilePath: string; const Password: AnsiString): boolean; overload; static;
說明 SignPDFWithPFX 是 v2.119.27 新增的端對端 PFX 簽章入口點。輸入 PDF 必須已包含由 THPDFPage.AddSignedSignatureField(或其 PAdES wrapper)發出、subFilter 為
1. 以有界的 256 KB 緩衝區複製輸入,或重用同一個可 seek 的可讀寫串流,找出 /ByteRange 與 /Contents placeholder,並以實際 byte offset 用 seek 修補固定寬度的 /ByteRange 欄位 2. 載入 PFX 檔案並以提供的 password 解密。PBES2 接受 AES-128-CBC、AES-192-CBC 與 AES-256-CBC,搭配預設的 HMAC-SHA-1,或明確指定的 HMAC-SHA-256、HMAC-SHA-384 與 HMAC-SHA-512 PBKDF2 PRF。選用的 PBKDF2 3. 依 public-key 材料與完全相符的 RFC 2985 4. 依配對到的金鑰選擇簽章方案、計算所需的文件摘要,並建立 CMS SignedData (RFC 5652) DER blob,內含 signer certificate、去除重複 DER 憑證後的對應鏈與 signed attributes。RSA 與 ECDSA 對 attribute digest 簽章;Ed25519 與 Ed448 以 PureEdDSA 模式對完整的 DER 5. 把 CMS DER 轉成十六進位編碼,確認能放進 AddSignedSignatureField 預留的 /Contents 預算(預設 8 KB 足以涵蓋 1024 / 2048-bit RSA),再注入 placeholder 6. 把修補後的位元組寫到輸出路徑或串流
成功時傳回 True。password 錯誤、加密或 private-key 設定檔不支援、PFX 格式錯誤、身分不明確、issuer 不明確或憑證鏈成環時丟出
Typical workflow
Doc := THotPDF.Create(nil); Doc.FileName := 'unsigned.pdf'; Doc.BeginDoc; Doc.CurrentPage.AddSignedSignatureField( 'Sig1', Rect(60, 60, 260, 90), 8192, 'adbe.pkcs7.detached', 'Approved', 'Brussels', '', []); Doc.EndDoc; Doc.Free; THotPDF.SignPDFWithPFX('unsigned.pdf', 'signed.pdf', 'mykey.pfx', 'mypassword');
Multi-identity PFX files 便利多載會刻意拒絕含有一個以上相符 certificate 與 private key 的 PFX。要檢視具決定性的身分清單及其 端對端簽章時,請以
Notes RSA 與 ECDSA 的 PFX 簽章走 HotPDF 原生與 Windows 密碼學路徑。Ed25519 與 Ed448 的 PFX 簽章需要 OpenSSL 3 簽章演算法為 RSA + SHA-256 (1.2.840.113549.1.1.1 + 2.16.840.1.101.3.4.2.1)。signer identifier 是從 X.509 certificate 擷取的 IssuerAndSerialNumber。encapContentInfo 為 detached(省略 eContent)。Signed attributes 會依 RFC 5652 §5.4,在雜湊前按 DER byte string 遞增排序
對於需要 RFC 3161 timestamps、DSS dictionaries 或 document timestamp signatures 的 PAdES B-T / B-LT / B-LTA 工作流程,producer-side helpers(AddPAdESSignatureField、AddPAdESDSSCertificate、AddDocumentTimestampSignature)仍然適用;SignPDFWithPFX 本身會輸出基礎的 CMS-only signature(等同 PAdES-B-B) stream 多載從不會在記憶體中保留完整的第二份 PDF。它會以有界區段對修補後的輸出計算雜湊,並在不改變檔案長度的前提下以 seek 方式修補十六進位的 CMS 值。固定寬度的十進位位移可支援小於 10,000,000,000 bytes 的已備妥 PDF。
請參閱: AddSignedSignatureField, PreparePDFForSigning, InsertSignatureHex, AddPAdESSignatureField |