| 31 | * @returns {[string, string]} |
| 32 | */ |
| 33 | export function validateAlgorithm(algorithm: string, publicKeyType?: string): [string, string] { |
| 34 | const alg = algorithm.toLowerCase().split('-') |
| 35 | |
| 36 | if (alg[0] === 'hs2019') { |
| 37 | return publicKeyType !== undefined ? validateAlgorithm(publicKeyType + '-sha256') : ['hs2019', 'sha256'] |
| 38 | } |
| 39 | |
| 40 | if (alg.length !== 2) { |
| 41 | throw new InvalidAlgorithmError(alg[0].toUpperCase() + ' is not a ' + 'valid algorithm') |
| 42 | } |
| 43 | |
| 44 | if (alg[0] !== 'hmac' && !PK_ALGOS.has(alg[0])) { |
| 45 | throw new InvalidAlgorithmError(alg[0].toUpperCase() + ' type keys ' + 'are not supported') |
| 46 | } |
| 47 | |
| 48 | if (!HASH_ALGOS.has(alg[1])) { |
| 49 | throw new InvalidAlgorithmError(alg[1].toUpperCase() + ' is not a ' + 'supported hash algorithm') |
| 50 | } |
| 51 | |
| 52 | return alg as [string, string] |
| 53 | } |