Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
native.Key.importJwk(data, keyType, (err, key) => {
try {
if (err) {
reject(new WebCryptoError(`ImportKey: Cannot import key from JWK\n${err}`));
} else {
resolve(key);
}
} catch (e) {
reject(e);
}
});
break;
case "pkcs8":
case "spki":
if (!Buffer.isBuffer(keyData)) {
throw new WebCryptoError("ImportKey: keyData is not a Buffer");
}
let importFunction = native.Key.importSpki;
if (formatLC === "pkcs8") {
keyType = native.KeyType.PRIVATE;
importFunction = native.Key.importPkcs8;
}
importFunction(keyData as Buffer, (err, key) => {
try {
if (err) {
reject(new WebCryptoError(`ImportKey: Can not import key for ${format}\n${err.message}`));
} else {
resolve(key);
}
} catch (e) {
reject(e);
}
native.Key.generateRsa(size, nExp, (err, key) => {
try {
if (err) {
reject(new WebCryptoError(`Rsa: Can not generate new key\n${err.message}`));
} else {
const prvUsages = ["sign", "decrypt", "unwrapKey"]
.filter((usage) => keyUsages.some((keyUsage) => keyUsage === usage)) as KeyUsage[];
const pubUsages = ["verify", "encrypt", "wrapKey"]
.filter((usage) => keyUsages.some((keyUsage) => keyUsage === usage)) as KeyUsage[];
resolve({
privateKey: new CryptoKey(key, algorithm, "private", extractable, prvUsages),
publicKey: new CryptoKey(key, algorithm, "public", true, pubUsages),
});
}
} catch (e) {
reject(e);
}
});
});
nativeKey.RsaOaepEncDec(alg, data, label, type, (err, res) => {
if (err) {
reject(new WebCryptoError("NativeError: " + err));
} else {
resolve(res.buffer as ArrayBuffer);
}
});
});