Skip to content
Snippets Groups Projects
index.ts 879 B
Newer Older
  • Learn to ignore specific revisions
  • Zdravko Iliev's avatar
    Zdravko Iliev committed
    import { checkForSubFilter, preparePDF } from "./generalUtils";
    import { extractSignature } from "./signatureUtils";
    import { verify } from "./verify";
    
    export const verifyPDF = (pdf: Buffer) => {
      const pdfBuffer = preparePDF(pdf);
    
      try {
        checkForSubFilter(pdfBuffer);
      } catch (error) {
    
        //SubFilter is only available if there is a sig in the pdf
        return null;
    
    Zdravko Iliev's avatar
    Zdravko Iliev committed
      try {
    
    Zdravko Iliev's avatar
    Zdravko Iliev committed
        const { signatureStr, signedData, signatureMeta } =
          extractSignature(pdfBuffer);
    
    Zdravko Iliev's avatar
    Zdravko Iliev committed
    
        const signatures: any = signedData.map((_signed, index) => {
          return verify(signatureStr[index], signatureMeta[index]);
        });
    
        return {
          // authenticity: signatures.every((o) => o.authenticity === true),
          expired: signatures.some((o) => o.expired === true),
          signatures,
        };
      } catch (error) {
        return { verified: false, message: error.message, error };
      }
    };