diff --git a/LICENSE.md b/LICENSE.md index 6a61825..adea165 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -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. diff --git a/Signer.php b/Signer.php index 3979d7b..3c339fe 100644 --- a/Signer.php +++ b/Signer.php @@ -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 @@ -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 * @@ -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 @@ -80,7 +82,7 @@ public function sign($data) } /** - * Initialize power and modulus + * Initialize the power and the modulus * * @param string $keyBuffer */ @@ -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 @@ -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 @@ -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 */ @@ -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 */ @@ -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 * @@ -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; } } diff --git a/composer.json b/composer.json index ad260f6..02649e5 100644 --- a/composer.json +++ b/composer.json @@ -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",