11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst {
41cb0ef41Sopenharmony_ci  ObjectKeys,
51cb0ef41Sopenharmony_ci  StringPrototypeToLowerCase,
61cb0ef41Sopenharmony_ci} = primordials;
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst kHashContextNode = 1;
91cb0ef41Sopenharmony_ciconst kHashContextWebCrypto = 2;
101cb0ef41Sopenharmony_ciconst kHashContextJwkRsa = 3;
111cb0ef41Sopenharmony_ciconst kHashContextJwkRsaPss = 4;
121cb0ef41Sopenharmony_ciconst kHashContextJwkRsaOaep = 5;
131cb0ef41Sopenharmony_ciconst kHashContextJwkHmac = 6;
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// WebCrypto and JWK use a bunch of different names for the
161cb0ef41Sopenharmony_ci// standard set of SHA-* digest algorithms... which is ... fun.
171cb0ef41Sopenharmony_ci// Here we provide a utility for mapping between them in order
181cb0ef41Sopenharmony_ci// make it easier in the code.
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst kHashNames = {
211cb0ef41Sopenharmony_ci  sha1: {
221cb0ef41Sopenharmony_ci    [kHashContextNode]: 'sha1',
231cb0ef41Sopenharmony_ci    [kHashContextWebCrypto]: 'SHA-1',
241cb0ef41Sopenharmony_ci    [kHashContextJwkRsa]: 'RS1',
251cb0ef41Sopenharmony_ci    [kHashContextJwkRsaPss]: 'PS1',
261cb0ef41Sopenharmony_ci    [kHashContextJwkRsaOaep]: 'RSA-OAEP',
271cb0ef41Sopenharmony_ci    [kHashContextJwkHmac]: 'HS1',
281cb0ef41Sopenharmony_ci  },
291cb0ef41Sopenharmony_ci  sha256: {
301cb0ef41Sopenharmony_ci    [kHashContextNode]: 'sha256',
311cb0ef41Sopenharmony_ci    [kHashContextWebCrypto]: 'SHA-256',
321cb0ef41Sopenharmony_ci    [kHashContextJwkRsa]: 'RS256',
331cb0ef41Sopenharmony_ci    [kHashContextJwkRsaPss]: 'PS256',
341cb0ef41Sopenharmony_ci    [kHashContextJwkRsaOaep]: 'RSA-OAEP-256',
351cb0ef41Sopenharmony_ci    [kHashContextJwkHmac]: 'HS256',
361cb0ef41Sopenharmony_ci  },
371cb0ef41Sopenharmony_ci  sha384: {
381cb0ef41Sopenharmony_ci    [kHashContextNode]: 'sha384',
391cb0ef41Sopenharmony_ci    [kHashContextWebCrypto]: 'SHA-384',
401cb0ef41Sopenharmony_ci    [kHashContextJwkRsa]: 'RS384',
411cb0ef41Sopenharmony_ci    [kHashContextJwkRsaPss]: 'PS384',
421cb0ef41Sopenharmony_ci    [kHashContextJwkRsaOaep]: 'RSA-OAEP-384',
431cb0ef41Sopenharmony_ci    [kHashContextJwkHmac]: 'HS384',
441cb0ef41Sopenharmony_ci  },
451cb0ef41Sopenharmony_ci  sha512: {
461cb0ef41Sopenharmony_ci    [kHashContextNode]: 'sha512',
471cb0ef41Sopenharmony_ci    [kHashContextWebCrypto]: 'SHA-512',
481cb0ef41Sopenharmony_ci    [kHashContextJwkRsa]: 'RS512',
491cb0ef41Sopenharmony_ci    [kHashContextJwkRsaPss]: 'PS512',
501cb0ef41Sopenharmony_ci    [kHashContextJwkRsaOaep]: 'RSA-OAEP-512',
511cb0ef41Sopenharmony_ci    [kHashContextJwkHmac]: 'HS512',
521cb0ef41Sopenharmony_ci  },
531cb0ef41Sopenharmony_ci};
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci{
561cb0ef41Sopenharmony_ci  // Index the aliases
571cb0ef41Sopenharmony_ci  const keys = ObjectKeys(kHashNames);
581cb0ef41Sopenharmony_ci  for (let n = 0; n < keys.length; n++) {
591cb0ef41Sopenharmony_ci    const contexts = ObjectKeys(kHashNames[keys[n]]);
601cb0ef41Sopenharmony_ci    for (let i = 0; i < contexts.length; i++) {
611cb0ef41Sopenharmony_ci      const alias =
621cb0ef41Sopenharmony_ci        StringPrototypeToLowerCase(kHashNames[keys[n]][contexts[i]]);
631cb0ef41Sopenharmony_ci      if (kHashNames[alias] === undefined)
641cb0ef41Sopenharmony_ci        kHashNames[alias] = kHashNames[keys[n]];
651cb0ef41Sopenharmony_ci    }
661cb0ef41Sopenharmony_ci  }
671cb0ef41Sopenharmony_ci}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_cifunction normalizeHashName(name, context = kHashContextNode) {
701cb0ef41Sopenharmony_ci  if (typeof name !== 'string')
711cb0ef41Sopenharmony_ci    return name;
721cb0ef41Sopenharmony_ci  name = StringPrototypeToLowerCase(name);
731cb0ef41Sopenharmony_ci  const alias = kHashNames[name] && kHashNames[name][context];
741cb0ef41Sopenharmony_ci  return alias || name;
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_cinormalizeHashName.kContextNode = kHashContextNode;
781cb0ef41Sopenharmony_cinormalizeHashName.kContextWebCrypto = kHashContextWebCrypto;
791cb0ef41Sopenharmony_cinormalizeHashName.kContextJwkRsa = kHashContextJwkRsa;
801cb0ef41Sopenharmony_cinormalizeHashName.kContextJwkRsaPss = kHashContextJwkRsaPss;
811cb0ef41Sopenharmony_cinormalizeHashName.kContextJwkRsaOaep = kHashContextJwkRsaOaep;
821cb0ef41Sopenharmony_cinormalizeHashName.kContextJwkHmac = kHashContextJwkHmac;
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_cimodule.exports = normalizeHashName;
85