react-native-quick-crypto
Version:
A fast implementation of Node's `crypto` module written in C/C++ JSI
93 lines (92 loc) • 3.04 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.HashContext = void 0;
exports.normalizeHashName = normalizeHashName;
let HashContext = exports.HashContext = /*#__PURE__*/function (HashContext) {
HashContext[HashContext["Node"] = 0] = "Node";
HashContext[HashContext["WebCrypto"] = 1] = "WebCrypto";
HashContext[HashContext["JwkRsa"] = 2] = "JwkRsa";
HashContext[HashContext["JwkRsaPss"] = 3] = "JwkRsaPss";
HashContext[HashContext["JwkRsaOaep"] = 4] = "JwkRsaOaep";
HashContext[HashContext["JwkHmac"] = 5] = "JwkHmac";
return HashContext;
}({}); // WebCrypto and JWK use a bunch of different names for the
// standard set of SHA-* digest algorithms... which is ... fun.
// Here we provide a utility for mapping between them in order
// make it easier in the code.
const kHashNames = {
sha1: {
[ ]: 'sha1',
[ ]: 'SHA-1',
[ ]: 'RS1',
[ ]: 'PS1',
[ ]: 'RSA-OAEP',
[ ]: 'HS1'
},
sha224: {
[ ]: 'sha224',
[ ]: 'SHA-224',
[ ]: 'RS224',
[ ]: 'PS224',
[ ]: 'RSA-OAEP-224',
[ ]: 'HS224'
},
sha256: {
[ ]: 'sha256',
[ ]: 'SHA-256',
[ ]: 'RS256',
[ ]: 'PS256',
[ ]: 'RSA-OAEP-256',
[ ]: 'HS256'
},
sha384: {
[ ]: 'sha384',
[ ]: 'SHA-384',
[ ]: 'RS384',
[ ]: 'PS384',
[ ]: 'RSA-OAEP-384',
[ ]: 'HS384'
},
sha512: {
[ ]: 'sha512',
[ ]: 'SHA-512',
[ ]: 'RS512',
[ ]: 'PS512',
[ ]: 'RSA-OAEP-512',
[ ]: 'HS512'
},
ripemd160: {
[ ]: 'ripemd160',
[ ]: 'RIPEMD-160'
}
};
{
// Index the aliases
const keys = Object.keys(kHashNames);
for (let n = 0; n < keys.length; n++) {
const contexts = Object.keys(kHashNames[keys[n]]);
for (let i = 0; i < contexts.length; i++) {
const alias = kHashNames[keys[n]][contexts[i]].toLowerCase();
if (kHashNames[alias] === undefined) kHashNames[alias] = kHashNames[keys[n]];
}
}
}
function normalizeHashName(algo, context = HashContext.Node) {
if (typeof algo !== 'undefined') {
if (typeof algo === 'object') {
algo = algo.name;
}
const normAlgo = algo.toString().toLowerCase();
try {
const alias = kHashNames[normAlgo][context];
if (alias) return alias;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (_e) {
/* empty */
}
}
throw new Error(`Invalid Hash Algorithm: ${algo}`);
}
//# sourceMappingURL=Hashnames.js.map