Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/WASignalGroup/sender_chain_key.js
1126 views
1
const SenderMessageKey = require('./sender_message_key');
2
//const HKDF = require('./hkdf');
3
const crypto = require('libsignal/src/crypto');
4
5
class SenderChainKey {
6
MESSAGE_KEY_SEED = Buffer.from([0x01]);
7
8
CHAIN_KEY_SEED = Buffer.from([0x02]);
9
10
iteration = 0;
11
12
chainKey = Buffer.alloc(0);
13
14
constructor(iteration, chainKey) {
15
this.iteration = iteration;
16
this.chainKey = chainKey;
17
}
18
19
getIteration() {
20
return this.iteration;
21
}
22
23
getSenderMessageKey() {
24
return new SenderMessageKey(
25
this.iteration,
26
this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey)
27
);
28
}
29
30
getNext() {
31
return new SenderChainKey(
32
this.iteration + 1,
33
this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey)
34
);
35
}
36
37
getSeed() {
38
return typeof this.chainKey === 'string' ? Buffer.from(this.chainKey, 'base64') : this.chainKey;
39
}
40
41
getDerivative(seed, key) {
42
key = typeof key === 'string' ? Buffer.from(key, 'base64') : key;
43
const hash = crypto.calculateMAC(key, seed);
44
//const hash = new Hash().hmac_hash(key, seed, 'sha256', '');
45
46
return hash;
47
}
48
}
49
50
module.exports = SenderChainKey;
51