Skip to content

Commit

Permalink
Fixed #7 the mbstring.func_overload issue
Browse files Browse the repository at this point in the history
  • Loading branch information
baibaratsky committed Jan 10, 2016
1 parent 2684777 commit 8911557
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
class Signer
{
const MB_ENCODING = '8bit'; // 8bit is the fastest suitable encoding for mb_* methods

private $power;
private $modulus;

Expand Down Expand Up @@ -39,7 +41,12 @@ public function __construct($wmid, $keyFileName, $keyPassword)

if ($keyBuffer === false) {
// Try one more time using only the first half of the password
$keyPassword = substr($keyPassword, 0, ceil(strlen($keyPassword) / 2));
$keyPassword = mb_substr(
$keyPassword,
0,
ceil(mb_strlen($keyPassword, self::MB_ENCODING) / 2),
self::MB_ENCODING
);
$keyBuffer = self::readKeyBuffer($keyData, $wmid, $keyPassword);

if ($keyBuffer === false) {
Expand Down Expand Up @@ -68,7 +75,7 @@ public function sign($data)
}

// Add the length of the base (56 = 16 + 40) as the first 2 bytes
$base = pack('v', strlen($base)) . $base;
$base = pack('v', mb_strlen($base, self::MB_ENCODING)) . $base;

// Modular exponentiation
$dec = bcpowmod(self::reverseToDecimal($base), $this->power, $this->modulus, 0);
Expand All @@ -77,15 +84,15 @@ public function sign($data)
$hex = self::dec2hex($dec);

// Fill empty bytes with zeros
$hex = str_repeat('0', 132 - strlen($hex)) . $hex;
$hex = str_repeat('0', 132 - mb_strlen($hex, self::MB_ENCODING)) . $hex;

// Reverse byte order
$hexReversed = '';
for ($i = 0; $i < strlen($hex) / 4; ++$i) {
$hexReversed = substr($hex, $i * 4, 4) . $hexReversed;
for ($i = 0; $i < mb_strlen($hex, self::MB_ENCODING) / 4; ++$i) {
$hexReversed = mb_substr($hex, $i * 4, 4, self::MB_ENCODING) . $hexReversed;
}

return strtolower($hexReversed);
return mb_strtolower($hexReversed, self::MB_ENCODING);
}

/**
Expand Down Expand Up @@ -146,10 +153,10 @@ private static function encryptKey($keyBuffer, $wmid, $keyPassword)
*/
private static function xorStrings($subject, $modifier, $shift = 0)
{
$modifierLength = strlen($modifier);
$modifierLength = mb_strlen($modifier, self::MB_ENCODING);
$i = $shift;
$j = 0;
while ($i < strlen($subject)) {
while ($i < mb_strlen($subject, self::MB_ENCODING)) {
$subject[$i] = chr(ord($subject[$i]) ^ ord($modifier[$j]));
++$i;
if (++$j >= $modifierLength) {
Expand Down Expand Up @@ -216,7 +223,7 @@ private static function hex2dec($hex)
*/
private static function hex2decBC($hex) {
$dec = '0';
$len = strlen($hex);
$len = mb_strlen($hex, self::MB_ENCODING);
for ($i = 1; $i <= $len; $i++) {
$dec = bcadd(
$dec,
Expand Down

0 comments on commit 8911557

Please sign in to comment.