-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
subtle_dsa.ts
32 lines (31 loc) · 1.01 KB
/
subtle_dsa.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { isCloudflareWorkers } from './env.ts'
import { JOSENotSupported } from '../util/errors.ts'
export default function subtleDsa(alg: string, algorithm: KeyAlgorithm | EcKeyAlgorithm) {
const hash = `SHA-${alg.slice(-3)}`
switch (alg) {
case 'HS256':
case 'HS384':
case 'HS512':
return { hash, name: 'HMAC' }
case 'PS256':
case 'PS384':
case 'PS512':
// @ts-expect-error
return { hash, name: 'RSA-PSS', saltLength: alg.slice(-3) >> 3 }
case 'RS256':
case 'RS384':
case 'RS512':
return { hash, name: 'RSASSA-PKCS1-v1_5' }
case 'ES256':
case 'ES384':
case 'ES512':
return { hash, name: 'ECDSA', namedCurve: (<EcKeyAlgorithm>algorithm).namedCurve }
case isCloudflareWorkers() && 'EdDSA':
const { namedCurve } = <EcKeyAlgorithm>algorithm
return <EcKeyAlgorithm>{ name: namedCurve, namedCurve }
default:
throw new JOSENotSupported(
`alg ${alg} is not supported either by JOSE or your javascript runtime`,
)
}
}