Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!senderPrivateKey) {
throw new Error("invalid senderPrivateKey");
}
const cryptoFactory = getCryptoFactory(this.securityPolicy);
if (!cryptoFactory) {
return null; // may be a not yet supported security Policy
}
assert(cryptoFactory, "expecting a cryptoFactory");
assert(_.isFunction(cryptoFactory.asymmetricSign));
const options: any = {};
options.signatureLength = rsa_length(senderPrivateKey);
options.signBufferFunc = (chunk: Buffer) => {
const s = cryptoFactory.asymmetricSign(chunk, senderPrivateKey);
assert(s.length === options.signatureLength);
return s;
};
if (!this.receiverPublicKey) {
throw new Error(" invalid receiverPublicKey");
}
const keyLength = rsa_length(this.receiverPublicKey);
options.plainBlockSize = keyLength - cryptoFactory.blockPaddingSize;
options.cipherBlockSize = keyLength;
const receiverPublicKey = this.receiverPublicKey;
options.encryptBufferFunc = (chunk: Buffer): Buffer => {
assert(self.receiverPublicKey);
assert(typeof self.receiverPublicKey === "string", "expecting a valid public key");
const cryptoFactory = securityPolicy_m.getCryptoFactory(self.securityPolicy);
if (!cryptoFactory) {
return null; // may be a not yet supported security Policy
}
assert(cryptoFactory, "expecting a cryptoFactory");
assert(_.isFunction(cryptoFactory.asymmetricSign));
const options = {};
options.signatureLength = crypto_utils.rsa_length(senderPrivateKey);
options.signingFunc = function (chunk) {
const s = cryptoFactory.asymmetricSign(chunk, senderPrivateKey);
assert(s.length === options.signatureLength);
return s;
};
assert(self.receiverPublicKey);
const keyLength = crypto_utils.rsa_length(self.receiverPublicKey);
options.plainBlockSize = keyLength - cryptoFactory.blockPaddingSize;
options.cipherBlockSize = keyLength;
options.encrypt_buffer = function (chunk) {
return cryptoFactory.asymmetricEncrypt(chunk, self.receiverPublicKey);
};
return options;
function RSAOAEP_Decrypt(buffer, privateKey) {
const block_size = crypto_utils.rsa_length(privateKey);
return crypto_utils.privateDecrypt_long(buffer, privateKey, block_size, crypto_utils.RSA_PKCS1_OAEP_PADDING);
}
// --------------------
}
assert(cryptoFactory, "expecting a cryptoFactory");
assert(_.isFunction(cryptoFactory.asymmetricSign));
const options = {};
options.signatureLength = crypto_utils.rsa_length(senderPrivateKey);
options.signingFunc = function (chunk) {
const s = cryptoFactory.asymmetricSign(chunk, senderPrivateKey);
assert(s.length === options.signatureLength);
return s;
};
assert(self.receiverPublicKey);
const keyLength = crypto_utils.rsa_length(self.receiverPublicKey);
options.plainBlockSize = keyLength - cryptoFactory.blockPaddingSize;
options.cipherBlockSize = keyLength;
options.encrypt_buffer = function (chunk) {
return cryptoFactory.asymmetricEncrypt(chunk, self.receiverPublicKey);
};
return options;
};
assert(_.isFunction(cryptoFactory.asymmetricSign));
const options: any = {};
options.signatureLength = rsa_length(senderPrivateKey);
options.signBufferFunc = (chunk: Buffer) => {
const s = cryptoFactory.asymmetricSign(chunk, senderPrivateKey);
assert(s.length === options.signatureLength);
return s;
};
if (!this.receiverPublicKey) {
throw new Error(" invalid receiverPublicKey");
}
const keyLength = rsa_length(this.receiverPublicKey);
options.plainBlockSize = keyLength - cryptoFactory.blockPaddingSize;
options.cipherBlockSize = keyLength;
const receiverPublicKey = this.receiverPublicKey;
options.encryptBufferFunc = (chunk: Buffer): Buffer => {
return cryptoFactory.asymmetricEncrypt(chunk, receiverPublicKey);
};
return options;
}
function RSAPKCS1V15SHA1_Sign(buffer: Buffer, privateKey: PrivateKeyPEM): Buffer {
assert(!((privateKey as any) instanceof Buffer), "privateKey should not be a Buffer but a PEM");
const params = {
algorithm: "RSA-SHA1",
privateKey,
signatureLength: rsa_length(privateKey),
};
return makeMessageChunkSignature(buffer, params);
}
function RSAPKCS1V15SHA1_Sign(buffer, privateKey) {
if (privateKey instanceof Buffer) {
privateKey = crypto_utils.toPem(privateKey, "RSA PRIVATE KEY");
}
const params = {
signatureLength: crypto_utils.rsa_length(privateKey),
algorithm: "RSA-SHA1",
privateKey: privateKey
};
return crypto_utils.makeMessageChunkSignature(buffer, params);
}
function RSAPKCS1V15SHA256_Sign(buffer, privateKey) {
if (privateKey instanceof Buffer) {
privateKey = crypto_utils.toPem(privateKey, "RSA PRIVATE KEY");
}
const params = {
signatureLength: crypto_utils.rsa_length(privateKey),
algorithm: "RSA-SHA256",
privateKey: privateKey
};
return crypto_utils.makeMessageChunkSignature(buffer, params);
}
function RSAPKCS1V15_Decrypt(buffer: Buffer, privateKey: PrivateKeyPEM): Buffer {
const blockSize = rsa_length(privateKey);
return privateDecrypt_long(buffer, privateKey, blockSize, RSA_PKCS1_PADDING);
}