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

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.ts CHANGED
@@ -20,6 +20,7 @@ declare enum JwkKeyUse {
20
20
  declare const SIG_KEY_ALGS: string[];
21
21
  declare const ENC_KEY_ALGS: string[];
22
22
  type KeyVisibility = 'public' | 'private';
23
+ type DigestAlgorithm = 'SHA-256' | 'sha256' | 'SHA-384' | 'sha384' | 'SHA-512' | 'sha512';
23
24
  interface X509Opts {
24
25
  cn?: string;
25
26
  privateKeyPEM?: string;
@@ -44,6 +45,7 @@ type SignatureAlgorithmFromKeyArgs = {
44
45
  };
45
46
  type SignatureAlgorithmFromKeyTypeArgs = {
46
47
  type: TKeyType;
48
+ algorithms?: string[];
47
49
  };
48
50
  type KeyTypeFromCryptographicSuiteArgs = {
49
51
  crv?: string;
@@ -86,7 +88,7 @@ declare const toBase64url: (input: string) => string;
86
88
  */
87
89
  declare const calculateJwkThumbprint: (args: {
88
90
  jwk: JWK;
89
- digestAlgorithm?: "sha256" | "sha512";
91
+ digestAlgorithm?: DigestAlgorithm;
90
92
  }) => string;
91
93
  declare const toJwkFromKey: (key: IKey | MinimalImportableKey | ManagedKeyInfo, opts?: {
92
94
  use?: JwkKeyUse;
@@ -137,11 +139,13 @@ declare const isRawCompressedPublicKey: (key: Uint8Array) => boolean;
137
139
  declare const toRawCompressedHexPublicKey: (rawPublicKey: Uint8Array, keyType: TKeyType) => string;
138
140
  declare const hexStringFromUint8Array: (value: Uint8Array) => string;
139
141
  declare const signatureAlgorithmFromKey: (args: SignatureAlgorithmFromKeyArgs) => Promise<JoseSignatureAlgorithm>;
142
+ declare function signatureAlgorithmToJoseAlgorithm(alg: string): JoseSignatureAlgorithm;
140
143
  declare const signatureAlgorithmFromKeyType: (args: SignatureAlgorithmFromKeyTypeArgs) => JoseSignatureAlgorithm;
141
144
  declare const keyTypeFromCryptographicSuite: (args: KeyTypeFromCryptographicSuiteArgs) => TKeyType;
142
145
  declare function removeNulls<T>(obj: T | any): any;
143
146
  declare const globalCrypto: (setGlobal: boolean, suppliedCrypto?: Crypto) => Crypto;
144
147
  declare const sanitizedJwk: (input: JWK | JsonWebKey) => JWK;
148
+ declare const base64ToBase64Url: (input: string) => string;
145
149
  /**
146
150
  *
147
151
  */
@@ -169,6 +173,12 @@ declare function toPkcs1(derBytes: Uint8Array): Uint8Array;
169
173
  * @returns DER‐encoded PKCS#1 RSAPublicKey in hex
170
174
  */
171
175
  declare function toPkcs1FromHex(publicKeyHex: string): any;
176
+ declare function joseAlgorithmToDigest(alg: string): DigestAlgorithm;
177
+ declare function isHash(input: string): boolean;
178
+ declare function isHashString(input: Uint8Array): boolean;
179
+ type HashAlgorithm = 'SHA-256' | 'sha256' | 'SHA-384' | 'sha384' | 'SHA-512' | 'sha512';
180
+ declare function normalizeHashAlgorithm(alg?: HashAlgorithm): 'SHA-256' | 'SHA-384' | 'SHA-512';
181
+ declare function isSameHash(left: HashAlgorithm, right: HashAlgorithm): boolean;
172
182
 
173
183
  declare function coseKeyToJwk(coseKey: ICoseKeyJson): JWK;
174
184
  declare function jwkToCoseKey(jwk: JWK): ICoseKeyJson;
@@ -180,6 +190,11 @@ declare function joseToCoseKeyOperation(keyOp: JoseKeyOperation | JoseKeyOperati
180
190
  declare function coseToJoseKeyOperation(keyOp: ICoseKeyOperation): JoseKeyOperation;
181
191
  declare function joseToCoseCurve(curve: JoseCurve | JoseCurveString): ICoseCurve;
182
192
  declare function coseToJoseCurve(curve: ICoseCurve): JoseCurve;
193
+ declare function joseSignatureAlgToWebCrypto(alg: JoseSignatureAlgorithm | JoseSignatureAlgorithmString): {
194
+ name: string;
195
+ hash: string;
196
+ saltLength?: number;
197
+ };
183
198
 
184
199
  /**
185
200
  * Checks if the JWK is valid. It must contain all the required members.
@@ -221,13 +236,12 @@ declare function jwkJcsDecode(bytes: ByteView<JsonWebKey$1>): JsonWebKey$1;
221
236
  declare function jcsCanonicalize(object: any): string;
222
237
 
223
238
  declare const SupportedEncodings: any;
224
- type HashAlgorithm = 'SHA-256' | 'SHA-384' | 'SHA-512';
225
239
  type TDigestMethod = (input: string, encoding?: typeof SupportedEncodings) => string;
226
- declare const digestMethodParams: (hashAlgorithm: HashAlgorithm) => {
227
- hashAlgorithm: HashAlgorithm;
240
+ declare const digestMethodParams: (hashAlgorithm: DigestAlgorithm) => {
241
+ hashAlgorithm: DigestAlgorithm;
228
242
  digestMethod: TDigestMethod;
229
243
  hash: (data: Uint8Array) => Uint8Array;
230
244
  };
231
245
  declare const shaHasher: HasherSync;
232
246
 
233
- export { ENC_KEY_ALGS, type HashAlgorithm, type IImportProvidedOrGeneratedKeyArgs, type IKeyOpts, JWK_JCS_PUB_NAME, JWK_JCS_PUB_PREFIX, JwkKeyUse, Key, type KeyTypeFromCryptographicSuiteArgs, type KeyVisibility, SIG_KEY_ALGS, type SignatureAlgorithmFromKeyArgs, type SignatureAlgorithmFromKeyTypeArgs, type TDigestMethod, type TKeyType, type X509Opts, asn1DerToRawPublicKey, calculateJwkThumbprint, calculateJwkThumbprintForKey, coseKeyToJwk, coseToJoseCurve, coseToJoseKeyOperation, coseToJoseKty, coseToJoseSignatureAlg, digestMethodParams, generatePrivateKeyHex, getKms, globalCrypto, hexStringFromUint8Array, importProvidedOrGeneratedKey, isAsn1Der, isRawCompressedPublicKey, jcsCanonicalize, joseToCoseCurve, joseToCoseKeyOperation, joseToCoseKty, joseToCoseSignatureAlg, jwkDetermineUse, jwkJcsDecode, jwkJcsEncode, jwkToCoseKey, jwkToRawHexKey, keyTypeFromCryptographicSuite, logger, minimalJwk, padLeft, removeNulls, rsaJwkToRawHexKey, sanitizedJwk, shaHasher, signatureAlgorithmFromKey, signatureAlgorithmFromKeyType, toBase64url, toJwk, toJwkFromKey, toPkcs1, toPkcs1FromHex, toRawCompressedHexPublicKey, validateJwk, verifyRawSignature, x25519PublicHexFromPrivateHex };
247
+ export { type DigestAlgorithm, ENC_KEY_ALGS, type HashAlgorithm, type IImportProvidedOrGeneratedKeyArgs, type IKeyOpts, JWK_JCS_PUB_NAME, JWK_JCS_PUB_PREFIX, JwkKeyUse, Key, type KeyTypeFromCryptographicSuiteArgs, type KeyVisibility, SIG_KEY_ALGS, type SignatureAlgorithmFromKeyArgs, type SignatureAlgorithmFromKeyTypeArgs, type TDigestMethod, type TKeyType, type X509Opts, asn1DerToRawPublicKey, base64ToBase64Url, calculateJwkThumbprint, calculateJwkThumbprintForKey, coseKeyToJwk, coseToJoseCurve, coseToJoseKeyOperation, coseToJoseKty, coseToJoseSignatureAlg, digestMethodParams, generatePrivateKeyHex, getKms, globalCrypto, hexStringFromUint8Array, importProvidedOrGeneratedKey, isAsn1Der, isHash, isHashString, isRawCompressedPublicKey, isSameHash, jcsCanonicalize, joseAlgorithmToDigest, joseSignatureAlgToWebCrypto, joseToCoseCurve, joseToCoseKeyOperation, joseToCoseKty, joseToCoseSignatureAlg, jwkDetermineUse, jwkJcsDecode, jwkJcsEncode, jwkToCoseKey, jwkToRawHexKey, keyTypeFromCryptographicSuite, logger, minimalJwk, normalizeHashAlgorithm, padLeft, removeNulls, rsaJwkToRawHexKey, sanitizedJwk, shaHasher, signatureAlgorithmFromKey, signatureAlgorithmFromKeyType, signatureAlgorithmToJoseAlgorithm, toBase64url, toJwk, toJwkFromKey, toPkcs1, toPkcs1FromHex, toRawCompressedHexPublicKey, validateJwk, verifyRawSignature, x25519PublicHexFromPrivateHex };
package/dist/index.js CHANGED
@@ -24,24 +24,25 @@ import { sha384, sha512 } from "@noble/hashes/sha512";
24
24
  import * as u8a from "uint8arrays";
25
25
  var { fromString, toString, SupportedEncodings } = u8a;
26
26
  var digestMethodParams = /* @__PURE__ */ __name((hashAlgorithm) => {
27
- if (hashAlgorithm === "SHA-256") {
28
- return {
29
- hashAlgorithm: "SHA-256",
30
- digestMethod: sha256DigestMethod,
31
- hash: sha256
32
- };
33
- } else if (hashAlgorithm === "SHA-384") {
34
- return {
35
- hashAlgorithm: "SHA-384",
36
- digestMethod: sha384DigestMethod,
37
- hash: sha384
38
- };
39
- } else {
40
- return {
41
- hashAlgorithm: "SHA-512",
42
- digestMethod: sha512DigestMethod,
43
- hash: sha512
44
- };
27
+ switch (normalizeHashAlgorithm(hashAlgorithm)) {
28
+ case "SHA-256":
29
+ return {
30
+ hashAlgorithm: "SHA-256",
31
+ digestMethod: sha256DigestMethod,
32
+ hash: sha256
33
+ };
34
+ case "SHA-384":
35
+ return {
36
+ hashAlgorithm: "SHA-384",
37
+ digestMethod: sha384DigestMethod,
38
+ hash: sha384
39
+ };
40
+ case "SHA-512":
41
+ return {
42
+ hashAlgorithm: "SHA-512",
43
+ digestMethod: sha512DigestMethod,
44
+ hash: sha512
45
+ };
45
46
  }
46
47
  }, "digestMethodParams");
47
48
  var shaHasher = /* @__PURE__ */ __name((input, alg) => {
@@ -365,7 +366,7 @@ var assertJwkClaimPresent = /* @__PURE__ */ __name((value, description) => {
365
366
  }, "assertJwkClaimPresent");
366
367
  var toBase64url = /* @__PURE__ */ __name((input) => toString2(fromString2(input), "base64url"), "toBase64url");
367
368
  var calculateJwkThumbprint = /* @__PURE__ */ __name((args) => {
368
- const { digestAlgorithm = "sha256" } = args;
369
+ const digestAlgorithm = normalizeHashAlgorithm(args.digestAlgorithm ?? "SHA-256");
369
370
  const jwk = sanitizedJwk(args.jwk);
370
371
  let components;
371
372
  switch (jwk.kty) {
@@ -409,7 +410,7 @@ var calculateJwkThumbprint = /* @__PURE__ */ __name((args) => {
409
410
  throw new Error('"kty" (Key Type) Parameter missing or unsupported');
410
411
  }
411
412
  const data = JSON.stringify(components);
412
- return digestAlgorithm === "sha512" ? digestMethodParams("SHA-512").digestMethod(data, "base64url") : digestMethodParams("SHA-256").digestMethod(data, "base64url");
413
+ return digestMethodParams(digestAlgorithm).digestMethod(data, "base64url");
413
414
  }, "calculateJwkThumbprint");
414
415
  var toJwkFromKey = /* @__PURE__ */ __name((key, opts) => {
415
416
  const isPrivateKey = "privateKeyHex" in key;
@@ -867,11 +868,45 @@ var hexStringFromUint8Array = /* @__PURE__ */ __name((value) => toString2(value,
867
868
  var signatureAlgorithmFromKey = /* @__PURE__ */ __name(async (args) => {
868
869
  const { key } = args;
869
870
  return signatureAlgorithmFromKeyType({
870
- type: key.type
871
+ type: key.type,
872
+ algorithms: key.meta?.algorithms
871
873
  });
872
874
  }, "signatureAlgorithmFromKey");
875
+ function signatureAlgorithmToJoseAlgorithm(alg) {
876
+ switch (alg) {
877
+ case "RSA_SHA256":
878
+ return JoseSignatureAlgorithm.RS256;
879
+ case "RSA_SHA384":
880
+ return JoseSignatureAlgorithm.RS384;
881
+ case "RSA_SHA512":
882
+ return JoseSignatureAlgorithm.RS512;
883
+ case "RSA_SSA_PSS_SHA256_MGF1":
884
+ return JoseSignatureAlgorithm.PS256;
885
+ case "RSA_SSA_PSS_SHA384_MGF1":
886
+ return JoseSignatureAlgorithm.PS384;
887
+ case "RSA_SSA_PSS_SHA512_MGF1":
888
+ return JoseSignatureAlgorithm.PS512;
889
+ case "ECDSA_SHA256":
890
+ return JoseSignatureAlgorithm.ES256;
891
+ case "ECDSA_SHA384":
892
+ return JoseSignatureAlgorithm.ES384;
893
+ case "ECDSA_SHA512":
894
+ return JoseSignatureAlgorithm.ES512;
895
+ case "ES256K":
896
+ return JoseSignatureAlgorithm.ES256K;
897
+ case "ED25519":
898
+ case "EdDSA":
899
+ return JoseSignatureAlgorithm.EdDSA;
900
+ default:
901
+ return alg;
902
+ }
903
+ }
904
+ __name(signatureAlgorithmToJoseAlgorithm, "signatureAlgorithmToJoseAlgorithm");
873
905
  var signatureAlgorithmFromKeyType = /* @__PURE__ */ __name((args) => {
874
- const { type } = args;
906
+ const { type, algorithms } = args;
907
+ if (algorithms && algorithms.length > 0) {
908
+ return signatureAlgorithmToJoseAlgorithm(algorithms[0]);
909
+ }
875
910
  switch (type) {
876
911
  case "Ed25519":
877
912
  case "X25519":
@@ -885,7 +920,7 @@ var signatureAlgorithmFromKeyType = /* @__PURE__ */ __name((args) => {
885
920
  case "Secp256k1":
886
921
  return JoseSignatureAlgorithm.ES256K;
887
922
  case "RSA":
888
- return JoseSignatureAlgorithm.PS256;
923
+ return JoseSignatureAlgorithm.RS256;
889
924
  default:
890
925
  throw new Error(`Key type '${type}' not supported`);
891
926
  }
@@ -1136,6 +1171,75 @@ function toPkcs1FromHex(publicKeyHex) {
1136
1171
  return toString2(pkcs1, "hex");
1137
1172
  }
1138
1173
  __name(toPkcs1FromHex, "toPkcs1FromHex");
1174
+ function joseAlgorithmToDigest(alg) {
1175
+ const normalized = alg.toUpperCase().replace(/-/g, "");
1176
+ switch (normalized) {
1177
+ case "RS256":
1178
+ case "ES256":
1179
+ case "ES256K":
1180
+ case "PS256":
1181
+ case "HS256":
1182
+ return "SHA-256";
1183
+ case "RS384":
1184
+ case "ES384":
1185
+ case "PS384":
1186
+ case "HS384":
1187
+ return "SHA-384";
1188
+ case "RS512":
1189
+ case "ES512":
1190
+ case "PS512":
1191
+ case "HS512":
1192
+ return "SHA-512";
1193
+ case "EDDSA":
1194
+ case "ED25519":
1195
+ return "SHA-512";
1196
+ default:
1197
+ throw new Error(`Unsupported JOSE algorithm: ${alg}. Cannot determine digest algorithm.`);
1198
+ }
1199
+ }
1200
+ __name(joseAlgorithmToDigest, "joseAlgorithmToDigest");
1201
+ function isHash(input) {
1202
+ const length = input.length;
1203
+ if (length !== 64 && length !== 96 && length !== 128) {
1204
+ return false;
1205
+ }
1206
+ return input.match(/^([0-9A-Fa-f])+$/g) !== null;
1207
+ }
1208
+ __name(isHash, "isHash");
1209
+ function isHashString(input) {
1210
+ const length = input.length;
1211
+ if (length !== 32 && length !== 48 && length !== 64) {
1212
+ return false;
1213
+ }
1214
+ let printableCount = 0;
1215
+ for (let i = 0; i < length; i++) {
1216
+ const byte = input[i];
1217
+ if (byte === void 0) {
1218
+ return false;
1219
+ }
1220
+ if (byte >= 32 && byte <= 126) {
1221
+ printableCount++;
1222
+ }
1223
+ }
1224
+ const printableRatio = printableCount / length;
1225
+ return printableRatio < 0.9;
1226
+ }
1227
+ __name(isHashString, "isHashString");
1228
+ function normalizeHashAlgorithm(alg) {
1229
+ if (!alg) {
1230
+ return "SHA-256";
1231
+ }
1232
+ const upper = alg.toUpperCase();
1233
+ if (upper.includes("256")) return "SHA-256";
1234
+ if (upper.includes("384")) return "SHA-384";
1235
+ if (upper.includes("512")) return "SHA-512";
1236
+ throw new Error(`Invalid hash algorithm: ${alg}`);
1237
+ }
1238
+ __name(normalizeHashAlgorithm, "normalizeHashAlgorithm");
1239
+ function isSameHash(left, right) {
1240
+ return normalizeHashAlgorithm(left) === normalizeHashAlgorithm(right);
1241
+ }
1242
+ __name(isSameHash, "isSameHash");
1139
1243
 
1140
1244
  // src/conversion.ts
1141
1245
  import { ICoseCurve, ICoseKeyOperation, ICoseKeyType, ICoseSignatureAlgorithm, JoseCurve as JoseCurve2, JoseKeyOperation, JoseSignatureAlgorithm as JoseSignatureAlgorithm2, JwkKeyType as JwkKeyType2 } from "@sphereon/ssi-types";
@@ -1384,6 +1488,100 @@ function coseToJoseCurve(curve) {
1384
1488
  }
1385
1489
  }
1386
1490
  __name(coseToJoseCurve, "coseToJoseCurve");
1491
+ function joseSignatureAlgToWebCrypto(alg) {
1492
+ switch (alg) {
1493
+ case JoseSignatureAlgorithm2.RS256:
1494
+ case "RS256":
1495
+ return {
1496
+ name: "RSASSA-PKCS1-v1_5",
1497
+ hash: "SHA-256"
1498
+ };
1499
+ case JoseSignatureAlgorithm2.RS384:
1500
+ case "RS384":
1501
+ return {
1502
+ name: "RSASSA-PKCS1-v1_5",
1503
+ hash: "SHA-384"
1504
+ };
1505
+ case JoseSignatureAlgorithm2.RS512:
1506
+ case "RS512":
1507
+ return {
1508
+ name: "RSASSA-PKCS1-v1_5",
1509
+ hash: "SHA-512"
1510
+ };
1511
+ case JoseSignatureAlgorithm2.PS256:
1512
+ case "PS256":
1513
+ return {
1514
+ name: "RSA-PSS",
1515
+ hash: "SHA-256",
1516
+ saltLength: 32
1517
+ };
1518
+ case JoseSignatureAlgorithm2.PS384:
1519
+ case "PS384":
1520
+ return {
1521
+ name: "RSA-PSS",
1522
+ hash: "SHA-384",
1523
+ saltLength: 48
1524
+ };
1525
+ case JoseSignatureAlgorithm2.PS512:
1526
+ case "PS512":
1527
+ return {
1528
+ name: "RSA-PSS",
1529
+ hash: "SHA-512",
1530
+ saltLength: 64
1531
+ };
1532
+ case JoseSignatureAlgorithm2.ES256:
1533
+ case "ES256":
1534
+ return {
1535
+ name: "ECDSA",
1536
+ hash: "SHA-256"
1537
+ };
1538
+ case JoseSignatureAlgorithm2.ES384:
1539
+ case "ES384":
1540
+ return {
1541
+ name: "ECDSA",
1542
+ hash: "SHA-384"
1543
+ };
1544
+ case JoseSignatureAlgorithm2.ES512:
1545
+ case "ES512":
1546
+ return {
1547
+ name: "ECDSA",
1548
+ hash: "SHA-512"
1549
+ };
1550
+ case JoseSignatureAlgorithm2.ES256K:
1551
+ case "ES256K":
1552
+ return {
1553
+ name: "ECDSA",
1554
+ hash: "SHA-256"
1555
+ };
1556
+ case JoseSignatureAlgorithm2.EdDSA:
1557
+ case "EdDSA":
1558
+ return {
1559
+ name: "Ed25519",
1560
+ hash: ""
1561
+ };
1562
+ case JoseSignatureAlgorithm2.HS256:
1563
+ case "HS256":
1564
+ return {
1565
+ name: "HMAC",
1566
+ hash: "SHA-256"
1567
+ };
1568
+ case JoseSignatureAlgorithm2.HS384:
1569
+ case "HS384":
1570
+ return {
1571
+ name: "HMAC",
1572
+ hash: "SHA-384"
1573
+ };
1574
+ case JoseSignatureAlgorithm2.HS512:
1575
+ case "HS512":
1576
+ return {
1577
+ name: "HMAC",
1578
+ hash: "SHA-512"
1579
+ };
1580
+ default:
1581
+ throw Error(`Signature algorithm ${alg} not supported in Web Crypto API`);
1582
+ }
1583
+ }
1584
+ __name(joseSignatureAlgToWebCrypto, "joseSignatureAlgToWebCrypto");
1387
1585
  export {
1388
1586
  ENC_KEY_ALGS,
1389
1587
  JWK_JCS_PUB_NAME,
@@ -1392,6 +1590,7 @@ export {
1392
1590
  Key,
1393
1591
  SIG_KEY_ALGS,
1394
1592
  asn1DerToRawPublicKey,
1593
+ base64ToBase64Url,
1395
1594
  calculateJwkThumbprint,
1396
1595
  calculateJwkThumbprintForKey,
1397
1596
  coseKeyToJwk,
@@ -1406,8 +1605,13 @@ export {
1406
1605
  hexStringFromUint8Array,
1407
1606
  importProvidedOrGeneratedKey,
1408
1607
  isAsn1Der,
1608
+ isHash,
1609
+ isHashString,
1409
1610
  isRawCompressedPublicKey,
1611
+ isSameHash,
1410
1612
  jcsCanonicalize,
1613
+ joseAlgorithmToDigest,
1614
+ joseSignatureAlgToWebCrypto,
1411
1615
  joseToCoseCurve,
1412
1616
  joseToCoseKeyOperation,
1413
1617
  joseToCoseKty,
@@ -1420,6 +1624,7 @@ export {
1420
1624
  keyTypeFromCryptographicSuite,
1421
1625
  logger,
1422
1626
  minimalJwk,
1627
+ normalizeHashAlgorithm,
1423
1628
  padLeft,
1424
1629
  removeNulls,
1425
1630
  rsaJwkToRawHexKey,
@@ -1427,6 +1632,7 @@ export {
1427
1632
  shaHasher,
1428
1633
  signatureAlgorithmFromKey,
1429
1634
  signatureAlgorithmFromKeyType,
1635
+ signatureAlgorithmToJoseAlgorithm,
1430
1636
  toBase64url,
1431
1637
  toJwk,
1432
1638
  toJwkFromKey,