Skip to content

Commit 60adf74

Browse files
authored
Merge pull request #81 from frzsombor/token-patch-1
Token generation fixes, cleanup, and making things more clear
2 parents a7c8429 + 952298a commit 60adf74

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

src/Stichoza/GoogleTranslate/Tokens/GoogleTokenGenerator.php

+34-25
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ private function TL($a)
5050
$d[$e++] = $g >> 12 & 63 | 128;
5151
} else {
5252
$d[$e++] = $g >> 12 | 224;
53-
$d[$e++] = $g >> 6 & 63 | 128;
5453
}
54+
$d[$e++] = $g >> 6 & 63 | 128;
5555
}
5656
$d[$e++] = $g & 63 | 128;
5757
}
@@ -62,7 +62,7 @@ private function TL($a)
6262
$a = $this->RL($a, '+-a^+6');
6363
}
6464
$a = $this->RL($a, '+-3^+b+-f');
65-
$a ^= $tkk[1];
65+
$a ^= $tkk[1] ? $tkk[1] + 0 : 0;
6666
if (0 > $a) {
6767
$a = ($a & 2147483647) + 2147483648;
6868
}
@@ -81,51 +81,60 @@ private function TKK()
8181

8282
/**
8383
* Process token data by applying multiple operations.
84+
* (Params are safe, no need for multibyte functions)
8485
*
85-
* @param $a
86-
* @param $b
86+
* @param int $a
87+
* @param string $b
8788
*
8889
* @return int
8990
*/
9091
private function RL($a, $b)
9192
{
9293
for ($c = 0; $c < strlen($b) - 2; $c += 3) {
93-
$d = $b{$c + 2};
94-
$d = $d >= 'a' ? $this->charCodeAt($d, 0) - 87 : intval($d);
95-
$d = $b{$c + 1}
96-
== '+' ? $this->shr32($a, $d) : $a << $d;
97-
$a = $b{$c}
98-
== '+' ? ($a + $d & 4294967295) : $a ^ $d;
94+
$d = $b[$c + 2];
95+
$d = 'a' <= $d ? ord($d[0]) - 87 : intval($d);
96+
$d = '+' == $b[$c + 1] ? $this->unsignedRightShift($a, $d) : $a << $d;
97+
$a = '+' == $b[$c] ? ($a + $d & 4294967295) : $a ^ $d;
9998
}
10099

101100
return $a;
102101
}
103102

104103
/**
105-
* Crypto function.
104+
* Unsigned right shift implementation
105+
* https://msdn.microsoft.com/en-us/library/342xfs5s(v=vs.94).aspx
106+
* http://stackoverflow.com/a/43359819/2953830
106107
*
107-
* @param $x
108-
* @param $bits
108+
* @param $a
109+
* @param $b
109110
*
110111
* @return number
111112
*/
112-
private function shr32($x, $bits)
113+
private function unsignedRightShift($a, $b)
113114
{
114-
if ($bits <= 0) {
115-
return $x;
115+
if ($b >= 32 || $b < -32) {
116+
$m = (int)($b / 32);
117+
$b = $b - ($m * 32);
116118
}
117-
if ($bits >= 32) {
118-
return 0;
119+
120+
if ($b < 0) {
121+
$b = 32 + $b;
119122
}
120-
$bin = decbin($x);
121-
$l = strlen($bin);
122-
if ($l > 32) {
123-
$bin = substr($bin, $l - 32, 32);
124-
} elseif ($l < 32) {
125-
$bin = str_pad($bin, 32, '0', STR_PAD_LEFT);
123+
124+
if ($b == 0) {
125+
return (($a >> 1) & 0x7fffffff) * 2 + (($a >> $b) & 1);
126+
}
127+
128+
if ($a < 0) {
129+
$a = ($a >> 1);
130+
$a &= 2147483647;
131+
$a |= 0x40000000;
132+
$a = ($a >> ($b - 1));
133+
} else {
134+
$a = ($a >> $b);
126135
}
127136

128-
return bindec(str_pad(substr($bin, 0, 32 - $bits), 32, '0', STR_PAD_LEFT));
137+
return $a;
129138
}
130139

131140
/**

0 commit comments

Comments
 (0)