@sphereon/ssi-sdk-ext.key-utils 0.36.1-feature.SSISDK.70.integrate.digidentity.57 → 0.36.1-feature.SSISDK.78.41
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 +23 -133
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -13
- package/dist/index.d.ts +5 -13
- package/dist/index.js +23 -133
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/digest-methods.ts +11 -13
- package/src/functions.ts +9 -133
- package/src/types/key-util-types.ts +0 -3
package/src/functions.ts
CHANGED
|
@@ -31,7 +31,6 @@ import * as u8a from 'uint8arrays'
|
|
|
31
31
|
import { digestMethodParams } from './digest-methods'
|
|
32
32
|
import { validateJwk } from './jwk-jcs'
|
|
33
33
|
import {
|
|
34
|
-
DigestAlgorithm,
|
|
35
34
|
ENC_KEY_ALGS,
|
|
36
35
|
type IImportProvidedOrGeneratedKeyArgs,
|
|
37
36
|
JwkKeyUse,
|
|
@@ -199,8 +198,8 @@ export const toBase64url = (input: string): string => toString(fromString(input)
|
|
|
199
198
|
* Calculate the JWK thumbprint
|
|
200
199
|
* @param args
|
|
201
200
|
*/
|
|
202
|
-
export const calculateJwkThumbprint = (args: { jwk: JWK; digestAlgorithm?:
|
|
203
|
-
const digestAlgorithm =
|
|
201
|
+
export const calculateJwkThumbprint = (args: { jwk: JWK; digestAlgorithm?: 'sha256' | 'sha512' }): string => {
|
|
202
|
+
const { digestAlgorithm = 'sha256' } = args
|
|
204
203
|
const jwk = sanitizedJwk(args.jwk)
|
|
205
204
|
let components
|
|
206
205
|
switch (jwk.kty) {
|
|
@@ -228,7 +227,10 @@ export const calculateJwkThumbprint = (args: { jwk: JWK; digestAlgorithm?: Diges
|
|
|
228
227
|
throw new Error('"kty" (Key Type) Parameter missing or unsupported')
|
|
229
228
|
}
|
|
230
229
|
const data = JSON.stringify(components)
|
|
231
|
-
|
|
230
|
+
|
|
231
|
+
return digestAlgorithm === 'sha512'
|
|
232
|
+
? digestMethodParams('SHA-512').digestMethod(data, 'base64url')
|
|
233
|
+
: digestMethodParams('SHA-256').digestMethod(data, 'base64url')
|
|
232
234
|
}
|
|
233
235
|
|
|
234
236
|
export const toJwkFromKey = (
|
|
@@ -789,49 +791,11 @@ export const hexStringFromUint8Array = (value: Uint8Array): string => toString(v
|
|
|
789
791
|
|
|
790
792
|
export const signatureAlgorithmFromKey = async (args: SignatureAlgorithmFromKeyArgs): Promise<JoseSignatureAlgorithm> => {
|
|
791
793
|
const { key } = args
|
|
792
|
-
return signatureAlgorithmFromKeyType({ type: key.type
|
|
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
|
-
}
|
|
794
|
+
return signatureAlgorithmFromKeyType({ type: key.type })
|
|
824
795
|
}
|
|
825
796
|
|
|
826
797
|
export const signatureAlgorithmFromKeyType = (args: SignatureAlgorithmFromKeyTypeArgs): JoseSignatureAlgorithm => {
|
|
827
|
-
const { type
|
|
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
|
|
798
|
+
const { type } = args
|
|
835
799
|
switch (type) {
|
|
836
800
|
case 'Ed25519':
|
|
837
801
|
case 'X25519':
|
|
@@ -845,7 +809,7 @@ export const signatureAlgorithmFromKeyType = (args: SignatureAlgorithmFromKeyTyp
|
|
|
845
809
|
case 'Secp256k1':
|
|
846
810
|
return JoseSignatureAlgorithm.ES256K
|
|
847
811
|
case 'RSA':
|
|
848
|
-
return JoseSignatureAlgorithm.
|
|
812
|
+
return JoseSignatureAlgorithm.PS256
|
|
849
813
|
default:
|
|
850
814
|
throw new Error(`Key type '${type}' not supported`)
|
|
851
815
|
}
|
|
@@ -1158,91 +1122,3 @@ export function toPkcs1FromHex(publicKeyHex: string) {
|
|
|
1158
1122
|
const pkcs1 = toPkcs1(fromString(publicKeyHex, 'hex'))
|
|
1159
1123
|
return toString(pkcs1, 'hex')
|
|
1160
1124
|
}
|
|
1161
|
-
|
|
1162
|
-
export function joseAlgorithmToDigest(alg: string): DigestAlgorithm {
|
|
1163
|
-
// Normalize the algorithm string by converting to uppercase and removing hyphens
|
|
1164
|
-
const normalized = alg.toUpperCase().replace(/-/g, '')
|
|
1165
|
-
|
|
1166
|
-
switch (normalized) {
|
|
1167
|
-
case 'RS256':
|
|
1168
|
-
case 'ES256':
|
|
1169
|
-
case 'ES256K':
|
|
1170
|
-
case 'PS256':
|
|
1171
|
-
case 'HS256':
|
|
1172
|
-
return 'SHA-256'
|
|
1173
|
-
case 'RS384':
|
|
1174
|
-
case 'ES384':
|
|
1175
|
-
case 'PS384':
|
|
1176
|
-
case 'HS384':
|
|
1177
|
-
return 'SHA-384'
|
|
1178
|
-
case 'RS512':
|
|
1179
|
-
case 'ES512':
|
|
1180
|
-
case 'PS512':
|
|
1181
|
-
case 'HS512':
|
|
1182
|
-
return 'SHA-512'
|
|
1183
|
-
case 'EDDSA':
|
|
1184
|
-
case 'ED25519':
|
|
1185
|
-
return 'SHA-512'
|
|
1186
|
-
default:
|
|
1187
|
-
throw new Error(`Unsupported JOSE algorithm: ${alg}. Cannot determine digest algorithm.`)
|
|
1188
|
-
}
|
|
1189
|
-
}
|
|
1190
|
-
|
|
1191
|
-
export function isHash(input: string): boolean {
|
|
1192
|
-
const length = input.length
|
|
1193
|
-
// SHA-256: 64 hex chars, SHA-384: 96 hex chars, SHA-512: 128 hex chars
|
|
1194
|
-
if (length !== 64 && length !== 96 && length !== 128) {
|
|
1195
|
-
return false
|
|
1196
|
-
}
|
|
1197
|
-
return input.match(/^([0-9A-Fa-f])+$/g) !== null
|
|
1198
|
-
}
|
|
1199
|
-
|
|
1200
|
-
export function isHashString(input: Uint8Array): boolean {
|
|
1201
|
-
const length = input.length
|
|
1202
|
-
// SHA-256: 32 bytes, SHA-384: 48 bytes, SHA-512: 64 bytes
|
|
1203
|
-
if (length !== 32 && length !== 48 && length !== 64) {
|
|
1204
|
-
return false
|
|
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
|
|
1216
|
-
for (let i = 0; i < length; i++) {
|
|
1217
|
-
const byte = input[i]
|
|
1218
|
-
if (byte === undefined) {
|
|
1219
|
-
return false
|
|
1220
|
-
}
|
|
1221
|
-
// Count printable ASCII characters
|
|
1222
|
-
if (byte >= 0x20 && byte <= 0x7e) {
|
|
1223
|
-
printableCount++
|
|
1224
|
-
}
|
|
1225
|
-
}
|
|
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
|
|
1231
|
-
}
|
|
1232
|
-
|
|
1233
|
-
export type HashAlgorithm = 'SHA-256' | 'sha256' | 'SHA-384' | 'sha384' | 'SHA-512' | 'sha512'
|
|
1234
|
-
|
|
1235
|
-
export function normalizeHashAlgorithm(alg?: HashAlgorithm): 'SHA-256' | 'SHA-384' | 'SHA-512' {
|
|
1236
|
-
if (!alg) {
|
|
1237
|
-
return 'SHA-256'
|
|
1238
|
-
}
|
|
1239
|
-
const upper = alg.toUpperCase()
|
|
1240
|
-
if (upper.includes('256')) return 'SHA-256'
|
|
1241
|
-
if (upper.includes('384')) return 'SHA-384'
|
|
1242
|
-
if (upper.includes('512')) return 'SHA-512'
|
|
1243
|
-
throw new Error(`Invalid hash algorithm: ${alg}`)
|
|
1244
|
-
}
|
|
1245
|
-
|
|
1246
|
-
export function isSameHash(left: HashAlgorithm, right: HashAlgorithm): boolean {
|
|
1247
|
-
return normalizeHashAlgorithm(left) === normalizeHashAlgorithm(right)
|
|
1248
|
-
}
|
|
@@ -21,8 +21,6 @@ export const ENC_KEY_ALGS = ['X25519', 'ECDH_ES_A256KW', 'RSA_OAEP_256']
|
|
|
21
21
|
|
|
22
22
|
export type KeyVisibility = 'public' | 'private'
|
|
23
23
|
|
|
24
|
-
export type DigestAlgorithm = 'SHA-256' | 'sha256' | 'SHA-384' | 'sha384' | 'SHA-512' | 'sha512'
|
|
25
|
-
|
|
26
24
|
export interface X509Opts {
|
|
27
25
|
cn?: string // The certificate Common Name. Will be used as the KID for the private key. Uses alias if not provided.
|
|
28
26
|
privateKeyPEM?: string // Optional as you also need to provide it in hex format, but advisable to use it
|
|
@@ -55,7 +53,6 @@ export type SignatureAlgorithmFromKeyArgs = {
|
|
|
55
53
|
|
|
56
54
|
export type SignatureAlgorithmFromKeyTypeArgs = {
|
|
57
55
|
type: TKeyType
|
|
58
|
-
algorithms?: string[]
|
|
59
56
|
}
|
|
60
57
|
|
|
61
58
|
export type KeyTypeFromCryptographicSuiteArgs = {
|