@sphereon/ssi-sdk-ext.key-utils 0.36.1-next.50 → 0.36.1-next.70

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,6 +139,7 @@ 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;
@@ -169,6 +172,12 @@ declare function toPkcs1(derBytes: Uint8Array): Uint8Array;
169
172
  * @returns DER‐encoded PKCS#1 RSAPublicKey in hex
170
173
  */
171
174
  declare function toPkcs1FromHex(publicKeyHex: string): any;
175
+ declare function joseAlgorithmToDigest(alg: string): DigestAlgorithm;
176
+ declare function isHash(input: string): boolean;
177
+ declare function isHashString(input: Uint8Array): boolean;
178
+ type HashAlgorithm = 'SHA-256' | 'sha256' | 'SHA-384' | 'sha384' | 'SHA-512' | 'sha512';
179
+ declare function normalizeHashAlgorithm(alg?: HashAlgorithm): 'SHA-256' | 'SHA-384' | 'SHA-512';
180
+ declare function isSameHash(left: HashAlgorithm, right: HashAlgorithm): boolean;
172
181
 
173
182
  declare function coseKeyToJwk(coseKey: ICoseKeyJson): JWK;
174
183
  declare function jwkToCoseKey(jwk: JWK): ICoseKeyJson;
@@ -221,13 +230,12 @@ declare function jwkJcsDecode(bytes: ByteView<JsonWebKey$1>): JsonWebKey$1;
221
230
  declare function jcsCanonicalize(object: any): string;
222
231
 
223
232
  declare const SupportedEncodings: any;
224
- type HashAlgorithm = 'SHA-256' | 'SHA-384' | 'SHA-512';
225
233
  type TDigestMethod = (input: string, encoding?: typeof SupportedEncodings) => string;
226
- declare const digestMethodParams: (hashAlgorithm: HashAlgorithm) => {
227
- hashAlgorithm: HashAlgorithm;
234
+ declare const digestMethodParams: (hashAlgorithm: DigestAlgorithm) => {
235
+ hashAlgorithm: DigestAlgorithm;
228
236
  digestMethod: TDigestMethod;
229
237
  hash: (data: Uint8Array) => Uint8Array;
230
238
  };
231
239
  declare const shaHasher: HasherSync;
232
240
 
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 };
241
+ 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, calculateJwkThumbprint, calculateJwkThumbprintForKey, coseKeyToJwk, coseToJoseCurve, coseToJoseKeyOperation, coseToJoseKty, coseToJoseSignatureAlg, digestMethodParams, generatePrivateKeyHex, getKms, globalCrypto, hexStringFromUint8Array, importProvidedOrGeneratedKey, isAsn1Der, isHash, isHashString, isRawCompressedPublicKey, isSameHash, jcsCanonicalize, joseAlgorithmToDigest, 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";
@@ -1406,8 +1510,12 @@ export {
1406
1510
  hexStringFromUint8Array,
1407
1511
  importProvidedOrGeneratedKey,
1408
1512
  isAsn1Der,
1513
+ isHash,
1514
+ isHashString,
1409
1515
  isRawCompressedPublicKey,
1516
+ isSameHash,
1410
1517
  jcsCanonicalize,
1518
+ joseAlgorithmToDigest,
1411
1519
  joseToCoseCurve,
1412
1520
  joseToCoseKeyOperation,
1413
1521
  joseToCoseKty,
@@ -1420,6 +1528,7 @@ export {
1420
1528
  keyTypeFromCryptographicSuite,
1421
1529
  logger,
1422
1530
  minimalJwk,
1531
+ normalizeHashAlgorithm,
1423
1532
  padLeft,
1424
1533
  removeNulls,
1425
1534
  rsaJwkToRawHexKey,
@@ -1427,6 +1536,7 @@ export {
1427
1536
  shaHasher,
1428
1537
  signatureAlgorithmFromKey,
1429
1538
  signatureAlgorithmFromKeyType,
1539
+ signatureAlgorithmToJoseAlgorithm,
1430
1540
  toBase64url,
1431
1541
  toJwk,
1432
1542
  toJwkFromKey,