@sphereon/ssi-sdk-ext.key-utils 0.36.1-feat.SSISDK.83.5 → 0.36.1-feature.SSISDK.82.and.SSISDK.70.35

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;
@@ -86,7 +87,7 @@ declare const toBase64url: (input: string) => string;
86
87
  */
87
88
  declare const calculateJwkThumbprint: (args: {
88
89
  jwk: JWK;
89
- digestAlgorithm?: "sha256" | "sha512";
90
+ digestAlgorithm?: DigestAlgorithm;
90
91
  }) => string;
91
92
  declare const toJwkFromKey: (key: IKey | MinimalImportableKey | ManagedKeyInfo, opts?: {
92
93
  use?: JwkKeyUse;
@@ -142,6 +143,7 @@ declare const keyTypeFromCryptographicSuite: (args: KeyTypeFromCryptographicSuit
142
143
  declare function removeNulls<T>(obj: T | any): any;
143
144
  declare const globalCrypto: (setGlobal: boolean, suppliedCrypto?: Crypto) => Crypto;
144
145
  declare const sanitizedJwk: (input: JWK | JsonWebKey) => JWK;
146
+ declare const base64ToBase64Url: (input: string) => string;
145
147
  /**
146
148
  *
147
149
  */
@@ -169,6 +171,12 @@ declare function toPkcs1(derBytes: Uint8Array): Uint8Array;
169
171
  * @returns DER‐encoded PKCS#1 RSAPublicKey in hex
170
172
  */
171
173
  declare function toPkcs1FromHex(publicKeyHex: string): any;
174
+ declare function joseAlgorithmToDigest(alg: string): DigestAlgorithm;
175
+ declare function isHash(input: string): boolean;
176
+ declare function isHashString(input: Uint8Array): boolean;
177
+ type HashAlgorithm = 'SHA-256' | 'sha256' | 'SHA-384' | 'sha384' | 'SHA-512' | 'sha512';
178
+ declare function normalizeHashAlgorithm(alg?: HashAlgorithm): 'SHA-256' | 'SHA-384' | 'SHA-512';
179
+ declare function isSameHash(left: HashAlgorithm, right: HashAlgorithm): boolean;
172
180
 
173
181
  declare function coseKeyToJwk(coseKey: ICoseKeyJson): JWK;
174
182
  declare function jwkToCoseKey(jwk: JWK): ICoseKeyJson;
@@ -221,13 +229,12 @@ declare function jwkJcsDecode(bytes: ByteView<JsonWebKey$1>): JsonWebKey$1;
221
229
  declare function jcsCanonicalize(object: any): string;
222
230
 
223
231
  declare const SupportedEncodings: any;
224
- type HashAlgorithm = 'SHA-256' | 'SHA-384' | 'SHA-512';
225
232
  type TDigestMethod = (input: string, encoding?: typeof SupportedEncodings) => string;
226
- declare const digestMethodParams: (hashAlgorithm: HashAlgorithm) => {
227
- hashAlgorithm: HashAlgorithm;
233
+ declare const digestMethodParams: (hashAlgorithm: DigestAlgorithm) => {
234
+ hashAlgorithm: DigestAlgorithm;
228
235
  digestMethod: TDigestMethod;
229
236
  hash: (data: Uint8Array) => Uint8Array;
230
237
  };
231
238
  declare const shaHasher: HasherSync;
232
239
 
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 };
240
+ 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, joseToCoseCurve, joseToCoseKeyOperation, joseToCoseKty, joseToCoseSignatureAlg, jwkDetermineUse, jwkJcsDecode, jwkJcsEncode, jwkToCoseKey, jwkToRawHexKey, keyTypeFromCryptographicSuite, logger, minimalJwk, normalizeHashAlgorithm, padLeft, removeNulls, rsaJwkToRawHexKey, sanitizedJwk, shaHasher, signatureAlgorithmFromKey, signatureAlgorithmFromKeyType, 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;
@@ -1136,6 +1137,71 @@ function toPkcs1FromHex(publicKeyHex) {
1136
1137
  return toString2(pkcs1, "hex");
1137
1138
  }
1138
1139
  __name(toPkcs1FromHex, "toPkcs1FromHex");
1140
+ function joseAlgorithmToDigest(alg) {
1141
+ switch (alg.toUpperCase().replace("-", "")) {
1142
+ case "RS256":
1143
+ case "ES256":
1144
+ case "ES256K":
1145
+ case "PS256":
1146
+ case "HS256":
1147
+ return "SHA-256";
1148
+ case "RS384":
1149
+ case "ES384":
1150
+ case "PS384":
1151
+ case "HS384":
1152
+ return "SHA-384";
1153
+ case "RS512":
1154
+ case "ES512":
1155
+ case "PS512":
1156
+ case "HS512":
1157
+ return "SHA-512";
1158
+ case "EdDSA":
1159
+ return "SHA-512";
1160
+ default:
1161
+ return "SHA-256";
1162
+ }
1163
+ }
1164
+ __name(joseAlgorithmToDigest, "joseAlgorithmToDigest");
1165
+ function isHash(input) {
1166
+ const length = input.length;
1167
+ if (length !== 64 && length !== 96 && length !== 128) {
1168
+ return false;
1169
+ }
1170
+ return input.match(/^([0-9A-Fa-f])+$/g) !== null;
1171
+ }
1172
+ __name(isHash, "isHash");
1173
+ function isHashString(input) {
1174
+ const length = input.length;
1175
+ if (length !== 32 && length !== 48 && length !== 64) {
1176
+ return false;
1177
+ }
1178
+ for (let i = 0; i < length; i++) {
1179
+ const byte = input[i];
1180
+ if (byte === void 0) {
1181
+ return false;
1182
+ }
1183
+ if (!(byte >= 48 && byte <= 57 || byte >= 65 && byte <= 70 || byte >= 97 && byte <= 102)) {
1184
+ return false;
1185
+ }
1186
+ }
1187
+ return true;
1188
+ }
1189
+ __name(isHashString, "isHashString");
1190
+ function normalizeHashAlgorithm(alg) {
1191
+ if (!alg) {
1192
+ return "SHA-256";
1193
+ }
1194
+ const upper = alg.toUpperCase();
1195
+ if (upper.includes("256")) return "SHA-256";
1196
+ if (upper.includes("384")) return "SHA-384";
1197
+ if (upper.includes("512")) return "SHA-512";
1198
+ throw new Error(`Invalid hash algorithm: ${alg}`);
1199
+ }
1200
+ __name(normalizeHashAlgorithm, "normalizeHashAlgorithm");
1201
+ function isSameHash(left, right) {
1202
+ return normalizeHashAlgorithm(left) === normalizeHashAlgorithm(right);
1203
+ }
1204
+ __name(isSameHash, "isSameHash");
1139
1205
 
1140
1206
  // src/conversion.ts
1141
1207
  import { ICoseCurve, ICoseKeyOperation, ICoseKeyType, ICoseSignatureAlgorithm, JoseCurve as JoseCurve2, JoseKeyOperation, JoseSignatureAlgorithm as JoseSignatureAlgorithm2, JwkKeyType as JwkKeyType2 } from "@sphereon/ssi-types";
@@ -1392,6 +1458,7 @@ export {
1392
1458
  Key,
1393
1459
  SIG_KEY_ALGS,
1394
1460
  asn1DerToRawPublicKey,
1461
+ base64ToBase64Url,
1395
1462
  calculateJwkThumbprint,
1396
1463
  calculateJwkThumbprintForKey,
1397
1464
  coseKeyToJwk,
@@ -1406,8 +1473,12 @@ export {
1406
1473
  hexStringFromUint8Array,
1407
1474
  importProvidedOrGeneratedKey,
1408
1475
  isAsn1Der,
1476
+ isHash,
1477
+ isHashString,
1409
1478
  isRawCompressedPublicKey,
1479
+ isSameHash,
1410
1480
  jcsCanonicalize,
1481
+ joseAlgorithmToDigest,
1411
1482
  joseToCoseCurve,
1412
1483
  joseToCoseKeyOperation,
1413
1484
  joseToCoseKty,
@@ -1420,6 +1491,7 @@ export {
1420
1491
  keyTypeFromCryptographicSuite,
1421
1492
  logger,
1422
1493
  minimalJwk,
1494
+ normalizeHashAlgorithm,
1423
1495
  padLeft,
1424
1496
  removeNulls,
1425
1497
  rsaJwkToRawHexKey,