Created
December 14, 2024 02:49
-
-
Save diegofcornejo/a941418db72418f78238e4aadd9b7247 to your computer and use it in GitHub Desktop.
Verify PDF Signature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const verifyPDF = require('pdf-signature-reader-jpc'); | |
const fs = require('fs'); | |
const path = require('path'); | |
// Define paths | |
const filesPath = 'files'; | |
const resultsPathPrefix = 'results'; | |
const timestamp = new Date().toISOString().replace(/[:]/g, '-').split('.')[0]; | |
const resultsPath = path.join(resultsPathPrefix, timestamp); | |
const verifiedPath = path.join(resultsPath, 'verified'); | |
const failedPath = path.join(resultsPath, 'failed'); | |
const createDirectory = (dirPath) => { | |
if (!fs.existsSync(dirPath)) { | |
fs.mkdirSync(dirPath, { recursive: true }); | |
} | |
}; | |
createDirectory(resultsPath); | |
createDirectory(verifiedPath); | |
createDirectory(failedPath); | |
const copyFile = (file, destinationPath) => { | |
const source = path.join(filesPath, file); | |
const destination = path.join(destinationPath, file); | |
fs.copyFileSync(source, destination); | |
console.log(destinationPath === verifiedPath | |
? '🟢 File moved to verified directory' | |
: '🔴 File moved to failed directory'); | |
}; | |
const files = fs.readdirSync(filesPath); | |
for (const file of files) { | |
console.log('=============================================='); | |
console.log('Document:', file); | |
try { | |
const signedPdfBuffer = fs.readFileSync(path.join(filesPath, file)); | |
const { | |
verified, | |
authenticity, | |
integrity, | |
expired, | |
} = verifyPDF(signedPdfBuffer); | |
console.log('verified:', verified); | |
console.log('authenticity:', authenticity); | |
console.log('integrity:', integrity); | |
console.log('expired:', expired); | |
if (verified && authenticity && integrity && !expired) { | |
copyFile(file, verifiedPath); | |
} else { | |
copyFile(file, failedPath); | |
} | |
} catch (error) { | |
console.error('Error processing file:', file, error.message); | |
copyFile(file, failedPath); | |
} | |
console.log('=============================================='); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment