-
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🗑️ 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 (#206)
- Loading branch information
Showing
7 changed files
with
79 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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; |
This file contains 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
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, | ||
} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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