如何使用HSM以C#數位簽署PDF
IronPDF 可透過 PKCS#11 API 使用硬體安全模組 (HSM) 進行安全的 PDF 簽署,私密金鑰永遠不會離開實體裝置,為需要防篡改數位簽章的關鍵任務應用程式提供銀行等級的安全性。
快速入門:在C#中使用HSM簽署PDF-
透過NuGet安裝IronPDF:
Install-Package IronPdf -
配置您的HSM設備 (或使用SoftHSM進行測試)
-
使用您的HSM憑證建立
UsbPkcs11HsmSigner:-
1使用 IronPDF 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var hsmSigner = new UsbPkcs11HsmSigner(libraryPath, pin, tokenLabel, keyLabel);Dim hsmSigner As New UsbPkcs11HsmSigner(libraryPath, pin, tokenLabel, keyLabel)Dim hsmSigner As New UsbPkcs11HsmSigner(libraryPath, pin, tokenLabel, keyLabel) -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronPDF,並使用 免費試用
-
-
生成您的PDF並進行簽名:
var pdf = renderer.RenderHtmlAsPdf("<h1>Document</h1>"); pdf.SignAndSave("signed.pdf", hsmSigner);var pdf = renderer.RenderHtmlAsPdf("<h1>Document</h1>"); pdf.SignAndSave("signed.pdf", hsmSigner);Dim pdf = renderer.RenderHtmlAsPdf("<h1>Document</h1>") pdf.SignAndSave("signed.pdf", hsmSigner)Dim pdf = renderer.RenderHtmlAsPdf("<h1>Document</h1>") pdf.SignAndSave("signed.pdf", hsmSigner) -
在您的PDF檢視器中驗證簽名
在許多應用程式中,為 PDF 文件添加簽名是一項常見需求。 然而,關鍵任務應用程式需要更高的安全性,其中的金鑰不能被篡改。 一般來說,利用.pfx文件進行簽名操作就像在家裡有一把萬能鑰匙。 應用程式會將金鑰載入記憶體以簽署文件。 如果電腦受到攻擊,金鑰可能會被竊取。
更安全的選擇是使用硬體安全模組 (HSM)。 使用 HSM(如 USB 令牌)時,私密金鑰會在裝置內部產生,無法離開裝置。
這個過程就像是將文件帶到銀行。 應用程序提供一個PIN,並且HSM將文件帶到保險庫,使用鑰匙蓋章然後返回蓋章後文件。 鑰匙從未離開保險庫。 這提供了額外的安全性,因為金鑰無法被複製或竊取。
如何使用 HSM 對 PDF 進行數位簽名
- 下載IronPDF C#程式庫以使用HSM進行PDF簽名
- 匯入您現有的 HSM 或使用其他函式庫對其進行模擬。
- 建立一個新的
UsbPkcs11HsmSigner對象 - 使用
SignAndSave對 PDF 進行簽名並儲存。 - 使用 PDF 檢視器驗證 PDF 輸出和憑證。
如何使用HSM簽署PDF?
使用 HSM 進行簽名通常需要一個實體設備,例如 USB 令牌,應用程式需要與該設備進行互動。 IronPDF 與這些作業相容,因為函式庫和標準 HSM 都使用 PKCS#11 作為共同的 API。 為了示範目的,本指南使用模擬 HSM 而非實體 HSM。
在生產或即時測試環境中,您不應該使用此模擬。 相反,請使用您實際的 HSM。 對於生產環境,考慮實施額外的PDF安全功能,如密碼保護和權限,以實現全面的文件保護。
要運行此模擬,您必須先安裝SoftHSM,OpenSSL,和OpenSC以生成必要的鑰匙和令牌。 如需使用 SoftHSM 的詳細資訊,請參閱其公開的 GitHub 儲存庫。
在實施HSM簽名之前,確保您已正確安裝IronPDF並配置好您的授權金鑰以供生產使用。
從 HTML 字串開始建立 PDF。 在下面的範例中,我們定義了模擬 SoftHSM 的路徑和憑證。 這包括提供SoftHSM .dll 程式庫文件和您創建的.crt證書文件的絕對路徑。
接下來,指定輸出路徑,此例中的路徑為output.pdf。
定義三個字串:hsmTokenLabel, hsmPin, 和 hsmKeyLabel。 這些字串區分大小寫,並且必須與您使用 SoftHSM 產生令牌和憑證時建立的憑證完全相符。 然後初始化UsbPkcs11HsmSigner物件,將SoftHSM程式庫路徑、PIN、令牌標籤和鑰匙標籤作為參數傳遞。
另外,創建一個PdfSignatureImage以在文件上添加簽名的視覺表示。 最後,調用SignAndSave,這會使用hsmSigner來簽署文件並將其保存到指定的輸出路徑。
HSM簽名程式碼如何運作?
using IronPdf;
using IronPdf.Signing;
using IronSoftware.Pdfium.Signing;
using System.Drawing;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Testing</h1>");
// Define Paths and Credentials
string softhsmLibraryPath = @"D:\SoftHSM2\lib\softhsm2-x64.dll";
// These MUST match what you created
string hsmTokenLabel = "MyTestToken";
string hsmPin = "123456";
string hsmKeyLabel = "my-key"; // The label for the key *inside* the token
// Create the HsmSigner object.
UsbPkcs11HsmSigner hsmSigner = new UsbPkcs11HsmSigner(
softhsmLibraryPath,
hsmPin,
hsmTokenLabel,
hsmKeyLabel
);
// Create the Signature Image
string signatureImagePath = "IronSoftware.png";
PdfSignatureImage sigImage = new PdfSignatureImage(signatureImagePath, 0, new Rectangle(50, 50, 150, 150));
// Sign PDF with HSM
pdf.SignAndSave("signedWithHSM.pdf", hsmSigner);using IronPdf;
using IronPdf.Signing;
using IronSoftware.Pdfium.Signing;
using System.Drawing;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Testing</h1>");
// Define Paths and Credentials
string softhsmLibraryPath = @"D:\SoftHSM2\lib\softhsm2-x64.dll";
// These MUST match what you created
string hsmTokenLabel = "MyTestToken";
string hsmPin = "123456";
string hsmKeyLabel = "my-key"; // The label for the key *inside* the token
// Create the HsmSigner object.
UsbPkcs11HsmSigner hsmSigner = new UsbPkcs11HsmSigner(
softhsmLibraryPath,
hsmPin,
hsmTokenLabel,
hsmKeyLabel
);
// Create the Signature Image
string signatureImagePath = "IronSoftware.png";
PdfSignatureImage sigImage = new PdfSignatureImage(signatureImagePath, 0, new Rectangle(50, 50, 150, 150));
// Sign PDF with HSM
pdf.SignAndSave("signedWithHSM.pdf", hsmSigner);Imports IronPdf
Imports IronPdf.Signing
Imports IronSoftware.Pdfium.Signing
Imports System.Drawing
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Testing</h1>")
' Define Paths and Credentials
Dim softhsmLibraryPath As String = "D:\SoftHSM2\lib\softhsm2-x64.dll"
' These MUST match what you created
Dim hsmTokenLabel As String = "MyTestToken"
Dim hsmPin As String = "123456"
Dim hsmKeyLabel As String = "my-key" ' The label for the key *inside* the token
' Create the HsmSigner object.
Dim hsmSigner As New UsbPkcs11HsmSigner(softhsmLibraryPath, hsmPin, hsmTokenLabel, hsmKeyLabel)
' Create the Signature Image
Dim signatureImagePath As String = "IronSoftware.png"
Dim sigImage As New PdfSignatureImage(signatureImagePath, 0, New Rectangle(50, 50, 150, 150))
' Sign PDF with HSM
pdf.SignAndSave("signedWithHSM.pdf", hsmSigner)Imports IronPdf
Imports IronPdf.Signing
Imports IronSoftware.Pdfium.Signing
Imports System.Drawing
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Testing</h1>")
' Define Paths and Credentials
Dim softhsmLibraryPath As String = "D:\SoftHSM2\lib\softhsm2-x64.dll"
' These MUST match what you created
Dim hsmTokenLabel As String = "MyTestToken"
Dim hsmPin As String = "123456"
Dim hsmKeyLabel As String = "my-key" ' The label for the key *inside* the token
' Create the HsmSigner object.
Dim hsmSigner As New UsbPkcs11HsmSigner(softhsmLibraryPath, hsmPin, hsmTokenLabel, hsmKeyLabel)
' Create the Signature Image
Dim signatureImagePath As String = "IronSoftware.png"
Dim sigImage As New PdfSignatureImage(signatureImagePath, 0, New Rectangle(50, 50, 150, 150))
' Sign PDF with HSM
pdf.SignAndSave("signedWithHSM.pdf", hsmSigner)UsbPkcs11HsmSigner額外接受兩個可選參數:digestAlgorithm 和 signingAlgorithm。 默認情況下,它們設置為SHA256 和 RSA。
處理不同的HSM配置
不同的 HSM 裝置可能需要特定的配置。 以下是一個範例,說明如何使用自訂演算法設定簽章器,以及處理多重簽章:
// Configure with custom algorithms
var customHsmSigner = new UsbPkcs11HsmSigner(
hsmLibraryPath,
hsmPin,
hsmTokenLabel,
hsmKeyLabel,
digestAlgorithm: IronPdf.Signing.DigestAlgorithm.SHA512,
signingAlgorithm: IronPdf.Signing.SigningAlgorithm.RSA
);
// Apply signature with custom location and reason
var signatureOptions = new SignatureOptions
{
SignerName = "Corporate Signing Authority",
Location = "Company Headquarters",
Reason = "Contract Approval"
};
// Load existing PDF for signing
var existingPdf = PdfDocument.FromFile("contract.pdf");
existingPdf.SignAndSave("contract-signed.pdf", customHsmSigner, signatureOptions);// Configure with custom algorithms
var customHsmSigner = new UsbPkcs11HsmSigner(
hsmLibraryPath,
hsmPin,
hsmTokenLabel,
hsmKeyLabel,
digestAlgorithm: IronPdf.Signing.DigestAlgorithm.SHA512,
signingAlgorithm: IronPdf.Signing.SigningAlgorithm.RSA
);
// Apply signature with custom location and reason
var signatureOptions = new SignatureOptions
{
SignerName = "Corporate Signing Authority",
Location = "Company Headquarters",
Reason = "Contract Approval"
};
// Load existing PDF for signing
var existingPdf = PdfDocument.FromFile("contract.pdf");
existingPdf.SignAndSave("contract-signed.pdf", customHsmSigner, signatureOptions);Imports IronPdf
' Configure with custom algorithms
Dim customHsmSigner As New UsbPkcs11HsmSigner(
hsmLibraryPath,
hsmPin,
hsmTokenLabel,
hsmKeyLabel,
digestAlgorithm:=IronPdf.Signing.DigestAlgorithm.SHA512,
signingAlgorithm:=IronPdf.Signing.SigningAlgorithm.RSA
)
' Apply signature with custom location and reason
Dim signatureOptions As New SignatureOptions With {
.SignerName = "Corporate Signing Authority",
.Location = "Company Headquarters",
.Reason = "Contract Approval"
}
' Load existing PDF for signing
Dim existingPdf As PdfDocument = PdfDocument.FromFile("contract.pdf")
existingPdf.SignAndSave("contract-signed.pdf", customHsmSigner, signatureOptions)Imports IronPdf
' Configure with custom algorithms
Dim customHsmSigner As New UsbPkcs11HsmSigner(
hsmLibraryPath,
hsmPin,
hsmTokenLabel,
hsmKeyLabel,
digestAlgorithm:=IronPdf.Signing.DigestAlgorithm.SHA512,
signingAlgorithm:=IronPdf.Signing.SigningAlgorithm.RSA
)
' Apply signature with custom location and reason
Dim signatureOptions As New SignatureOptions With {
.SignerName = "Corporate Signing Authority",
.Location = "Company Headquarters",
.Reason = "Contract Approval"
}
' Load existing PDF for signing
Dim existingPdf As PdfDocument = PdfDocument.FromFile("contract.pdf")
existingPdf.SignAndSave("contract-signed.pdf", customHsmSigner, signatureOptions)這種方法特別有用於需要特定合規性標準的數位簽名範例,或者當您需要添加元數據來跟蹤簽名細節時。
如何添加信任的時間戳到HSM簽名?
簽名記錄誰簽署了文件。 信任的時間戳獨立於簽名機器的時鐘記錄何時。 將TimeStampUrl設置為RFC 3161時間戳機構的端點,生成的簽名帶有時間戳記令牌,可以被Adobe Acrobat等符合標準的讀者識別。這對於長期驗證和需要可驗證簽名時間的合規性制度很重要。
using IronPdf;
using IronPdf.Signing;
using IronSoftware.Pdfium.Signing;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Timestamped Document</h1>");
// Define Paths and Credentials
string softhsmLibraryPath = @"D:\SoftHSM2\lib\softhsm2-x64.dll";
// These MUST match what you created
string hsmTokenLabel = "MyTestToken";
string hsmPin = "123456";
string hsmKeyLabel = "my-key"; // The label for the key *inside* the token
// Create the HsmSigner object.
UsbPkcs11HsmSigner hsmSigner = new UsbPkcs11HsmSigner(
softhsmLibraryPath,
hsmPin,
hsmTokenLabel,
hsmKeyLabel
);
// Point the signer at an RFC 3161 Time Stamp Authority. The endpoint is a string.
hsmSigner.TimeStampUrl = "https://freetsa.org/tsr";
// Optional. Selects the digest the TSA is asked to use.
hsmSigner.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA512;
// SignAndSave is unchanged - the timestamp is configured on the signer beforehand.
pdf.SignAndSave("signedWithTimestamp.pdf", hsmSigner);using IronPdf;
using IronPdf.Signing;
using IronSoftware.Pdfium.Signing;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Timestamped Document</h1>");
// Define Paths and Credentials
string softhsmLibraryPath = @"D:\SoftHSM2\lib\softhsm2-x64.dll";
// These MUST match what you created
string hsmTokenLabel = "MyTestToken";
string hsmPin = "123456";
string hsmKeyLabel = "my-key"; // The label for the key *inside* the token
// Create the HsmSigner object.
UsbPkcs11HsmSigner hsmSigner = new UsbPkcs11HsmSigner(
softhsmLibraryPath,
hsmPin,
hsmTokenLabel,
hsmKeyLabel
);
// Point the signer at an RFC 3161 Time Stamp Authority. The endpoint is a string.
hsmSigner.TimeStampUrl = "https://freetsa.org/tsr";
// Optional. Selects the digest the TSA is asked to use.
hsmSigner.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA512;
// SignAndSave is unchanged - the timestamp is configured on the signer beforehand.
pdf.SignAndSave("signedWithTimestamp.pdf", hsmSigner);Imports IronPdf
Imports IronPdf.Signing
Imports IronSoftware.Pdfium.Signing
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Timestamped Document</h1>")
' Define Paths and Credentials
Dim softhsmLibraryPath As String = "D:\SoftHSM2\lib\softhsm2-x64.dll"
' These MUST match what you created
Dim hsmTokenLabel As String = "MyTestToken"
Dim hsmPin As String = "123456"
Dim hsmKeyLabel As String = "my-key" ' The label for the key *inside* the token
' Create the HsmSigner object.
Dim hsmSigner As New UsbPkcs11HsmSigner(softhsmLibraryPath, hsmPin, hsmTokenLabel, hsmKeyLabel)
' Point the signer at an RFC 3161 Time Stamp Authority. The endpoint is a string.
hsmSigner.TimeStampUrl = "https://freetsa.org/tsr"
' Optional. Selects the digest the TSA is asked to use.
hsmSigner.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA512
' SignAndSave is unchanged - the timestamp is configured on the signer beforehand.
pdf.SignAndSave("signedWithTimestamp.pdf", hsmSigner)Imports IronPdf
Imports IronPdf.Signing
Imports IronSoftware.Pdfium.Signing
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Timestamped Document</h1>")
' Define Paths and Credentials
Dim softhsmLibraryPath As String = "D:\SoftHSM2\lib\softhsm2-x64.dll"
' These MUST match what you created
Dim hsmTokenLabel As String = "MyTestToken"
Dim hsmPin As String = "123456"
Dim hsmKeyLabel As String = "my-key" ' The label for the key *inside* the token
' Create the HsmSigner object.
Dim hsmSigner As New UsbPkcs11HsmSigner(softhsmLibraryPath, hsmPin, hsmTokenLabel, hsmKeyLabel)
' Point the signer at an RFC 3161 Time Stamp Authority. The endpoint is a string.
hsmSigner.TimeStampUrl = "https://freetsa.org/tsr"
' Optional. Selects the digest the TSA is asked to use.
hsmSigner.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA512
' SignAndSave is unchanged - the timestamp is configured on the signer beforehand.
pdf.SignAndSave("signedWithTimestamp.pdf", hsmSigner)TimeStampUrl將端點作為string來使用。 將其留作null、空白或空格將完全跳過時間戳:不會調用TSA,簽名行為與沒有一樣。
TimestampHashAlgorithm選擇TSA被要求生成的摘要,並接受SHA1, SHA256, 或 SHA512。 在TimeStampUrl未設置時被忽略。
digestAlgorithm構造函數參數不同。
digestAlgorithm管理簽名本身,而TimestampHashAlgorithm僅管理從機構請求的時間戳記。 兩者都默認為內建簽署者的SHA-256,因此很容易將兩者混淆。AHsmSigner,是UsbPkcs11HsmSigner背後的基本類別。 直接實施IHsmSigner的自定義簽名者,而不是從AHsmSigner派生,會得到未初始化的枚舉值 - 即SHA1,這是一種印記,越來越多的TSA服務器會直接拒絕。 在自訂實現中顯式設置該屬性。SignAndSave保持不變。 時間戳被預先配置在簽署者上,而不是作為參數傳遞,因此現有的調用會繼續正常運行。
常見的HSM配置問題是什麼?
如果在執行程式碼範例時遇到以下錯誤,請依照下列故障排除步驟進行偵錯和驗證您的配置。 如需更多數位簽名問題的幫助,請查閱我們的數位簽名故障排除指南。
此CKR_GENERAL_ERROR通常發生在程式找不到SoftHSM配置文件或.NET應用程式以32位進程運行而SoftHSM程式庫為64位時。

更改平台目標
導致此錯誤的常見原因是架構不符。 您的C#應用程式必須以64位進程運行,以匹配64位SoftHSM程式庫(CHOOSE_x64)。 在您的Visual Studio專案屬性中,將平台目標從'Any CPU'或'x86'更改為x64以確保兼容性。

設置環境變數
另一個常見原因是程式找不到SoftHSM的.conf文件。 您必須透過設定係統範圍的環境變數來告訴庫在哪裡尋找。 創建一個名為SOFTHSM2_CONF的新變量,並將其值設置為配置文件的完整路徑(例如,D:\SoftHSM2\etc\softhsm2.conf)。 切記在進行變更後重新啟動 Visual Studio。

此外,您可以加入這一行來驗證是否找到變數:
Console.WriteLine($"Verifying variable: {Environment.GetEnvironmentVariable("SOFTHSM2_CONF")}");Console.WriteLine($"Verifying variable: {Environment.GetEnvironmentVariable("SOFTHSM2_CONF")}");Console.WriteLine($"Verifying variable: {Environment.GetEnvironmentVariable(""SOFTHSM2_CONF"")}")Console.WriteLine($"Verifying variable: {Environment.GetEnvironmentVariable(""SOFTHSM2_CONF"")}")如果控制台輸出回傳空白,表示程式無法找到環境變數。 您必須進行設置,重新啟動 Visual Studio 或計算機,然後重試。
引發TimestampException的原因是什麼?
當無法在簽名期間獲取或嵌入時間戳時,會拋出TimestampException。 它是不同的類型,因此可以單獨捕獲時間戳失敗,並且報告清晰的消息,而不是低級緩衝區錯誤。 三種情況產生該錯誤:
- 無法連接到授權機構。 檢查端點URL和簽名主機上的任何傳出防火牆或代理規則。
- 授權機構返回空令牌。 請求被接受但沒有返回可用的內容。 嘗試不同的TSA。
- 令牌未適合保留的簽名空間。 某些授權機構返回非常大的證書鏈。 異常消息聲明實際大小和保留大小。
try
{
hsmSigner.TimeStampUrl = "https://freetsa.org/tsr";
pdf.SignAndSave("signed.pdf", hsmSigner);
}
catch (TimestampException ex)
{
// TSA unreachable, empty token, or token too large for the reserved space
Console.WriteLine(ex.Message);
}try
{
hsmSigner.TimeStampUrl = "https://freetsa.org/tsr";
pdf.SignAndSave("signed.pdf", hsmSigner);
}
catch (TimestampException ex)
{
// TSA unreachable, empty token, or token too large for the reserved space
Console.WriteLine(ex.Message);
}Try
hsmSigner.TimeStampUrl = "https://freetsa.org/tsr"
pdf.SignAndSave("signed.pdf", hsmSigner)
Catch ex As TimestampException
' TSA unreachable, empty token, or token too large for the reserved space
Console.WriteLine(ex.Message)
End TryTry
hsmSigner.TimeStampUrl = "https://freetsa.org/tsr"
pdf.SignAndSave("signed.pdf", hsmSigner)
Catch ex As TimestampException
' TSA unreachable, empty token, or token too large for the reserved space
Console.WriteLine(ex.Message)
End Try由於異常是在簽名構建期間引發的,因此發生時不會寫入輸出文件。
生產HSM部署的建議做法
在生產環境中部署 HSM 簽署的 PDF 時,請考慮這些額外的安全措施:
- 審計記錄: 實施全面的記錄記錄所有HSM操作以維持合規性並跟蹤訪問
- 證書管理: 根據您組織的安全政策定期更新和旋轉證書
- 備份程序: 建立正確的備份和恢復程序以保護HSM配置
- 性能優化: 監控簽名性能並對經常訪問的證書實施緩存策略
這些做法補充標準的PDF簽名工作流程,確保您的文件安全基礎設施保持可靠並符合行業標準。
常見問題
在C#中使用HSM簽署PDF的主要好處是什麼?
使用HSM(硬體安全模組)簽署PDF提供銀行級別的安全性,因為私鑰永不離開物理裝置,從而確保防篡改的數位簽章。IronPDF透過使用PKCS#11 API實現這種安全方法。
如何開始在C#中使用HSM簽署PDF?
要開始在C#中使用HSM簽署PDF,首先透過NuGet安裝IronPDF,配置您的HSM裝置(或使用SoftHSM進行模擬),然後使用帶憑證的UsbPkcs11HsmSigner物件來簽署並儲存PDF。
PKCS#11 API在PDF簽署中的重要性是什麼?
PKCS#11 API是一個標準化介面,允許IronPDF安全地與HSM進行數位簽署操作,確保在整個過程中私鑰保持在硬體設備內受到保護。
為什麼要在PDF簽署示範中使用SoftHSM?
SoftHSM在PDF簽署示範中用於模擬硬體安全模組,讓您在不需要實體HSM裝置的情況下安全地測試和開發。
如何在使用IronPDF和HSM簽署後驗證我的PDF?
使用HSM透過IronPDF簽署PDF後,您可以在PDF檢視器中打開已簽署的文件來驗證簽名。此步驟確保簽名的完整性和真實性。
如何驗證我的 PDF 已成功使用 HSM 簽署?
使用 IronPDF 的 HSM 功能簽署 PDF 之後,您可以在任何標準的 PDF 檢視器中開啟已簽署的 PDF 來驗證簽章。查看器將顯示數位簽章資訊,並確認文件的真實性和完整性。
如何向HSM簽署的PDF添加可信時間戳?
在調用SignAndSave之前,將TimeStampUrl屬性設置為RFC 3161時間戳認證機構的端點。端點作為字串提供,生成的簽章攜帶讀者應用程式(例如Adobe Acrobat)識別的時間戳令牌。將屬性設為null或空值將完全跳過時間戳且不進行任何網路調用。
HSM時間戳使用哪種雜湊演算法?
TimestampHashAlgorithm選擇從時間戳認證機構請求的摘要,接受SHA1, SHA256或SHA512。內建的UsbPkcs11HsmSigner通過其AHsmSigner基類預設使用SHA256。直接實現IHsmSigner而不從AHsmSigner繼承的自定義簽名器將接收未初始化的enum值,這是SHA1,因此應明確設置該屬性。請注意,這與控制簽章本身而不是時間戳的digestAlgorithm構造函數參數是分開的。
HSM簽名時引發TimestampException的原因是什麼?
當無法獲得或嵌入時間戳時,會引發TimestampException。三個原因是:無法訪問時間戳認可機構、認可機構返回空令牌以及帶時間戳的簽名不符合保留的簽名空間,這可能發生在TSA返回非常大的證書鏈時。例外訊息會指出實際大小對比保留大小。
How do I sign a PDF with a key stored in Azure Key Vault in C#?
Implement IExternalSignatureProvider so that its SignHash method calls Azure Key Vault, pass the provider to an ExternalSigner, and sign with SignAndSave. Only the document hash is sent to the vault, so the private key never leaves it.
Can IronPDF return a signed PDF as a byte array instead of saving it to disk?
Yes. SignToBytes signs the PDF with an HSM or external signer and returns the signed document as a byte array, for example to return it from a web API. Store or send the bytes unchanged, because re-saving them through another PdfDocument can invalidate the signature.
Which keys and hash algorithms does ExternalSigner support?
RSA and ECDSA keys, with SHA256, SHA384 or SHA512. Services that tie the hash to the key size need SHA384 for P-384 keys and SHA512 for P-521 keys, as Azure Key Vault ES384 and ES512 do. Other key types or hash algorithms throw NotSupportedException.
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。