@libpdf/core 0.3.0 → 0.3.1

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.1";
15
15
 
16
16
  //#endregion
17
17
  //#region src/objects/pdf-array.ts
@@ -38222,9 +38222,6 @@ const OID_AD_CA_ISSUERS = "1.3.6.1.5.5.7.48.2";
38222
38222
  //#endregion
38223
38223
  //#region src/signatures/utils.ts
38224
38224
  /**
38225
- * Shared utilities for signature operations.
38226
- */
38227
- /**
38228
38225
  * Escape special characters in PDF literal string.
38229
38226
  *
38230
38227
  * PDF strings use backslash escapes for special characters.
@@ -38238,16 +38235,17 @@ function escapePdfString(str) {
38238
38235
  /**
38239
38236
  * Hash data using the specified algorithm.
38240
38237
  *
38238
+ * Uses the Web Crypto API for native-speed hashing, which is significantly
38239
+ * faster than pure-JS implementations for large inputs (e.g. hashing an
38240
+ * entire PDF during signing).
38241
+ *
38241
38242
  * @param data - Data to hash
38242
38243
  * @param algorithm - Digest algorithm
38243
38244
  * @returns Hash bytes
38244
38245
  */
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
- }
38246
+ async function hashData(data, algorithm) {
38247
+ const digest = await crypto.subtle.digest(algorithm, data);
38248
+ return new Uint8Array(digest);
38251
38249
  }
38252
38250
 
38253
38251
  //#endregion
@@ -38363,7 +38361,7 @@ var CAdESDetachedBuilder = class {
38363
38361
  const { signer, documentHash, digestAlgorithm, signingTime } = options;
38364
38362
  const signerCert = parseCertificate$1(signer.certificate);
38365
38363
  const allCerts = [signerCert, ...(signer.certificateChain ?? []).map(parseCertificate$1)];
38366
- const signedAttrs = this.buildSignedAttributes(documentHash, digestAlgorithm, signer, signerCert, signingTime);
38364
+ const signedAttrs = await this.buildSignedAttributes(documentHash, digestAlgorithm, signer, signerCert, signingTime);
38367
38365
  const signedAttrsForSigning = encodeSignedAttributesForSigning(signedAttrs);
38368
38366
  this.signatureValue = await signer.sign(new Uint8Array(signedAttrsForSigning), digestAlgorithm);
38369
38367
  this.signerInfo = new pkijs.SignerInfo({
@@ -38414,7 +38412,7 @@ var CAdESDetachedBuilder = class {
38414
38412
  /**
38415
38413
  * Build the signed attributes for CAdES signature.
38416
38414
  */
38417
- buildSignedAttributes(documentHash, digestAlgorithm, signer, signerCert, signingTime) {
38415
+ async buildSignedAttributes(documentHash, digestAlgorithm, signer, signerCert, signingTime) {
38418
38416
  const attrs = [];
38419
38417
  attrs.push(new pkijs.Attribute({
38420
38418
  type: OID_CONTENT_TYPE,
@@ -38429,7 +38427,7 @@ var CAdESDetachedBuilder = class {
38429
38427
  type: OID_MESSAGE_DIGEST,
38430
38428
  values: [new OctetString({ valueHex: toArrayBuffer(documentHash) })]
38431
38429
  }));
38432
- attrs.push(this.buildSigningCertificateV2(signerCert, digestAlgorithm));
38430
+ attrs.push(await this.buildSigningCertificateV2(signerCert, digestAlgorithm));
38433
38431
  return attrs;
38434
38432
  }
38435
38433
  /**
@@ -38450,9 +38448,9 @@ var CAdESDetachedBuilder = class {
38450
38448
  * issuerSerial IssuerSerial OPTIONAL
38451
38449
  * }
38452
38450
  */
38453
- buildSigningCertificateV2(signerCert, digestAlgorithm) {
38451
+ async buildSigningCertificateV2(signerCert, digestAlgorithm) {
38454
38452
  const certDer = signerCert.toSchema().toBER(false);
38455
- const certHash = hashData(new Uint8Array(certDer), digestAlgorithm);
38453
+ const certHash = await hashData(new Uint8Array(certDer), digestAlgorithm);
38456
38454
  const generalName = new pkijs.GeneralName({
38457
38455
  type: 4,
38458
38456
  value: signerCert.issuer
@@ -41609,7 +41607,7 @@ var PDFSignature = class {
41609
41607
  const placeholders = findPlaceholders(pdfBytes);
41610
41608
  const byteRange = calculateByteRange(pdfBytes, placeholders);
41611
41609
  patchByteRange(pdfBytes, placeholders, byteRange);
41612
- const documentHash = hashData(extractSignedBytes(pdfBytes, byteRange), resolved.digestAlgorithm);
41610
+ const documentHash = await hashData(extractSignedBytes(pdfBytes, byteRange), resolved.digestAlgorithm);
41613
41611
  const signedData = await this.getFormatBuilder(resolved.subFilter).create({
41614
41612
  signer: resolved.signer,
41615
41613
  documentHash,
@@ -41617,7 +41615,7 @@ var PDFSignature = class {
41617
41615
  signingTime: resolved.signingTime
41618
41616
  });
41619
41617
  if (resolved.timestampAuthority) {
41620
- const signatureHash = hashData(signedData.getSignatureValue(), resolved.digestAlgorithm);
41618
+ const signatureHash = await hashData(signedData.getSignatureValue(), resolved.digestAlgorithm);
41621
41619
  const timestampToken = await resolved.timestampAuthority.timestamp(signatureHash, resolved.digestAlgorithm);
41622
41620
  signedData.addTimestampToken(timestampToken);
41623
41621
  }
@@ -41769,7 +41767,7 @@ var PDFSignature = class {
41769
41767
  const placeholders = findPlaceholders(savedBytes);
41770
41768
  const byteRange = calculateByteRange(savedBytes, placeholders);
41771
41769
  patchByteRange(savedBytes, placeholders, byteRange);
41772
- const documentHash = hashData(extractSignedBytes(savedBytes, byteRange), digestAlgorithm);
41770
+ const documentHash = await hashData(extractSignedBytes(savedBytes, byteRange), digestAlgorithm);
41773
41771
  const timestampToken = await timestampAuthority.timestamp(documentHash, digestAlgorithm);
41774
41772
  patchContents(savedBytes, placeholders, timestampToken);
41775
41773
  await this.pdf.reload(savedBytes);
@@ -44531,7 +44529,7 @@ var GoogleKmsSigner = class GoogleKmsSigner {
44531
44529
  */
44532
44530
  async sign(data, algorithm) {
44533
44531
  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);
44532
+ const { digest, digestKey } = await this.hashData(data, algorithm);
44535
44533
  try {
44536
44534
  const [response] = await this.client.asymmetricSign({
44537
44535
  name: this.keyVersionName,
@@ -44549,23 +44547,21 @@ var GoogleKmsSigner = class GoogleKmsSigner {
44549
44547
  /**
44550
44548
  * Hash data using the specified algorithm.
44551
44549
  *
44550
+ * Uses the Web Crypto API for native-speed hashing.
44551
+ *
44552
44552
  * @returns The digest bytes and the KMS digest key name
44553
44553
  */
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
- }
44554
+ async hashData(data, algorithm) {
44555
+ const digestKeyMap = {
44556
+ "SHA-256": "sha256",
44557
+ "SHA-384": "sha384",
44558
+ "SHA-512": "sha512"
44559
+ };
44560
+ const arrayBuffer = await crypto.subtle.digest(algorithm, data);
44561
+ return {
44562
+ digest: new Uint8Array(arrayBuffer),
44563
+ digestKey: digestKeyMap[algorithm]
44564
+ };
44569
44565
  }
44570
44566
  };
44571
44567