summaryrefslogtreecommitdiffhomepage
path: root/packages/server/src/helpers/iso/isoCrypto/mapCoseAlgToWebCryptoAlg.ts
blob: 542a14f60be890b8508804bdb341e6a6b1b0b3a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { SubtleCryptoAlg } from './structs.ts';
import { COSEALG } from '../../cose.ts';

/**
 * Convert a COSE alg ID into a corresponding string value that WebCrypto APIs expect
 */
export function mapCoseAlgToWebCryptoAlg(alg: COSEALG): SubtleCryptoAlg {
  if ([COSEALG.RS1].indexOf(alg) >= 0) {
    return 'SHA-1';
  } else if ([COSEALG.ES256, COSEALG.PS256, COSEALG.RS256].indexOf(alg) >= 0) {
    return 'SHA-256';
  } else if ([COSEALG.ES384, COSEALG.PS384, COSEALG.RS384].indexOf(alg) >= 0) {
    return 'SHA-384';
  } else if (
    [COSEALG.ES512, COSEALG.PS512, COSEALG.RS512, COSEALG.EdDSA].indexOf(alg) >=
      0
  ) {
    return 'SHA-512';
  }

  throw new Error(`Could not map COSE alg value of ${alg} to a WebCrypto alg`);
}