Sign PDF with Plain JavaScript
Challenge:
Digitally signing a PDF document using the WebCrypto API poses a challenge, as the current API doesn't directly allow access to key storage or local crypto devices.
Solution:
To circumvent this limitation, it's recommended to:
-
Create a Hash of the PDF: Generate a hash of the PDF file instead of sending the entire document to the browser.
-
Utilize a Browser Extension: Install a browser extension such as Signer.Digital to access local keystores (e.g., Windows Certificate store or PKCS#11 on Linux).
-
Sign the Hash: Use the extension's API to sign the hashed PDF using the private key obtained from the local keystore.
-
Receive the Signature: The browser extension will return the signature in a suitable format (e.g., PKCS7).
-
Inject Signature into PDF: On the server side, use an appropriate library (or the one provided by Signer.Digital) to inject the signature back into the original PDF file.
Signer.Digital Extension:
The Signer.Digital browser extension provides the necessary functionality to perform the signing process as follows:
-
Function: SignerDigital.signPdfHash(hash, certThumbPrint, hashingAlgo)
-
Parameters:
- hash: Hash of the PDF in Base64-encoded string
- certThumbPrint: Thumbprint of the certificate to use for signing
- hashingAlgo: Algorithm used to hash the PDF (e.g., "SHA256")
Example:
// Assuming hash is a Base64-encoded PDF hash
SignerDigital.signPdfHash(hash, $("#CertThumbPrint").val(), "SHA-256")
.then(
function (signDataResp) {
// Send signDataResp (contains Base64 PKCS7 signature) to server
},
function (errmsg) {
// Handle error
}
);
Note:
- The private key is stored locally in the certificate settings of the browser.
- In the future, with the advancement of WebCrypto API, direct access to key storage might become possible, eliminating the need for browser extensions.
The above is the detailed content of How Can I Digitally Sign a PDF Using JavaScript Without Direct Key Access?. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn