forked from domingopa/WhatsAPI-Official
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeystream.class.php
More file actions
executable file
·82 lines (72 loc) · 2.19 KB
/
Copy pathkeystream.class.php
File metadata and controls
executable file
·82 lines (72 loc) · 2.19 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* Created by JetBrains PhpStorm.
* User: max
* Date: 29-1-14
* Time: 11:55
* To change this template use File | Settings | File Templates.
*/
require_once 'rc4.php';
require_once 'func.php';
class KeyStream
{
public static $AuthMethod = 'WAUTH-2';
const DROP = 768;
private $rc4;
private $seq = 0;
private $macKey;
//key = password, mackey = challengedata
public function __construct($key, $macKey)
{
$this->rc4 = new rc4($key, self::DROP);
$this->macKey = $macKey;
}
public static function GenerateKeys($password, $nonce)
{
$array = [
'key', //placeholders
'key',
'key',
'key',
];
$array2 = [1, 2, 3, 4];
$nonce .= '0';
for ($j = 0; $j < count($array); $j++) {
$nonce[(strlen($nonce) - 1)] = chr($array2[$j]);
$foo = wa_pbkdf2('sha1', $password, $nonce, 2, 20, true);
$array[$j] = $foo;
}
return $array;
}
public function DecodeMessage($buffer, $macOffset, $offset, $length)
{
$mac = $this->computeMac($buffer, $offset, $length);
//validate mac
for ($i = 0; $i < 4; $i++) {
$foo = ord($buffer[$macOffset + $i]);
$bar = ord($mac[$i]);
if ($foo !== $bar) {
throw new Exception("MAC mismatch: $foo != $bar");
}
}
return $this->rc4->cipher($buffer, $offset, $length);
}
public function EncodeMessage($buffer, $macOffset, $offset, $length)
{
$data = $this->rc4->cipher($buffer, $offset, $length);
$mac = $this->computeMac($data, $offset, $length);
return substr($data, 0, $macOffset).substr($mac, 0, 4).substr($data, $macOffset + 4);
}
private function computeMac($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);
}
}