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 checkLib() {
if (typeof (elliptic) === "undefined") {
throw new core.OperationError("Cannot implement EC mechanism. Add 'https://peculiarventures.github.io/pv-webcrypto-tests/src/elliptic.js' script to your project");
}
}
protected static getNamedCurve(wcNamedCurve: string) {
const crv = wcNamedCurve.toUpperCase();
let res = "";
if (["P-256", "P-384", "P-521"].indexOf(crv) > -1) {
res = crv.replace("-", "").toLowerCase();
} else if (crv === "K-256") {
res = "secp256k1";
} else {
throw new core.OperationError(`Unsupported named curve '${wcNamedCurve}'`);
}
return res;
}
res = "SHA1_RSA_PKCS";
break;
case "SHA-224":
res = "SHA224_RSA_PKCS";
break;
case "SHA-256":
res = "SHA256_RSA_PKCS";
break;
case "SHA-384":
res = "SHA384_RSA_PKCS";
break;
case "SHA-512":
res = "SHA512_RSA_PKCS";
break;
default:
throw new core.OperationError(`Cannot create PKCS11 mechanism from algorithm '${keyAlg.hash.name}'`);
}
return { name: res, params: null };
}
} else {
const asnKey = JsonParser.fromJSON(keyData, { targetSchema: asn.RsaPublicKey });
return this.importPublicKey(asnKey, algorithm, extractable, keyUsages);
}
case "spki": {
const keyInfo = AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), asn.PublicKeyInfo);
const asnKey = AsnParser.parse(keyInfo.publicKey, asn.RsaPublicKey);
return this.importPublicKey(asnKey, algorithm, extractable, keyUsages);
}
case "pkcs8": {
const keyInfo = AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), asn.PrivateKeyInfo);
const asnKey = AsnParser.parse(keyInfo.privateKey, asn.RsaPrivateKey);
return this.importPrivateKey(asnKey, algorithm, extractable, keyUsages);
}
default:
throw new core.OperationError("format: Must be 'jwk', 'pkcs8' or 'spki'");
}
}
public static async encrypt(algorithm: Algorithm, key: AesCryptoKey, data: Uint8Array): Promise {
switch (algorithm.name.toUpperCase()) {
case "AES-CBC":
return this.encryptAesCBC(algorithm as AesCbcParams, key, Buffer.from(data));
case "AES-CTR":
return this.encryptAesCTR(algorithm as AesCtrParams, key, Buffer.from(data));
case "AES-GCM":
return this.encryptAesGCM(algorithm as AesGcmParams, key, Buffer.from(data));
case "AES-KW":
return this.encryptAesKW(algorithm as AesKeyAlgorithm, key, Buffer.from(data));
case "AES-ECB":
return this.encryptAesECB(algorithm as AesKeyAlgorithm, key, Buffer.from(data));
default:
throw new core.OperationError("algorithm: Is not recognized");
}
}
public static checkLib() {
if (typeof(des) === "undefined") {
throw new core.OperationError("Cannot implement DES mechanism. Add 'https://peculiarventures.github.io/pv-webcrypto-tests/src/des.js' script to your project");
}
}
public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise {
if (format === "raw") {
const key = new PbkdfCryptoKey();
key.data = Buffer.from(keyData as ArrayBuffer);
key.algorithm = { name: this.name };
key.extractable = false;
key.usages = keyUsages;
return key;
}
throw new core.OperationError("format: Must be 'raw'");
}
public static async exportKey(format: string, key: AesCryptoKey): Promise {
this.checkLib();
switch (format) {
case "jwk":
return key.toJSON();
case "raw":
return key.raw.buffer;
default:
throw new core.OperationError("format: Must be 'jwk' or 'raw'");
}
}
public static checkLib() {
if (typeof(asmCrypto) === "undefined") {
throw new core.OperationError("Cannot implement DES mechanism. Add 'https://peculiarventures.github.io/pv-webcrypto-tests/src/asmcrypto.js' script to your project");
}
}