Sign PDF with plain JavaScript

前端 未结 4 1129
悲&欢浪女
悲&欢浪女 2020-11-22 04:41

With WebCrypto API evolving and being supported by Chrome and Firefox, I would like to use it for digitally signing a PDF document. There is not much of literature around, b

相关标签:
4条回答
  • 2020-11-22 05:30

    It is technically possible to do this, in-fact it is one of the scenarios we had in mind when we made PKIjs (which is why there is this sample) - https://pkijs.org/examples/PDFexample.html

    That said to do signing requires working with the PDF structure itself, which either requires a custom parser or modifications to an existing one (pdfjs for example).

    Long story short, signing a PDF in browser will take a lot of work, it is something we are working on though.

    0 讨论(0)
  • 2020-11-22 05:32

    Disclosure: I work for CISPL.

    As of now, WebCrypto API does not provide access to (Windows) or any other Key stores or local crypto USB/Smartcard device.

    Also in most of the signing scenarios, for requirement to protect pdf file within the server boundaries, its not recommended to send complete pdf file to browser or to signing API server.

    Thus, its good practice, to create hash of PDF for signing, send hash to browser and use javascript through browser extension to access some application running on local system to access local keystore (or USB/Smartcard) and produce the signature and send back (PKCS7 or CMS container in case of PDF signing) to server where the signature may be injected back to PDF from which hash was created for signing and was sent to browser or to signing api server.

    For browser based signing scenarios, my company provides one such free Browser extension Signer.Digital and .NET library required on server. Local system (host running behind the chrome browser on windows) may be downloaded from cNET Download site Installing this host and restarting Chrome will automatically add Signer.Digital Chrome Extension and/or Signer.Digital Firefox Extension

    The actual working of this extension is illustrated here along with complete code walk through and download link to working sample VS 2015 project source code.

    Javascript to call method from extension:

     //Calculate Sign for the Hash by Calling function from Extension SignerDigital
     SignerDigital.signPdfHash(hash, $("#CertThumbPrint").val(), "SHA-256")      //or "SHA256"
      .then(
             function (signDataResp) {
               //Send signDataResp to Server
         },
             function (errmsg) {
                 //Send errmsg to server or display the result in browser.
               }
      );
    

    If success, returns Base64 encoded pkcs7 signature - use suitable library or one provided by Signer.Digital to inject sign to pdf

    If Failed, returns error msg starting with "SDHost Error:"

    Digital Signing from Browser

    0 讨论(0)
  • 2020-11-22 05:36

    You can sign any file (including pdf) using openpgp.js

    https://openpgpjs.org/openpgpjs/doc/#create-and-verify-detached-signatures

    (scroll down to 'create-and-verify-detached-signatures')

    Read the file as a Uint8Array and sign it with your private key.

    0 讨论(0)
  • 2020-11-22 05:40

    There is PDFSign.js, a library that can sign a PDF file in the browser. It uses forge though for the signature. If PKI.js supports detached pkcs7 signatures, then it should be easy to replace forge.

    0 讨论(0)
提交回复
热议问题