Skip to content

Commit

Permalink
Merge pull request #4 from baibaratsky/test
Browse files Browse the repository at this point in the history
Fixed #3: “Maximum function nesting level of '100' reached, aborting!” when using BCMath
  • Loading branch information
baibaratsky committed Apr 24, 2015
2 parents 13d316b + cd14821 commit 7a5b3a7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
4 changes: 2 additions & 2 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Webmoney API PHP library is free software. It is released under
the terms of the following BSD License.
WebMoney Signer is free software. It is released under the terms
of the following BSD License.

Copyright © 2013-2015 by Andrei Baibaratsky. All rights reserved.

Expand Down
67 changes: 36 additions & 31 deletions Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace baibaratsky\WebMoney;

/**
* WebMoney Signer: a native PHP implementation of the WMSigner module
* @package baibaratsky\WebMoney
*/
class Signer
{
private $power;
private $modulus;

/**
* Create RequestSigner object
*
* @param string $wmid Signer WMID
* @param string $keyFileName Full path to the key file
* @param string $keyPassword Key file password
Expand Down Expand Up @@ -42,7 +44,7 @@ public function __construct($wmid, $keyFileName, $keyPassword)
}

/**
* Create signature for given data
* Create a signature for the given data
*
* @param string $data
*
Expand All @@ -58,7 +60,7 @@ public function sign($data)
$base .= pack('V', mt_rand());
}

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

// Modular exponentiation
Expand All @@ -80,7 +82,7 @@ public function sign($data)
}

/**
* Initialize power and modulus
* Initialize the power and the modulus
*
* @param string $keyBuffer
*/
Expand All @@ -95,7 +97,7 @@ private function initSignVariables($keyBuffer)
}

/**
* Encrypt key using hash of WMID and key password
* Encrypt the key using the hash of the WMID and the key password
*
* @param string $keyBuffer
* @param string $wmid
Expand All @@ -111,7 +113,7 @@ private static function encryptKey($keyBuffer, $wmid, $keyPassword)
}

/**
* XOR subject with modifier
* XOR operation on two strings
*
* @param string $subject
* @param string $modifier
Expand All @@ -136,9 +138,9 @@ private static function xorStrings($subject, $modifier, $shift = 0)
}

/**
* Verify hash of the key
* Verify the hash of the key
*
* @param $keyData
* @param array $keyData
*
* @return bool
*/
Expand Down Expand Up @@ -167,9 +169,9 @@ private static function reverseToDecimal($binaryData)
}

/**
* Convert hexadecimal string to decimal string
* Convert a hexadecimal string to a decimal one
*
* @param $hex
* @param string $hex
*
* @return string
*/
Expand All @@ -183,29 +185,31 @@ private static function hex2dec($hex)
}

/**
* Convert hexadecimal string to decimal string using BCMath
* Convert a hexadecimal string to a decimal one using BCMath
*
* @param $hex
* @param string $hex
*
* @return string
*/
private static function hex2decBC($hex) {
if (strlen($hex) == 1) {
return (string)hexdec($hex);
}

$last = substr($hex, -1);
$rest = substr($hex, 0, -1);

return bcadd(
(string)hexdec($last),
bcmul('16', self::hex2decBC($rest), 0),
$dec = '0';
$len = strlen($hex);
for ($i = 1; $i <= $len; $i++) {
$dec = bcadd(
$dec,
bcmul(
strval(hexdec($hex[$i - 1])),
bcpow('16', strval($len - $i), 0),
0
),
0
);
);
}
return $dec;
}

/**
* Convert decimal string to hexadecimal string
* Convert a decimal string to a hexadecimal one
*
* @param string $dec
*
Expand All @@ -221,20 +225,21 @@ private static function dec2hex($dec)
}

/**
* Convert decimal string to hexadecimal string using BCMath
* Convert a decimal string to a hexadecimal one using BCMath
*
* @param string $dec
*
* @return string
*/
private static function dec2hexBC($dec) {
$remainder = bcmod($dec, '16');
$quotient = bcdiv(bcsub($dec, $remainder, 0), '16', 0);
$hex = '';

if ($quotient == 0) {
return dechex($remainder);
while ($dec) {
$modulus = bcmod($dec, '16');
$hex = dechex($modulus) . $hex;
$dec = bcdiv(bcsub($dec, $modulus, 0), '16', 0);
}

return self::dec2hexBC($quotient) . dechex($remainder);
return $hex;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "baibaratsky/php-wmsigner",
"description": "WebMoney Signer",
"description": "WebMoney Signer: a native PHP implementation of the WMSigner module",
"keywords": ["webmoney", "signer", "wmsigner", "WMXI"],
"license": "BSD-3-Clause",
"homepage": "http://github.com/baibaratsky/php-wmsigner",
Expand Down

0 comments on commit 7a5b3a7

Please sign in to comment.