Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public static alg2jwk(alg: Algorithm) {
const hash = (alg as any).hash as Algorithm;
const hashSize = /(\d+)/.exec(hash.name)![1];
switch (alg.name!.toUpperCase()) {
case AlgorithmNames.RsaOAEP.toUpperCase():
return `RSA-OAEP${hashSize === "1" ? "" : `-${hashSize}`}`;
case AlgorithmNames.RsaPSS.toUpperCase():
return `PS${hashSize}`;
case AlgorithmNames.RsaSSA.toUpperCase():
return `RS${hashSize}`;
default:
throw new AlgorithmError(AlgorithmError.UNSUPPORTED_ALGORITHM, alg.name);
}
}
public static getDigest(name: string) {
switch (name) {
case "SHA-1":
return new asmCrypto.Sha1();
case "SHA-256":
return new asmCrypto.Sha256();
case "SHA-512":
return new asmCrypto.Sha512();
default:
throw new core.AlgorithmError("keyAlgorithm.hash: Is not recognized");
}
}
.then((raw) => {
let CryptoClass: typeof Core.BaseCrypto;
switch (derivedKeyType.name.toUpperCase()) {
case Core.AlgorithmNames.AesCBC:
case Core.AlgorithmNames.AesGCM:
case Core.AlgorithmNames.AesKW:
CryptoClass = AesCrypto;
break;
case Core.AlgorithmNames.Hmac:
CryptoClass = HmacCrypto;
break;
default:
throw new Core.AlgorithmError(Core.AlgorithmError.UNSUPPORTED_ALGORITHM, algorithm.name);
}
return CryptoClass.importKey("raw", Buffer.from(raw), derivedKeyType as any, extractable, keyUsages);
});
}
public get alg() {
switch (this.algorithm.name.toUpperCase()) {
case "AES-CBC":
return `A${this.algorithm.length}CBC`;
case "AES-CTR":
return `A${this.algorithm.length}CTR`;
case "AES-GCM":
return `A${this.algorithm.length}GCM`;
case "AES-KW":
return `A${this.algorithm.length}KW`;
case "AES-CMAC":
return `A${this.algorithm.length}CMAC`;
case "AES-ECB":
return `A${this.algorithm.length}ECB`;
default:
throw new core.AlgorithmError("Unsupported algorithm name");
}
}