@sphereon/ssi-sdk-ext.key-utils 0.36.1-feature.SSISDK.82.and.SSISDK.70.37 → 0.36.1-feature.SSISDK.98.deep.merge.metadata.109

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/src/conversion.ts CHANGED
@@ -242,3 +242,56 @@ export function coseToJoseCurve(curve: ICoseCurve): JoseCurve {
242
242
  throw Error(`Curve ${curve} not supported in Jose`)
243
243
  }
244
244
  }
245
+
246
+ export function joseSignatureAlgToWebCrypto(alg: JoseSignatureAlgorithm | JoseSignatureAlgorithmString): {
247
+ name: string
248
+ hash: string
249
+ saltLength?: number
250
+ } {
251
+ switch (alg) {
252
+ case JoseSignatureAlgorithm.RS256:
253
+ case 'RS256':
254
+ return { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' }
255
+ case JoseSignatureAlgorithm.RS384:
256
+ case 'RS384':
257
+ return { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-384' }
258
+ case JoseSignatureAlgorithm.RS512:
259
+ case 'RS512':
260
+ return { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-512' }
261
+ case JoseSignatureAlgorithm.PS256:
262
+ case 'PS256':
263
+ return { name: 'RSA-PSS', hash: 'SHA-256', saltLength: 32 }
264
+ case JoseSignatureAlgorithm.PS384:
265
+ case 'PS384':
266
+ return { name: 'RSA-PSS', hash: 'SHA-384', saltLength: 48 }
267
+ case JoseSignatureAlgorithm.PS512:
268
+ case 'PS512':
269
+ return { name: 'RSA-PSS', hash: 'SHA-512', saltLength: 64 }
270
+ case JoseSignatureAlgorithm.ES256:
271
+ case 'ES256':
272
+ return { name: 'ECDSA', hash: 'SHA-256' }
273
+ case JoseSignatureAlgorithm.ES384:
274
+ case 'ES384':
275
+ return { name: 'ECDSA', hash: 'SHA-384' }
276
+ case JoseSignatureAlgorithm.ES512:
277
+ case 'ES512':
278
+ return { name: 'ECDSA', hash: 'SHA-512' }
279
+ case JoseSignatureAlgorithm.ES256K:
280
+ case 'ES256K':
281
+ return { name: 'ECDSA', hash: 'SHA-256' }
282
+ case JoseSignatureAlgorithm.EdDSA:
283
+ case 'EdDSA':
284
+ return { name: 'Ed25519', hash: '' }
285
+ case JoseSignatureAlgorithm.HS256:
286
+ case 'HS256':
287
+ return { name: 'HMAC', hash: 'SHA-256' }
288
+ case JoseSignatureAlgorithm.HS384:
289
+ case 'HS384':
290
+ return { name: 'HMAC', hash: 'SHA-384' }
291
+ case JoseSignatureAlgorithm.HS512:
292
+ case 'HS512':
293
+ return { name: 'HMAC', hash: 'SHA-512' }
294
+ default:
295
+ throw Error(`Signature algorithm ${alg} not supported in Web Crypto API`)
296
+ }
297
+ }
package/src/functions.ts CHANGED
@@ -789,11 +789,49 @@ export const hexStringFromUint8Array = (value: Uint8Array): string => toString(v
789
789
 
790
790
  export const signatureAlgorithmFromKey = async (args: SignatureAlgorithmFromKeyArgs): Promise<JoseSignatureAlgorithm> => {
791
791
  const { key } = args
792
- return signatureAlgorithmFromKeyType({ type: key.type })
792
+ return signatureAlgorithmFromKeyType({ type: key.type, algorithms: key.meta?.algorithms })
793
+ }
794
+
795
+ export function signatureAlgorithmToJoseAlgorithm(alg: string): JoseSignatureAlgorithm {
796
+ switch (alg) {
797
+ case 'RSA_SHA256':
798
+ return JoseSignatureAlgorithm.RS256
799
+ case 'RSA_SHA384':
800
+ return JoseSignatureAlgorithm.RS384
801
+ case 'RSA_SHA512':
802
+ return JoseSignatureAlgorithm.RS512
803
+ case 'RSA_SSA_PSS_SHA256_MGF1':
804
+ return JoseSignatureAlgorithm.PS256
805
+ case 'RSA_SSA_PSS_SHA384_MGF1':
806
+ return JoseSignatureAlgorithm.PS384
807
+ case 'RSA_SSA_PSS_SHA512_MGF1':
808
+ return JoseSignatureAlgorithm.PS512
809
+ case 'ECDSA_SHA256':
810
+ return JoseSignatureAlgorithm.ES256
811
+ case 'ECDSA_SHA384':
812
+ return JoseSignatureAlgorithm.ES384
813
+ case 'ECDSA_SHA512':
814
+ return JoseSignatureAlgorithm.ES512
815
+ case 'ES256K':
816
+ return JoseSignatureAlgorithm.ES256K
817
+ case 'ED25519':
818
+ case 'EdDSA':
819
+ return JoseSignatureAlgorithm.EdDSA
820
+ default:
821
+ // If already in JOSE format, return as-is
822
+ return alg as JoseSignatureAlgorithm
823
+ }
793
824
  }
794
825
 
795
826
  export const signatureAlgorithmFromKeyType = (args: SignatureAlgorithmFromKeyTypeArgs): JoseSignatureAlgorithm => {
796
- const { type } = args
827
+ const { type, algorithms } = args
828
+
829
+ // If algorithms metadata is provided, use the first one
830
+ if (algorithms && algorithms.length > 0) {
831
+ return signatureAlgorithmToJoseAlgorithm(algorithms[0])
832
+ }
833
+
834
+ // Fallback to type-based defaults
797
835
  switch (type) {
798
836
  case 'Ed25519':
799
837
  case 'X25519':
@@ -807,7 +845,7 @@ export const signatureAlgorithmFromKeyType = (args: SignatureAlgorithmFromKeyTyp
807
845
  case 'Secp256k1':
808
846
  return JoseSignatureAlgorithm.ES256K
809
847
  case 'RSA':
810
- return JoseSignatureAlgorithm.PS256
848
+ return JoseSignatureAlgorithm.RS256 // Default to RS256 instead of PS256
811
849
  default:
812
850
  throw new Error(`Key type '${type}' not supported`)
813
851
  }
@@ -1122,7 +1160,10 @@ export function toPkcs1FromHex(publicKeyHex: string) {
1122
1160
  }
1123
1161
 
1124
1162
  export function joseAlgorithmToDigest(alg: string): DigestAlgorithm {
1125
- switch (alg.toUpperCase().replace('-', '')) {
1163
+ // Normalize the algorithm string by converting to uppercase and removing hyphens
1164
+ const normalized = alg.toUpperCase().replace(/-/g, '')
1165
+
1166
+ switch (normalized) {
1126
1167
  case 'RS256':
1127
1168
  case 'ES256':
1128
1169
  case 'ES256K':
@@ -1139,10 +1180,11 @@ export function joseAlgorithmToDigest(alg: string): DigestAlgorithm {
1139
1180
  case 'PS512':
1140
1181
  case 'HS512':
1141
1182
  return 'SHA-512'
1142
- case 'EdDSA':
1183
+ case 'EDDSA':
1184
+ case 'ED25519':
1143
1185
  return 'SHA-512'
1144
1186
  default:
1145
- return 'SHA-256'
1187
+ throw new Error(`Unsupported JOSE algorithm: ${alg}. Cannot determine digest algorithm.`)
1146
1188
  }
1147
1189
  }
1148
1190
 
@@ -1161,17 +1203,31 @@ export function isHashString(input: Uint8Array): boolean {
1161
1203
  if (length !== 32 && length !== 48 && length !== 64) {
1162
1204
  return false
1163
1205
  }
1206
+
1207
+ // A hash digest is raw binary data (any byte values 0x00-0xFF are valid).
1208
+ // We should NOT check if bytes are ASCII hex characters, as that would only detect
1209
+ // hex-encoded strings, not actual binary hash digests.
1210
+ // Instead, we use a heuristic: if the data looks like it has high entropy
1211
+ // and is the right length, we assume it's already a hash.
1212
+
1213
+ // Simple heuristic: Check if data is all printable ASCII (which would indicate it's NOT a hash)
1214
+ // Printable ASCII is roughly 0x20-0x7E
1215
+ let printableCount = 0
1164
1216
  for (let i = 0; i < length; i++) {
1165
1217
  const byte = input[i]
1166
1218
  if (byte === undefined) {
1167
1219
  return false
1168
1220
  }
1169
- // 0-9: 48-57, A-F: 65-70, a-f: 97-102
1170
- if (!((byte >= 48 && byte <= 57) || (byte >= 65 && byte <= 70) || (byte >= 97 && byte <= 102))) {
1171
- return false
1221
+ // Count printable ASCII characters
1222
+ if (byte >= 0x20 && byte <= 0x7e) {
1223
+ printableCount++
1172
1224
  }
1173
1225
  }
1174
- return true
1226
+
1227
+ // If more than 90% of bytes are printable ASCII, it's likely NOT a raw binary hash
1228
+ // Raw binary hashes should have a more uniform distribution across all byte values
1229
+ const printableRatio = printableCount / length
1230
+ return printableRatio < 0.9
1175
1231
  }
1176
1232
 
1177
1233
  export type HashAlgorithm = 'SHA-256' | 'sha256' | 'SHA-384' | 'sha384' | 'SHA-512' | 'sha512'
@@ -55,6 +55,7 @@ export type SignatureAlgorithmFromKeyArgs = {
55
55
 
56
56
  export type SignatureAlgorithmFromKeyTypeArgs = {
57
57
  type: TKeyType
58
+ algorithms?: string[]
58
59
  }
59
60
 
60
61
  export type KeyTypeFromCryptographicSuiteArgs = {