Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_encryptMessage (data, sharedMacData = null) {
const privateKey = util.genPrivateKey()
const x = ecdhX(this._remotePublicKey, privateKey)
const key = concatKDF(x, 32)
const ekey = key.slice(0, 16) // encryption key
const mkey = crypto.createHash('sha256').update(key.slice(16, 32)).digest() // MAC key
// encrypt
const IV = crypto.randomBytes(16)
const cipher = crypto.createCipheriv('aes-128-ctr', ekey, IV)
const encryptedData = cipher.update(data)
const dataIV = Buffer.concat([ IV, encryptedData ])
// create tag
if (!sharedMacData) {
sharedMacData = Buffer.from([])
}
const tag = crypto.createHmac('sha256', mkey).update(Buffer.concat([dataIV, sharedMacData])).digest()
const publicKey = secp256k1.publicKeyCreate(privateKey, false)
return Buffer.concat([ publicKey, dataIV, tag ])
}
var buffer = Buffer.concat([
// Header
protocol.PUBLISH_HEADER[qos][opts.dup ? 1 : 0][retain ? 1 : 0],
// Remaining length
genBufLengthCached(length),
// Topic length
numCache[byteLength(topic)],
// Topic text
new Buffer(topic)])
// Message ID
if (qos > 0) buffer = Buffer.concat([buffer, numCache[id]])
// payload
buffer = Buffer.concat([buffer, new Buffer(payload)])
return stream.write(buffer)
}
merkleHash() {
return utils.sha3(Buffer.concat([this.hash(false), this.sig1, this.sig2]))
}
_transform(chunk, encoding, cb) {
this.buffer = Buffer.concat([this.buffer, chunk]);
this.updateTimer();
cb();
}
function keccak256 (...buffers) {
const buffer = Buffer.concat(buffers)
return createKeccakHash('keccak256').update(buffer).digest()
}
keyForTx(tx) {
return Buffer.concat([config.prefixes.txpool, tx.merkleHash()])
}
}
getMerkleProof(txIndex) {
if (txIndex < 0 || txIndex >= this.transactions.length) {
return null
}
const merkleHashes = this.transactions.map(tx => tx.merkleHash())
const tree = new FixedMerkleTree(16, merkleHashes)
return {
root: tree.getRoot(),
leaf: this.transactions[txIndex].merkleHash(),
proof: Buffer.concat(
tree.getPlasmaProof(this.transactions[txIndex].merkleHash())
)
}
}
}
private readFrameSize(): number {
if (this.chunks[0].length >= BitsUtil.INT_SIZE_IN_BYTES) {
return this.chunks[0].readInt32LE(0);
}
let readChunksSize = 0;
for (let i = 0; i < this.chunks.length; i++) {
readChunksSize += this.chunks[i].length;
if (readChunksSize >= BitsUtil.INT_SIZE_IN_BYTES) {
const merged = Buffer.concat(this.chunks.slice(0, i + 1), readChunksSize);
return merged.readInt32LE(0);
}
}
throw new Error('Detected illegal internal call in FrameReader!');
}
}
blobForSignature () {
return Buffer.concat([
Buffer.from(this.key),
this.value,
this.author.id
])
}