Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function performMessageChunkManagerTest(options: SecureMessageChunkManagerOptions) {
options = options || {};
const securityHeader = new SymmetricAlgorithmSecurityHeader();
const bodySize = 32;
const headerSize = 12 + securityHeader.binaryStoreSize();
options.signatureLength = options.signatureLength || 0; // 128 bytes for signature
options.chunkSize = bodySize + options.signatureLength + headerSize + 8; // bodySize useful bytes
options.requestId = 1;
const sequenceNumberGenerator = new SequenceNumberGenerator();
const msgChunkManager = new SecureMessageChunkManager(
"HEL", options, securityHeader, sequenceNumberGenerator
);
const chunks: Buffer[] = [];
assert(_.isFunction(messageChunkCallback));
// calculate message size ( with its encodingDefaultBinary)
const binSize = message.binaryStoreSize() + 4;
const stream = new BinaryStream(binSize);
this._stream = stream;
encodeExpandedNodeId(message.schema.encodingDefaultBinary!, stream);
message.encode(stream);
let securityHeader;
if (msgType === "OPN") {
securityHeader = this.securityHeader;
} else {
securityHeader = new SymmetricAlgorithmSecurityHeader({tokenId: options.tokenId});
}
const chunkManager = new SecureMessageChunkManager(
msgType, options, securityHeader, this.sequenceNumberGenerator
);
chunkManager
.on("chunk", (messageChunk: Buffer) => {
messageChunkCallback(messageChunk);
})
.on("finished", () => {
messageChunkCallback(null);
});
chunkManager.write(stream.buffer, stream.buffer.length);
assert(_.isFunction(messageChunkCallback));
// calculate message size ( with its encodingDefaultBinary)
const binSize = message.binaryStoreSize() + 4;
const stream = new BinaryStream(binSize);
this._stream = stream;
ec.encodeExpandedNodeId(message.encodingDefaultBinary, stream);
message.encode(stream);
let securityHeader;
if (msgType === "OPN") {
securityHeader = this.securityHeader;
} else {
securityHeader = new SymmetricAlgorithmSecurityHeader({tokenId: options.tokenId});
}
const secure_chunker = new SecureMessageChunkManager(
msgType, options, securityHeader, this.sequenceNumberGenerator
)
.on("chunk", function (messageChunk) {
messageChunkCallback(messageChunk);
})
.on("finished", function () {
messageChunkCallback(null);
});
secure_chunker.write(stream._buffer, stream._buffer.length);
secure_chunker.end();
};
chunkSize: 1024,
encryptBufferFunc: null,
plainBlockSize: 0,
requestId: 10,
signBufferFunc: null,
signatureLength: 0,
};
options.signatureLength = derivedKeys.signatureLength;
options.signBufferFunc = (chunk: Buffer) => makeMessageChunkSignatureWithDerivedKeys(chunk, derivedKeys);
options.plainBlockSize = derivedKeys.encryptingBlockSize;
options.cipherBlockSize = derivedKeys.encryptingBlockSize;
options.encryptBufferFunc = (chunk: Buffer) => encryptBufferWithDerivedKeys(chunk, derivedKeys);
const securityHeader = new SymmetricAlgorithmSecurityHeader({
tokenId: 10
});
const msgChunkManager = new SecureMessageChunkManager("MSG", options, securityHeader, sequenceNumberGenerator);
msgChunkManager.on("chunk", (chunk, final) => callback(null, chunk));
msgChunkManager.write(buffer, buffer.length);
msgChunkManager.end();
}
const options = {
requestId: 10,
chunkSize: 1024
};
options.signatureLength = derivedKeys.signatureLength;
options.signingFunc = function (chunk) {
return crypto_utils.makeMessageChunkSignatureWithDerivedKeys(chunk, derivedKeys);
};
options.plainBlockSize = derivedKeys.encryptingBlockSize;
options.cipherBlockSize = derivedKeys.encryptingBlockSize;
options.encrypt_buffer = function (chunk) {
return crypto_utils.encryptBufferWithDerivedKeys(chunk, derivedKeys);
};
const securityHeader = new SymmetricAlgorithmSecurityHeader({
tokenId: 10
});
const msgChunkManager = new SecureMessageChunkManager("MSG", options, securityHeader, sequenceNumberGenerator);
msgChunkManager.on("chunk", function (chunk, final) {
callback(null, chunk);
});
msgChunkManager.write(buffer, buffer.length);
msgChunkManager.end();
}
exports.iterate_on_symmetric_encrypted_chunk = iterate_on_symmetric_encrypted_chunk;
export function chooseSecurityHeader(msgType: string): SecurityHeader {
return msgType === "OPN" ? new AsymmetricAlgorithmSecurityHeader() : new SymmetricAlgorithmSecurityHeader();
}
function chooseSecurityHeader(msgType) {
const securityHeader =
msgType === "OPN" ? new AsymmetricAlgorithmSecurityHeader() : new SymmetricAlgorithmSecurityHeader();
return securityHeader;
}