Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise((resolve, reject) => {
const alg = algorithm as Pbkdf2Params;
const nativeKey = baseKey.native as native.Pbkdf2Key;
const hash = Core.PrepareAlgorithm(alg.hash);
const salt = Buffer.from(Core.PrepareData(alg.salt!, "salt"));
// derive bits
nativeKey.deriveBits(this.wc2ssl(hash), salt, alg.iterations, length, (err, raw) => {
if (err) {
reject(err);
} else {
resolve(raw.buffer as ArrayBuffer);
}
});
});
}
.then(() => {
let res: Uint8Array;
switch (algorithm.name.toUpperCase()) {
case AlgorithmNames.AesECB:
const algECB = algorithm as AesEcbParams;
res = asmCrypto.AES_ECB.decrypt(data, key.key, !!algECB.padding) as Uint8Array;
break;
case AlgorithmNames.AesCBC:
const algCBC = algorithm as AesCbcParams;
res = asmCrypto.AES_CBC.decrypt(data, key.key, undefined, PrepareData(algCBC.iv as Uint8Array, "iv")) as Uint8Array;
break;
case AlgorithmNames.AesGCM:
const algGCM = algorithm as AesGcmParams;
algGCM.tagLength = algGCM.tagLength || 128;
let additionalData;
if (algGCM.additionalData) {
additionalData = PrepareData(algGCM.additionalData, "additionalData");
}
res = asmCrypto.AES_GCM.decrypt(data, key.key, algGCM.iv as Uint8Array, additionalData, algGCM.tagLength / 8) as Uint8Array;
break;
default:
throw new LinerError(AlgorithmError.UNSUPPORTED_ALGORITHM, algorithm.name);
}
return res.buffer;
});
}
switch (algorithm.name.toUpperCase()) {
case AlgorithmNames.AesECB:
const algECB = algorithm as AesEcbParams;
res = asmCrypto.AES_ECB.decrypt(data, key.key, !!algECB.padding) as Uint8Array;
break;
case AlgorithmNames.AesCBC:
const algCBC = algorithm as AesCbcParams;
res = asmCrypto.AES_CBC.decrypt(data, key.key, undefined, PrepareData(algCBC.iv as Uint8Array, "iv")) as Uint8Array;
break;
case AlgorithmNames.AesGCM:
const algGCM = algorithm as AesGcmParams;
algGCM.tagLength = algGCM.tagLength || 128;
let additionalData;
if (algGCM.additionalData) {
additionalData = PrepareData(algGCM.additionalData, "additionalData");
}
res = asmCrypto.AES_GCM.decrypt(data, key.key, algGCM.iv as Uint8Array, additionalData, algGCM.tagLength / 8) as Uint8Array;
break;
default:
throw new LinerError(AlgorithmError.UNSUPPORTED_ALGORITHM, algorithm.name);
}
return res.buffer;
});
}
switch ((keyAlg.hash as Algorithm).name.toUpperCase()) {
case AlgorithmNames.Sha1:
decrypt = asmCrypto.RSA_OAEP_SHA1.decrypt;
break;
case AlgorithmNames.Sha256:
decrypt = asmCrypto.RSA_OAEP_SHA256.decrypt;
break;
case AlgorithmNames.Sha512:
decrypt = asmCrypto.RSA_OAEP_SHA512.decrypt;
break;
default:
throw new LinerError(LinerError.UNSUPPORTED_ALGORITHM, `${keyAlg.name} ${(keyAlg.hash as Algorithm).name}`);
}
let label;
if (rsaAlg.label) {
label = PrepareData(rsaAlg.label, "label");
}
return decrypt(data, key.key, label);
default:
throw new LinerError(LinerError.UNSUPPORTED_ALGORITHM, algorithm.name);
}
});
}
.then(() => {
let res: Uint8Array;
switch (algorithm.name.toUpperCase()) {
case AlgorithmNames.AesECB:
const algECB = algorithm as AesEcbParams;
res = asmCrypto.AES_ECB.encrypt(data, key.key, !!algECB.padding) as Uint8Array;
break;
case AlgorithmNames.AesCBC:
const algCBC = algorithm as AesCbcParams;
res = asmCrypto.AES_CBC.encrypt(data, key.key, undefined, PrepareData(algCBC.iv as Uint8Array, "iv")) as Uint8Array;
break;
case AlgorithmNames.AesGCM:
const algGCM = algorithm as AesGcmParams;
algGCM.tagLength = algGCM.tagLength || 128;
let additionalData;
if (algGCM.additionalData) {
additionalData = PrepareData(algGCM.additionalData, "additionalData");
}
res = asmCrypto.AES_GCM.encrypt(data, key.key, algGCM.iv as Uint8Array, additionalData, algGCM.tagLength / 8) as Uint8Array;
break;
default:
throw new LinerError(AlgorithmError.UNSUPPORTED_ALGORITHM, algorithm.name);
}
return res.buffer;
});
}
let res: Uint8Array;
switch (algorithm.name.toUpperCase()) {
case AlgorithmNames.AesECB:
const algECB = algorithm as AesEcbParams;
res = asmCrypto.AES_ECB.encrypt(data, key.key, !!algECB.padding) as Uint8Array;
break;
case AlgorithmNames.AesCBC:
const algCBC = algorithm as AesCbcParams;
res = asmCrypto.AES_CBC.encrypt(data, key.key, undefined, PrepareData(algCBC.iv as Uint8Array, "iv")) as Uint8Array;
break;
case AlgorithmNames.AesGCM:
const algGCM = algorithm as AesGcmParams;
algGCM.tagLength = algGCM.tagLength || 128;
let additionalData;
if (algGCM.additionalData) {
additionalData = PrepareData(algGCM.additionalData, "additionalData");
}
res = asmCrypto.AES_GCM.encrypt(data, key.key, algGCM.iv as Uint8Array, additionalData, algGCM.tagLength / 8) as Uint8Array;
break;
default:
throw new LinerError(AlgorithmError.UNSUPPORTED_ALGORITHM, algorithm.name);
}
return res.buffer;
});
}