Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecated extractSignature as it was never public-ready. #208

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
71 changes: 71 additions & 0 deletions packages/internal-utils/extractSignature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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;
};

/**
* @typedef {Object} ExtractSignatureResult
* @property {number[]} ByteRange
* @property {Buffer} signature
* @property {Buffer} signedData
*/

/**
* 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 {ExtractSignatureResult}
*/
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
11 changes: 10 additions & 1 deletion packages/utils/dist/extractSignature.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
export function extractSignature(pdf: Buffer, signatureCount?: number): any;
/**
* @type {(pdf: Buffer, signatureCount: number) => ExtractSignatureResult}
* @deprecated Should be used from internal-utils. Will be removed in a major release.
*/
export const extractSignature: (pdf: Buffer, signatureCount: number) => ExtractSignatureResult;
export type ExtractSignatureResult = {
ByteRange: number[];
signature: Buffer;
signedData: Buffer;
};
//# sourceMappingURL=extractSignature.d.ts.map
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.

27 changes: 22 additions & 5 deletions packages/utils/dist/extractSignature.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports.extractSignature = void 0;
var _util = _interopRequireDefault(require("util"));
var _SignPdfError = require("./SignPdfError");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const getSubstringIndex = (str, substring, n) => {
let times = 0;
let index = null;
Expand All @@ -14,16 +16,26 @@ const getSubstringIndex = (str, substring, n) => {
}
return index;
};

/**
* @typedef {Object} ExtractSignatureResult
* @property {number[]} ByteRange
* @property {Buffer} signature
* @property {Buffer} signedData
*/

/**
* 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
* @returns {Object} {ByteRange: Number[], signature: Buffer, signedData: Buffer}
* @param {number} signatureCount
* @returns {ExtractSignatureResult}
*
* @deprecated Should be used internally from internal-utils. Will be removed in a major release.
*/
const extractSignature = (pdf, signatureCount = 1) => {
const extractSignatureDeprecated = (pdf, signatureCount = 1) => {
if (!(pdf instanceof Buffer)) {
throw new _SignPdfError.SignPdfError('PDF expected as Buffer.', _SignPdfError.SignPdfError.TYPE_INPUT);
}
Expand Down Expand Up @@ -52,4 +64,9 @@ const extractSignature = (pdf, signatureCount = 1) => {
signedData
};
};
exports.extractSignature = extractSignature;

/**
* @type {(pdf: Buffer, signatureCount: number) => ExtractSignatureResult}
* @deprecated Should be used from internal-utils. Will be removed in a major release.
*/
const extractSignature = exports.extractSignature = _util.default.deprecate(extractSignatureDeprecated, 'Should be used internally from internal-utils. Will be removed in a major release.');
Binary file modified packages/utils/src/__snapshots__/extractSignature.test.js.snap
Binary file not shown.
25 changes: 21 additions & 4 deletions packages/utils/src/extractSignature.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import nodeUtil from 'util';
import {SignPdfError} from './SignPdfError';

const getSubstringIndex = (str, substring, n) => {
Expand All @@ -11,16 +12,26 @@ const getSubstringIndex = (str, substring, n) => {

return index;
};

/**
* @typedef {Object} ExtractSignatureResult
* @property {number[]} ByteRange
* @property {Buffer} signature
* @property {Buffer} signedData
*/

/**
* 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
* @returns {Object} {ByteRange: Number[], signature: Buffer, signedData: Buffer}
* @param {number} signatureCount
* @returns {ExtractSignatureResult}
*
* @deprecated Should be used internally from internal-utils. Will be removed in a major release.
*/
export const extractSignature = (pdf, signatureCount = 1) => {
const extractSignatureDeprecated = (pdf, signatureCount = 1) => {
if (!(pdf instanceof Buffer)) {
throw new SignPdfError(
'PDF expected as Buffer.',
Expand Down Expand Up @@ -72,3 +83,9 @@ export const extractSignature = (pdf, signatureCount = 1) => {
signedData,
};
};

/**
* @type {(pdf: Buffer, signatureCount: number) => ExtractSignatureResult}
* @deprecated Should be used from internal-utils. Will be removed in a major release.
*/
export const extractSignature = nodeUtil.deprecate(extractSignatureDeprecated, 'Should be used internally from internal-utils. Will be removed in a major release.');
Loading