@libpdf/core 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -10508,6 +10508,8 @@ declare class GoogleKmsSigner implements Signer {
10508
10508
  /**
10509
10509
  * Hash data using the specified algorithm.
10510
10510
  *
10511
+ * Uses the Web Crypto API for native-speed hashing.
10512
+ *
10511
10513
  * @returns The digest bytes and the KMS digest key name
10512
10514
  */
10513
10515
  private hashData;
package/dist/index.mjs CHANGED
@@ -11,7 +11,7 @@ import { createCMSECDSASignature } from "pkijs";
11
11
  import { base64 } from "@scure/base";
12
12
 
13
13
  //#region package.json
14
- var version = "0.3.0";
14
+ var version = "0.3.2";
15
15
 
16
16
  //#endregion
17
17
  //#region src/objects/pdf-array.ts
@@ -34186,19 +34186,21 @@ function encryptStreamDict(stream, ctx) {
34186
34186
  */
34187
34187
  function collectReachableRefs(registry, root, info, encrypt) {
34188
34188
  const visited = /* @__PURE__ */ new Set();
34189
- const walk = (obj) => {
34190
- if (obj === null) return;
34189
+ const stack = [root];
34190
+ if (info) stack.push(info);
34191
+ if (encrypt) stack.push(encrypt);
34192
+ while (stack.length > 0) {
34193
+ const obj = stack.pop();
34191
34194
  if (obj instanceof PdfRef) {
34192
34195
  const key$1 = `${obj.objectNumber} ${obj.generation}`;
34193
- if (visited.has(key$1)) return;
34196
+ if (visited.has(key$1)) continue;
34194
34197
  visited.add(key$1);
34195
- walk(registry.resolve(obj));
34196
- } else if (obj instanceof PdfDict) for (const [, value] of obj) walk(value);
34197
- else if (obj instanceof PdfArray) for (const item of obj) walk(item);
34198
- };
34199
- walk(root);
34200
- if (info) walk(info);
34201
- if (encrypt) walk(encrypt);
34198
+ const resolved = registry.resolve(obj);
34199
+ if (resolved !== null) stack.push(resolved);
34200
+ } else if (obj instanceof PdfDict) {
34201
+ for (const [, value] of obj) if (value != null) stack.push(value);
34202
+ } else if (obj instanceof PdfArray) for (const item of obj) stack.push(item);
34203
+ }
34202
34204
  return visited;
34203
34205
  }
34204
34206
  /**
@@ -38222,9 +38224,6 @@ const OID_AD_CA_ISSUERS = "1.3.6.1.5.5.7.48.2";
38222
38224
  //#endregion
38223
38225
  //#region src/signatures/utils.ts
38224
38226
  /**
38225
- * Shared utilities for signature operations.
38226
- */
38227
- /**
38228
38227
  * Escape special characters in PDF literal string.
38229
38228
  *
38230
38229
  * PDF strings use backslash escapes for special characters.
@@ -38238,16 +38237,17 @@ function escapePdfString(str) {
38238
38237
  /**
38239
38238
  * Hash data using the specified algorithm.
38240
38239
  *
38240
+ * Uses the Web Crypto API for native-speed hashing, which is significantly
38241
+ * faster than pure-JS implementations for large inputs (e.g. hashing an
38242
+ * entire PDF during signing).
38243
+ *
38241
38244
  * @param data - Data to hash
38242
38245
  * @param algorithm - Digest algorithm
38243
38246
  * @returns Hash bytes
38244
38247
  */
38245
- function hashData(data, algorithm) {
38246
- switch (algorithm) {
38247
- case "SHA-256": return sha256(data);
38248
- case "SHA-384": return sha384(data);
38249
- case "SHA-512": return sha512(data);
38250
- }
38248
+ async function hashData(data, algorithm) {
38249
+ const digest = await crypto.subtle.digest(algorithm, data);
38250
+ return new Uint8Array(digest);
38251
38251
  }
38252
38252
 
38253
38253
  //#endregion
@@ -38363,7 +38363,7 @@ var CAdESDetachedBuilder = class {
38363
38363
  const { signer, documentHash, digestAlgorithm, signingTime } = options;
38364
38364
  const signerCert = parseCertificate$1(signer.certificate);
38365
38365
  const allCerts = [signerCert, ...(signer.certificateChain ?? []).map(parseCertificate$1)];
38366
- const signedAttrs = this.buildSignedAttributes(documentHash, digestAlgorithm, signer, signerCert, signingTime);
38366
+ const signedAttrs = await this.buildSignedAttributes(documentHash, digestAlgorithm, signer, signerCert, signingTime);
38367
38367
  const signedAttrsForSigning = encodeSignedAttributesForSigning(signedAttrs);
38368
38368
  this.signatureValue = await signer.sign(new Uint8Array(signedAttrsForSigning), digestAlgorithm);
38369
38369
  this.signerInfo = new pkijs.SignerInfo({
@@ -38414,7 +38414,7 @@ var CAdESDetachedBuilder = class {
38414
38414
  /**
38415
38415
  * Build the signed attributes for CAdES signature.
38416
38416
  */
38417
- buildSignedAttributes(documentHash, digestAlgorithm, signer, signerCert, signingTime) {
38417
+ async buildSignedAttributes(documentHash, digestAlgorithm, signer, signerCert, signingTime) {
38418
38418
  const attrs = [];
38419
38419
  attrs.push(new pkijs.Attribute({
38420
38420
  type: OID_CONTENT_TYPE,
@@ -38429,7 +38429,7 @@ var CAdESDetachedBuilder = class {
38429
38429
  type: OID_MESSAGE_DIGEST,
38430
38430
  values: [new OctetString({ valueHex: toArrayBuffer(documentHash) })]
38431
38431
  }));
38432
- attrs.push(this.buildSigningCertificateV2(signerCert, digestAlgorithm));
38432
+ attrs.push(await this.buildSigningCertificateV2(signerCert, digestAlgorithm));
38433
38433
  return attrs;
38434
38434
  }
38435
38435
  /**
@@ -38450,9 +38450,9 @@ var CAdESDetachedBuilder = class {
38450
38450
  * issuerSerial IssuerSerial OPTIONAL
38451
38451
  * }
38452
38452
  */
38453
- buildSigningCertificateV2(signerCert, digestAlgorithm) {
38453
+ async buildSigningCertificateV2(signerCert, digestAlgorithm) {
38454
38454
  const certDer = signerCert.toSchema().toBER(false);
38455
- const certHash = hashData(new Uint8Array(certDer), digestAlgorithm);
38455
+ const certHash = await hashData(new Uint8Array(certDer), digestAlgorithm);
38456
38456
  const generalName = new pkijs.GeneralName({
38457
38457
  type: 4,
38458
38458
  value: signerCert.issuer
@@ -41609,7 +41609,7 @@ var PDFSignature = class {
41609
41609
  const placeholders = findPlaceholders(pdfBytes);
41610
41610
  const byteRange = calculateByteRange(pdfBytes, placeholders);
41611
41611
  patchByteRange(pdfBytes, placeholders, byteRange);
41612
- const documentHash = hashData(extractSignedBytes(pdfBytes, byteRange), resolved.digestAlgorithm);
41612
+ const documentHash = await hashData(extractSignedBytes(pdfBytes, byteRange), resolved.digestAlgorithm);
41613
41613
  const signedData = await this.getFormatBuilder(resolved.subFilter).create({
41614
41614
  signer: resolved.signer,
41615
41615
  documentHash,
@@ -41617,7 +41617,7 @@ var PDFSignature = class {
41617
41617
  signingTime: resolved.signingTime
41618
41618
  });
41619
41619
  if (resolved.timestampAuthority) {
41620
- const signatureHash = hashData(signedData.getSignatureValue(), resolved.digestAlgorithm);
41620
+ const signatureHash = await hashData(signedData.getSignatureValue(), resolved.digestAlgorithm);
41621
41621
  const timestampToken = await resolved.timestampAuthority.timestamp(signatureHash, resolved.digestAlgorithm);
41622
41622
  signedData.addTimestampToken(timestampToken);
41623
41623
  }
@@ -41769,7 +41769,7 @@ var PDFSignature = class {
41769
41769
  const placeholders = findPlaceholders(savedBytes);
41770
41770
  const byteRange = calculateByteRange(savedBytes, placeholders);
41771
41771
  patchByteRange(savedBytes, placeholders, byteRange);
41772
- const documentHash = hashData(extractSignedBytes(savedBytes, byteRange), digestAlgorithm);
41772
+ const documentHash = await hashData(extractSignedBytes(savedBytes, byteRange), digestAlgorithm);
41773
41773
  const timestampToken = await timestampAuthority.timestamp(documentHash, digestAlgorithm);
41774
41774
  patchContents(savedBytes, placeholders, timestampToken);
41775
41775
  await this.pdf.reload(savedBytes);
@@ -44531,7 +44531,7 @@ var GoogleKmsSigner = class GoogleKmsSigner {
44531
44531
  */
44532
44532
  async sign(data, algorithm) {
44533
44533
  if (algorithm !== this.digestAlgorithm) throw new KmsSignerError(`Digest algorithm mismatch: this KMS key requires ${this.digestAlgorithm}, but ${algorithm} was requested`);
44534
- const { digest, digestKey } = this.hashData(data, algorithm);
44534
+ const { digest, digestKey } = await this.hashData(data, algorithm);
44535
44535
  try {
44536
44536
  const [response] = await this.client.asymmetricSign({
44537
44537
  name: this.keyVersionName,
@@ -44549,23 +44549,21 @@ var GoogleKmsSigner = class GoogleKmsSigner {
44549
44549
  /**
44550
44550
  * Hash data using the specified algorithm.
44551
44551
  *
44552
+ * Uses the Web Crypto API for native-speed hashing.
44553
+ *
44552
44554
  * @returns The digest bytes and the KMS digest key name
44553
44555
  */
44554
- hashData(data, algorithm) {
44555
- switch (algorithm) {
44556
- case "SHA-256": return {
44557
- digest: sha256(data),
44558
- digestKey: "sha256"
44559
- };
44560
- case "SHA-384": return {
44561
- digest: sha384(data),
44562
- digestKey: "sha384"
44563
- };
44564
- case "SHA-512": return {
44565
- digest: sha512(data),
44566
- digestKey: "sha512"
44567
- };
44568
- }
44556
+ async hashData(data, algorithm) {
44557
+ const digestKeyMap = {
44558
+ "SHA-256": "sha256",
44559
+ "SHA-384": "sha384",
44560
+ "SHA-512": "sha512"
44561
+ };
44562
+ const arrayBuffer = await crypto.subtle.digest(algorithm, data);
44563
+ return {
44564
+ digest: new Uint8Array(arrayBuffer),
44565
+ digestKey: digestKeyMap[algorithm]
44566
+ };
44569
44567
  }
44570
44568
  };
44571
44569