@sphereon/ssi-sdk-ext.key-utils 0.36.1-next.11 → 0.36.1-next.115

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.cjs CHANGED
@@ -38,6 +38,7 @@ __export(index_exports, {
38
38
  Key: () => Key,
39
39
  SIG_KEY_ALGS: () => SIG_KEY_ALGS,
40
40
  asn1DerToRawPublicKey: () => asn1DerToRawPublicKey,
41
+ base64ToBase64Url: () => base64ToBase64Url,
41
42
  calculateJwkThumbprint: () => calculateJwkThumbprint,
42
43
  calculateJwkThumbprintForKey: () => calculateJwkThumbprintForKey,
43
44
  coseKeyToJwk: () => coseKeyToJwk,
@@ -52,8 +53,13 @@ __export(index_exports, {
52
53
  hexStringFromUint8Array: () => hexStringFromUint8Array,
53
54
  importProvidedOrGeneratedKey: () => importProvidedOrGeneratedKey,
54
55
  isAsn1Der: () => isAsn1Der,
56
+ isHash: () => isHash,
57
+ isHashString: () => isHashString,
55
58
  isRawCompressedPublicKey: () => isRawCompressedPublicKey,
59
+ isSameHash: () => isSameHash,
56
60
  jcsCanonicalize: () => jcsCanonicalize,
61
+ joseAlgorithmToDigest: () => joseAlgorithmToDigest,
62
+ joseSignatureAlgToWebCrypto: () => joseSignatureAlgToWebCrypto,
57
63
  joseToCoseCurve: () => joseToCoseCurve,
58
64
  joseToCoseKeyOperation: () => joseToCoseKeyOperation,
59
65
  joseToCoseKty: () => joseToCoseKty,
@@ -66,6 +72,7 @@ __export(index_exports, {
66
72
  keyTypeFromCryptographicSuite: () => keyTypeFromCryptographicSuite,
67
73
  logger: () => logger,
68
74
  minimalJwk: () => minimalJwk,
75
+ normalizeHashAlgorithm: () => normalizeHashAlgorithm,
69
76
  padLeft: () => padLeft,
70
77
  removeNulls: () => removeNulls,
71
78
  rsaJwkToRawHexKey: () => rsaJwkToRawHexKey,
@@ -73,6 +80,7 @@ __export(index_exports, {
73
80
  shaHasher: () => shaHasher,
74
81
  signatureAlgorithmFromKey: () => signatureAlgorithmFromKey,
75
82
  signatureAlgorithmFromKeyType: () => signatureAlgorithmFromKeyType,
83
+ signatureAlgorithmToJoseAlgorithm: () => signatureAlgorithmToJoseAlgorithm,
76
84
  toBase64url: () => toBase64url,
77
85
  toJwk: () => toJwk,
78
86
  toJwkFromKey: () => toJwkFromKey,
@@ -108,24 +116,25 @@ var import_sha512 = require("@noble/hashes/sha512");
108
116
  var u8a = __toESM(require("uint8arrays"), 1);
109
117
  var { fromString, toString, SupportedEncodings } = u8a;
110
118
  var digestMethodParams = /* @__PURE__ */ __name((hashAlgorithm) => {
111
- if (hashAlgorithm === "SHA-256") {
112
- return {
113
- hashAlgorithm: "SHA-256",
114
- digestMethod: sha256DigestMethod,
115
- hash: import_sha256.sha256
116
- };
117
- } else if (hashAlgorithm === "SHA-384") {
118
- return {
119
- hashAlgorithm: "SHA-384",
120
- digestMethod: sha384DigestMethod,
121
- hash: import_sha512.sha384
122
- };
123
- } else {
124
- return {
125
- hashAlgorithm: "SHA-512",
126
- digestMethod: sha512DigestMethod,
127
- hash: import_sha512.sha512
128
- };
119
+ switch (normalizeHashAlgorithm(hashAlgorithm)) {
120
+ case "SHA-256":
121
+ return {
122
+ hashAlgorithm: "SHA-256",
123
+ digestMethod: sha256DigestMethod,
124
+ hash: import_sha256.sha256
125
+ };
126
+ case "SHA-384":
127
+ return {
128
+ hashAlgorithm: "SHA-384",
129
+ digestMethod: sha384DigestMethod,
130
+ hash: import_sha512.sha384
131
+ };
132
+ case "SHA-512":
133
+ return {
134
+ hashAlgorithm: "SHA-512",
135
+ digestMethod: sha512DigestMethod,
136
+ hash: import_sha512.sha512
137
+ };
129
138
  }
130
139
  }, "digestMethodParams");
131
140
  var shaHasher = /* @__PURE__ */ __name((input, alg) => {
@@ -449,7 +458,7 @@ var assertJwkClaimPresent = /* @__PURE__ */ __name((value, description) => {
449
458
  }, "assertJwkClaimPresent");
450
459
  var toBase64url = /* @__PURE__ */ __name((input) => toString2(fromString2(input), "base64url"), "toBase64url");
451
460
  var calculateJwkThumbprint = /* @__PURE__ */ __name((args) => {
452
- const { digestAlgorithm = "sha256" } = args;
461
+ const digestAlgorithm = normalizeHashAlgorithm(args.digestAlgorithm ?? "SHA-256");
453
462
  const jwk = sanitizedJwk(args.jwk);
454
463
  let components;
455
464
  switch (jwk.kty) {
@@ -493,7 +502,7 @@ var calculateJwkThumbprint = /* @__PURE__ */ __name((args) => {
493
502
  throw new Error('"kty" (Key Type) Parameter missing or unsupported');
494
503
  }
495
504
  const data = JSON.stringify(components);
496
- return digestAlgorithm === "sha512" ? digestMethodParams("SHA-512").digestMethod(data, "base64url") : digestMethodParams("SHA-256").digestMethod(data, "base64url");
505
+ return digestMethodParams(digestAlgorithm).digestMethod(data, "base64url");
497
506
  }, "calculateJwkThumbprint");
498
507
  var toJwkFromKey = /* @__PURE__ */ __name((key, opts) => {
499
508
  const isPrivateKey = "privateKeyHex" in key;
@@ -951,11 +960,45 @@ var hexStringFromUint8Array = /* @__PURE__ */ __name((value) => toString2(value,
951
960
  var signatureAlgorithmFromKey = /* @__PURE__ */ __name(async (args) => {
952
961
  const { key } = args;
953
962
  return signatureAlgorithmFromKeyType({
954
- type: key.type
963
+ type: key.type,
964
+ algorithms: key.meta?.algorithms
955
965
  });
956
966
  }, "signatureAlgorithmFromKey");
967
+ function signatureAlgorithmToJoseAlgorithm(alg) {
968
+ switch (alg) {
969
+ case "RSA_SHA256":
970
+ return import_ssi_types.JoseSignatureAlgorithm.RS256;
971
+ case "RSA_SHA384":
972
+ return import_ssi_types.JoseSignatureAlgorithm.RS384;
973
+ case "RSA_SHA512":
974
+ return import_ssi_types.JoseSignatureAlgorithm.RS512;
975
+ case "RSA_SSA_PSS_SHA256_MGF1":
976
+ return import_ssi_types.JoseSignatureAlgorithm.PS256;
977
+ case "RSA_SSA_PSS_SHA384_MGF1":
978
+ return import_ssi_types.JoseSignatureAlgorithm.PS384;
979
+ case "RSA_SSA_PSS_SHA512_MGF1":
980
+ return import_ssi_types.JoseSignatureAlgorithm.PS512;
981
+ case "ECDSA_SHA256":
982
+ return import_ssi_types.JoseSignatureAlgorithm.ES256;
983
+ case "ECDSA_SHA384":
984
+ return import_ssi_types.JoseSignatureAlgorithm.ES384;
985
+ case "ECDSA_SHA512":
986
+ return import_ssi_types.JoseSignatureAlgorithm.ES512;
987
+ case "ES256K":
988
+ return import_ssi_types.JoseSignatureAlgorithm.ES256K;
989
+ case "ED25519":
990
+ case "EdDSA":
991
+ return import_ssi_types.JoseSignatureAlgorithm.EdDSA;
992
+ default:
993
+ return alg;
994
+ }
995
+ }
996
+ __name(signatureAlgorithmToJoseAlgorithm, "signatureAlgorithmToJoseAlgorithm");
957
997
  var signatureAlgorithmFromKeyType = /* @__PURE__ */ __name((args) => {
958
- const { type } = args;
998
+ const { type, algorithms } = args;
999
+ if (algorithms && algorithms.length > 0) {
1000
+ return signatureAlgorithmToJoseAlgorithm(algorithms[0]);
1001
+ }
959
1002
  switch (type) {
960
1003
  case "Ed25519":
961
1004
  case "X25519":
@@ -969,7 +1012,7 @@ var signatureAlgorithmFromKeyType = /* @__PURE__ */ __name((args) => {
969
1012
  case "Secp256k1":
970
1013
  return import_ssi_types.JoseSignatureAlgorithm.ES256K;
971
1014
  case "RSA":
972
- return import_ssi_types.JoseSignatureAlgorithm.PS256;
1015
+ return import_ssi_types.JoseSignatureAlgorithm.RS256;
973
1016
  default:
974
1017
  throw new Error(`Key type '${type}' not supported`);
975
1018
  }
@@ -1220,6 +1263,75 @@ function toPkcs1FromHex(publicKeyHex) {
1220
1263
  return toString2(pkcs1, "hex");
1221
1264
  }
1222
1265
  __name(toPkcs1FromHex, "toPkcs1FromHex");
1266
+ function joseAlgorithmToDigest(alg) {
1267
+ const normalized = alg.toUpperCase().replace(/-/g, "");
1268
+ switch (normalized) {
1269
+ case "RS256":
1270
+ case "ES256":
1271
+ case "ES256K":
1272
+ case "PS256":
1273
+ case "HS256":
1274
+ return "SHA-256";
1275
+ case "RS384":
1276
+ case "ES384":
1277
+ case "PS384":
1278
+ case "HS384":
1279
+ return "SHA-384";
1280
+ case "RS512":
1281
+ case "ES512":
1282
+ case "PS512":
1283
+ case "HS512":
1284
+ return "SHA-512";
1285
+ case "EDDSA":
1286
+ case "ED25519":
1287
+ return "SHA-512";
1288
+ default:
1289
+ throw new Error(`Unsupported JOSE algorithm: ${alg}. Cannot determine digest algorithm.`);
1290
+ }
1291
+ }
1292
+ __name(joseAlgorithmToDigest, "joseAlgorithmToDigest");
1293
+ function isHash(input) {
1294
+ const length = input.length;
1295
+ if (length !== 64 && length !== 96 && length !== 128) {
1296
+ return false;
1297
+ }
1298
+ return input.match(/^([0-9A-Fa-f])+$/g) !== null;
1299
+ }
1300
+ __name(isHash, "isHash");
1301
+ function isHashString(input) {
1302
+ const length = input.length;
1303
+ if (length !== 32 && length !== 48 && length !== 64) {
1304
+ return false;
1305
+ }
1306
+ let printableCount = 0;
1307
+ for (let i = 0; i < length; i++) {
1308
+ const byte = input[i];
1309
+ if (byte === void 0) {
1310
+ return false;
1311
+ }
1312
+ if (byte >= 32 && byte <= 126) {
1313
+ printableCount++;
1314
+ }
1315
+ }
1316
+ const printableRatio = printableCount / length;
1317
+ return printableRatio < 0.9;
1318
+ }
1319
+ __name(isHashString, "isHashString");
1320
+ function normalizeHashAlgorithm(alg) {
1321
+ if (!alg) {
1322
+ return "SHA-256";
1323
+ }
1324
+ const upper = alg.toUpperCase();
1325
+ if (upper.includes("256")) return "SHA-256";
1326
+ if (upper.includes("384")) return "SHA-384";
1327
+ if (upper.includes("512")) return "SHA-512";
1328
+ throw new Error(`Invalid hash algorithm: ${alg}`);
1329
+ }
1330
+ __name(normalizeHashAlgorithm, "normalizeHashAlgorithm");
1331
+ function isSameHash(left, right) {
1332
+ return normalizeHashAlgorithm(left) === normalizeHashAlgorithm(right);
1333
+ }
1334
+ __name(isSameHash, "isSameHash");
1223
1335
 
1224
1336
  // src/conversion.ts
1225
1337
  var import_ssi_types2 = require("@sphereon/ssi-types");
@@ -1468,4 +1580,98 @@ function coseToJoseCurve(curve) {
1468
1580
  }
1469
1581
  }
1470
1582
  __name(coseToJoseCurve, "coseToJoseCurve");
1583
+ function joseSignatureAlgToWebCrypto(alg) {
1584
+ switch (alg) {
1585
+ case import_ssi_types2.JoseSignatureAlgorithm.RS256:
1586
+ case "RS256":
1587
+ return {
1588
+ name: "RSASSA-PKCS1-v1_5",
1589
+ hash: "SHA-256"
1590
+ };
1591
+ case import_ssi_types2.JoseSignatureAlgorithm.RS384:
1592
+ case "RS384":
1593
+ return {
1594
+ name: "RSASSA-PKCS1-v1_5",
1595
+ hash: "SHA-384"
1596
+ };
1597
+ case import_ssi_types2.JoseSignatureAlgorithm.RS512:
1598
+ case "RS512":
1599
+ return {
1600
+ name: "RSASSA-PKCS1-v1_5",
1601
+ hash: "SHA-512"
1602
+ };
1603
+ case import_ssi_types2.JoseSignatureAlgorithm.PS256:
1604
+ case "PS256":
1605
+ return {
1606
+ name: "RSA-PSS",
1607
+ hash: "SHA-256",
1608
+ saltLength: 32
1609
+ };
1610
+ case import_ssi_types2.JoseSignatureAlgorithm.PS384:
1611
+ case "PS384":
1612
+ return {
1613
+ name: "RSA-PSS",
1614
+ hash: "SHA-384",
1615
+ saltLength: 48
1616
+ };
1617
+ case import_ssi_types2.JoseSignatureAlgorithm.PS512:
1618
+ case "PS512":
1619
+ return {
1620
+ name: "RSA-PSS",
1621
+ hash: "SHA-512",
1622
+ saltLength: 64
1623
+ };
1624
+ case import_ssi_types2.JoseSignatureAlgorithm.ES256:
1625
+ case "ES256":
1626
+ return {
1627
+ name: "ECDSA",
1628
+ hash: "SHA-256"
1629
+ };
1630
+ case import_ssi_types2.JoseSignatureAlgorithm.ES384:
1631
+ case "ES384":
1632
+ return {
1633
+ name: "ECDSA",
1634
+ hash: "SHA-384"
1635
+ };
1636
+ case import_ssi_types2.JoseSignatureAlgorithm.ES512:
1637
+ case "ES512":
1638
+ return {
1639
+ name: "ECDSA",
1640
+ hash: "SHA-512"
1641
+ };
1642
+ case import_ssi_types2.JoseSignatureAlgorithm.ES256K:
1643
+ case "ES256K":
1644
+ return {
1645
+ name: "ECDSA",
1646
+ hash: "SHA-256"
1647
+ };
1648
+ case import_ssi_types2.JoseSignatureAlgorithm.EdDSA:
1649
+ case "EdDSA":
1650
+ return {
1651
+ name: "Ed25519",
1652
+ hash: ""
1653
+ };
1654
+ case import_ssi_types2.JoseSignatureAlgorithm.HS256:
1655
+ case "HS256":
1656
+ return {
1657
+ name: "HMAC",
1658
+ hash: "SHA-256"
1659
+ };
1660
+ case import_ssi_types2.JoseSignatureAlgorithm.HS384:
1661
+ case "HS384":
1662
+ return {
1663
+ name: "HMAC",
1664
+ hash: "SHA-384"
1665
+ };
1666
+ case import_ssi_types2.JoseSignatureAlgorithm.HS512:
1667
+ case "HS512":
1668
+ return {
1669
+ name: "HMAC",
1670
+ hash: "SHA-512"
1671
+ };
1672
+ default:
1673
+ throw Error(`Signature algorithm ${alg} not supported in Web Crypto API`);
1674
+ }
1675
+ }
1676
+ __name(joseSignatureAlgToWebCrypto, "joseSignatureAlgToWebCrypto");
1471
1677
  //# sourceMappingURL=index.cjs.map