Skip to content

Commit

Permalink
🗑️ Deprecated extractSignature as it was never public-ready.
Browse files Browse the repository at this point in the history
Copied it in internal-utils to be used in tests. Will remove from `utils` in a next major release (#206)
  • Loading branch information
vbuch committed Nov 22, 2023
1 parent 5855e24 commit 5ba4f4f
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## [next]

* [utils] Added SIG_FLAGS and ANNOTATION_FLAGS to improve readability;
* [utils] Reworked findByteRange to match in more cases where it was incompatible so far (it didn't allow optional spaces in the array).
* [utils] Reworked findByteRange to match in more cases where it was incompatible so far (it didn't allow optional spaces in the array);
* [utils] Deprecated extractSignature as it was never public-ready. Copied it in internal-utils to be used in tests. Will remove from `utils` in a next major release;
* [placeholder-pdfkit010] Uses SIG_FLAGS and ANNOTATION_FLAGS instead of magic numbers;
* [placeholder-pdfkit010] Allow passing in widgetRect to override the default [0, 0, 0, 0] one;
* [placeholder-plain] Allow passing in widgetRect to override the default [0, 0, 0, 0] one;
Expand Down
64 changes: 64 additions & 0 deletions packages/internal-utils/extractSignature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
function getSubstringIndex(str, substring, n) {
var times = 0;
var index = null;

while (times < n && index !== -1) {
index = str.indexOf(substring, index + 1);
times += 1;
}

return index;
};

/**
* Basic implementation of signature extraction.
*
* Really basic. Would work in the simplest of cases where there is only one signature
* in a document and ByteRange is only used once in it.
*
* @param {Buffer} pdf
* @param {number} signatureCount
* @returns {Object} {ByteRange: Number[], signature: Buffer, signedData: Buffer}
*/
function extractSignature (pdf, signatureCount) {
if (!(pdf instanceof Buffer)) {
throw new Error('PDF expected as Buffer.');
}

// const byteRangePos = pdf.indexOf('/ByteRange [');
var byteRangePos = getSubstringIndex(pdf, '/ByteRange [', signatureCount || 1);
if (byteRangePos === -1) {
throw new Error('Failed to locate ByteRange.');
}

var byteRangeEnd = pdf.indexOf(']', byteRangePos);
if (byteRangeEnd === -1) {
throw new Error('Failed to locate the end of the ByteRange.');
}

var byteRange = pdf.subarray(byteRangePos, byteRangeEnd + 1).toString();
var matches = (/\/ByteRange \[(\d+) +(\d+) +(\d+) +(\d+) *\]/).exec(byteRange);
if (matches === null) {
throw new Error('Failed to parse the ByteRange.');
}

var ByteRange = matches.slice(1).map(Number);
var signedData = Buffer.concat([
pdf.subarray(ByteRange[0], ByteRange[0] + ByteRange[1]),
pdf.subarray(ByteRange[2], ByteRange[2] + ByteRange[3]),
]);

var signatureHex = pdf.subarray(ByteRange[0] + ByteRange[1] + 1, ByteRange[2])
.toString('binary')
.replace(/(?:00|>)+$/, '');

var signature = Buffer.from(signatureHex, 'hex').toString('binary');

return {
ByteRange: matches.slice(1, 5).map(Number),
signature: signature,
signedData: signedData,
};
};

module.exports = extractSignature;
2 changes: 2 additions & 0 deletions packages/internal-utils/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var readTestResource = require('./readTestResource');
var createPdfkitDocument = require('./createPdfkitDocument');
var extractSignature = require('./extractSignature');

module.exports = {
readTestResource: readTestResource,
createPdfkitDocument: createPdfkitDocument,
extractSignature: extractSignature,
}
8 changes: 2 additions & 6 deletions packages/signpdf/src/signpdf.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import {pdfkitAddPlaceholder} from '@signpdf/placeholder-pdfkit010';
import {plainAddPlaceholder} from '@signpdf/placeholder-plain';
import {P12Signer} from '@signpdf/signer-p12';
import {
extractSignature,
Signer,
SignPdfError,
} from '@signpdf/utils';
import {readTestResource, createPdfkitDocument} from '@signpdf/internal-utils';
import {Signer, SignPdfError} from '@signpdf/utils';
import {readTestResource, createPdfkitDocument, extractSignature} from '@signpdf/internal-utils';
import signpdf from './signpdf';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/dist/extractSignature.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/utils/dist/extractSignature.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ const getSubstringIndex = (str, substring, n) => {
/**
* Basic implementation of signature extraction.
*
* Really basic. Would work in the simplest of cases where there is only one signature
* in a document and ByteRange is only used once in it.
* Really basic. Would work in the simplest of cases.
*
* @param {Buffer} pdf
* @param {number} signatureCount
* @returns {Object} {ByteRange: Number[], signature: Buffer, signedData: Buffer}
*
* @deprecated Internally use the version in internal-utils. Will be removed in a major release.
*/
const extractSignature = (pdf, signatureCount = 1) => {
if (!(pdf instanceof Buffer)) {
Expand Down
6 changes: 4 additions & 2 deletions packages/utils/src/extractSignature.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ const getSubstringIndex = (str, substring, n) => {
/**
* Basic implementation of signature extraction.
*
* Really basic. Would work in the simplest of cases where there is only one signature
* in a document and ByteRange is only used once in it.
* Really basic. Would work in the simplest of cases.
*
* @param {Buffer} pdf
* @param {number} signatureCount
* @returns {Object} {ByteRange: Number[], signature: Buffer, signedData: Buffer}
*
* @deprecated Internally use the version in internal-utils. Will be removed in a major release.
*/
export const extractSignature = (pdf, signatureCount = 1) => {
if (!(pdf instanceof Buffer)) {
Expand Down

0 comments on commit 5ba4f4f

Please sign in to comment.