forked from basharmadi/node-whatsapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.js
58 lines (48 loc) · 1.78 KB
/
encryption.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var crypto = require('crypto');
var rc4 = require('./rc4');
function KeyStream(key, macKey) {
//this.cipher = crypto.createCipheriv('rc4', key, new Buffer(''));
//this.key = key;
this.seq=0;
this.macKey = macKey;
var drop = 0x300; //768
this.rc4engine = new rc4.Engine();
this.rc4engine.init(key);
this.rc4engine.drop(0x300);
}
KeyStream.prototype.computeMac = function(buffer, offset, length){
// $hmac = hash_init("sha1", HASH_HMAC, $this->macKey);
// hash_update($hmac, substr($buffer, $offset, $length));
// $array = chr($this->seq >> 24)
// . chr($this->seq >> 16)
// . chr($this->seq >> 8)
// . chr($this->seq);
// hash_update($hmac, $array);
// $this->seq++;
// return hash_final($hmac, true);
var hmac = crypto.createHmac('sha1', this.macKey);
hmac.update(buffer.slice(offset,offset+length));
var updateBuffer = new Buffer([this.seq >> 24, (this.seq >> 16)%256, (this.seq >> 8)%256, (this.seq)%256]);
hmac.update(updateBuffer);
this.seq++;
return hmac.digest();
};
//WAUTH-2
KeyStream.prototype.encodeMessage = function(buffer, macOffset, offset, length){
var data = this.rc4engine.cipher(buffer, offset, length);
var mac = this.computeMac(data, offset, length);
return Buffer.concat( [data.slice(0, macOffset), mac.slice(0,4), data.slice(macOffset + 4)] );
};
//WAUTH-2
KeyStream.prototype.decodeMessage = function(buffer, macOffset, offset, length){
var mac = this.computeMac(buffer, offset, length);
var decoded = this.rc4engine.cipher(buffer, offset, length);
return decoded.slice(0, length);
};
function pbkdf2(password, salt, iterations, length) {
iterations = iterations || 16;
length = length || 20;
return crypto.pbkdf2Sync(password, salt, iterations, length);
}
exports.KeyStream = KeyStream;
exports.pbkdf2 = pbkdf2;