Giter Club home page Giter Club logo

node-signpdf's Introduction

node-signpdf

npm version Build Status Coverage Status Known Vulnerabilities

Simple signing of PDFs in node.

Purpose

The purpose of this package is not as much to be used as a dependendency, although it could. The main purpose is to demonstrate the way signing can be achieved in a piece of readable code as it can take a lot of hours to figure out.

Usage

Simply said this could be used in two steps. install and sign.

Install with npm i -S node-signpdf node-forge.

Signing

Call .sign()

import signer from 'node-signpdf';

const signedPdf = signer.sign(
  fs.readFileSync(PATH_TO_PDF_FILE)
  fs.readFileSync(PATH_TO_P12_CERTIFICATE),
);

In practice we expect that most people will just read through the code we've written in the testing part of this package and figure it out themselves. If that's your case, you should read the [Signing PDF in simple steps] section.

Verifying

To verify a signed pdf call .verify().

import signer from 'node-signpdf';
...

const signedPdfBuffer = signer.sign(pdfBuffer, p12Buffer);
const {verified} = signer.verify(signedPdfBuffer);

Notes

  • The process of signing a document is described in the Digital Signatures in PDF document. As Adobe's files are deprecated, here is the standard as defined by ETSI.
  • This lib:
    • requires the signature placeholder to already be in the document;
    • requires the Contents descriptor in the Sig be placed after the ByteRange one;
    • takes Buffers of the PDF and a P12 certificate to use when signing;
    • does not cover multiple signatures, incremental updates, etc. Only the basic scenario of signing a freshly created PDF. We actually only worked with documents created with PDFKit;
  • Feel free to copy and paste any part of this code. See its defined Purpose.

Signing PDF in simple steps

Generate a PDF

See the unit-testing code. PDFKit is used there for generating the document. This also allows easy addition of the signature placeholder.

Append a signature placeholder

What's needed is a Sig element and a Widget that is also linked in a Form. The form needs to be referenced in the root descriptor of the PDF as well. A (hopefully) readable sample is available in the helpers. Note the Contents descriptor of the Sig where zeros are placed that will later be replaced with the actual signature.

Note: Signing in detached mode makes the signature length independent of the PDF's content length, but it may still vary between different signing certificates. So every time you sign using the same P12 you will get the same length of the output signature, no matter the length of the signed content. It is safe to find out the actual signature length your certificate produces and use it to properly configure the placeholder length.

Generate and apply signature

That's where node-signpdf kicks in. Given a PDF and a P12 certificate a signature is generated in detached mode and is replaced in the placeholder. This is best demonstrated in the tests.

Verifying PDF signature

The signed PDF file has the public certificate embeded in it, so all we need to verify a PDF file is the file itself.

Dependencies

node-forge is used for working with signatures.

PDFKit is used in the tests for generating a PDF with a signature placeholder.

Credits

node-signpdf's People

Contributors

vbuch avatar marin-aldimirov avatar mohammedessehemy avatar maldimirov avatar neuromancer64 avatar alekssakovsky avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.