diff options
Diffstat (limited to 'vendor/stephenhill')
-rw-r--r-- | vendor/stephenhill/base58/.gitignore | 3 | ||||
-rw-r--r-- | vendor/stephenhill/base58/.travis.yml | 15 | ||||
-rw-r--r-- | vendor/stephenhill/base58/benchmarks/Base16Event.php | 24 | ||||
-rw-r--r-- | vendor/stephenhill/base58/benchmarks/Base58BCMathEvent.php | 33 | ||||
-rw-r--r-- | vendor/stephenhill/base58/benchmarks/Base58GMPEvent.php | 33 | ||||
-rw-r--r-- | vendor/stephenhill/base58/benchmarks/Base64Event.php | 24 | ||||
-rw-r--r-- | vendor/stephenhill/base58/composer.json | 24 | ||||
-rw-r--r-- | vendor/stephenhill/base58/contributing.md | 24 | ||||
-rw-r--r-- | vendor/stephenhill/base58/docs/index.md | 145 | ||||
-rw-r--r-- | vendor/stephenhill/base58/docs/navbar.md | 5 | ||||
-rw-r--r-- | vendor/stephenhill/base58/license | 21 | ||||
-rw-r--r-- | vendor/stephenhill/base58/phpunit.xml | 31 | ||||
-rw-r--r-- | vendor/stephenhill/base58/readme.md | 145 | ||||
-rw-r--r-- | vendor/stephenhill/base58/src/BCMathService.php | 165 | ||||
-rw-r--r-- | vendor/stephenhill/base58/src/Base58.php | 90 | ||||
-rw-r--r-- | vendor/stephenhill/base58/src/GMPService.php | 156 | ||||
-rw-r--r-- | vendor/stephenhill/base58/src/ServiceInterface.php | 24 |
17 files changed, 962 insertions, 0 deletions
diff --git a/vendor/stephenhill/base58/.gitignore b/vendor/stephenhill/base58/.gitignore new file mode 100644 index 000000000..b2df07bd9 --- /dev/null +++ b/vendor/stephenhill/base58/.gitignore @@ -0,0 +1,3 @@ +/vendor/ +/bin/ +composer.lock diff --git a/vendor/stephenhill/base58/.travis.yml b/vendor/stephenhill/base58/.travis.yml new file mode 100644 index 000000000..3e995f336 --- /dev/null +++ b/vendor/stephenhill/base58/.travis.yml @@ -0,0 +1,15 @@ +language: php + +before_script: + - composer install + +php: + - 5.3 + - 5.4 + - 5.5 + - 5.6 + - 7 + - 7.1 + - hhvm + +script: bin/phpunit diff --git a/vendor/stephenhill/base58/benchmarks/Base16Event.php b/vendor/stephenhill/base58/benchmarks/Base16Event.php new file mode 100644 index 000000000..f3ec0034d --- /dev/null +++ b/vendor/stephenhill/base58/benchmarks/Base16Event.php @@ -0,0 +1,24 @@ +<?php + +namespace StephenHill\Benchmarks; + +use Athletic\AthleticEvent; + +class Base16Event extends AthleticEvent +{ + /** + * @iterations 10000 + */ + public function encodeBase16() + { + bin2hex('Hello World'); + } + + /** + * @iterations 10000 + */ + public function decodeBase16() + { + pack('H*', '48656c6c6f20576f726c64'); + } +} diff --git a/vendor/stephenhill/base58/benchmarks/Base58BCMathEvent.php b/vendor/stephenhill/base58/benchmarks/Base58BCMathEvent.php new file mode 100644 index 000000000..fc8ec23ee --- /dev/null +++ b/vendor/stephenhill/base58/benchmarks/Base58BCMathEvent.php @@ -0,0 +1,33 @@ +<?php + +namespace StephenHill\Benchmarks; + +use Athletic\AthleticEvent; +use StephenHill\Base58; +use StephenHill\BCMathService; + +class Base58BCMathEvent extends AthleticEvent +{ + protected $base58; + + public function setUp() + { + $this->base58 = new Base58(null, new BCMathService()); + } + + /** + * @iterations 10000 + */ + public function encodeBase58() + { + $this->base58->encode('Hello World'); + } + + /** + * @iterations 10000 + */ + public function decodeBase58() + { + $this->base58->decode('JxF12TrwUP45BMd'); + } +} diff --git a/vendor/stephenhill/base58/benchmarks/Base58GMPEvent.php b/vendor/stephenhill/base58/benchmarks/Base58GMPEvent.php new file mode 100644 index 000000000..d8a4d20bf --- /dev/null +++ b/vendor/stephenhill/base58/benchmarks/Base58GMPEvent.php @@ -0,0 +1,33 @@ +<?php + +namespace StephenHill\Benchmarks; + +use Athletic\AthleticEvent; +use StephenHill\Base58; +use StephenHill\GMPService; + +class Base58GMPEvent extends AthleticEvent +{ + protected $base58; + + public function setUp() + { + $this->base58 = new Base58(null, new GMPService()); + } + + /** + * @iterations 10000 + */ + public function encodeBase58() + { + $this->base58->encode('Hello World'); + } + + /** + * @iterations 10000 + */ + public function decodeBase58() + { + $this->base58->decode('JxF12TrwUP45BMd'); + } +} diff --git a/vendor/stephenhill/base58/benchmarks/Base64Event.php b/vendor/stephenhill/base58/benchmarks/Base64Event.php new file mode 100644 index 000000000..a558968a3 --- /dev/null +++ b/vendor/stephenhill/base58/benchmarks/Base64Event.php @@ -0,0 +1,24 @@ +<?php + +namespace StephenHill\Benchmarks; + +use Athletic\AthleticEvent; + +class Base64Event extends AthleticEvent +{ + /** + * @iterations 10000 + */ + public function encodeBase64() + { + base64_encode('Hello World'); + } + + /** + * @iterations 10000 + */ + public function decodeBase64() + { + base64_decode('SGVsbG8gV29ybGQ='); + } +} diff --git a/vendor/stephenhill/base58/composer.json b/vendor/stephenhill/base58/composer.json new file mode 100644 index 000000000..560701f50 --- /dev/null +++ b/vendor/stephenhill/base58/composer.json @@ -0,0 +1,24 @@ +{ + "name": "stephenhill/base58", + "description": "Base58 Encoding and Decoding Library for PHP", + "require-dev": { + "phpunit/phpunit": "4.*", + "athletic/athletic": "~0.1" + }, + "license": "MIT", + "authors": [ + { + "name": "Stephen Hill", + "email": "stephen@gatekiller.co.uk" + } + ], + "config": { + "bin-dir": "bin" + }, + "autoload": { + "psr-4": { + "StephenHill\\": "src/", + "StephenHill\\Benchmarks\\": "benchmarks/" + } + } +}
\ No newline at end of file diff --git a/vendor/stephenhill/base58/contributing.md b/vendor/stephenhill/base58/contributing.md new file mode 100644 index 000000000..fbc33266d --- /dev/null +++ b/vendor/stephenhill/base58/contributing.md @@ -0,0 +1,24 @@ +# Contributing + +I welcome anyone to contribute to this project. All bug reports, feature requests, and pull requests are greatly appriciated. + +To contribute some code, please do the following: + +1. Fork this repository. +2. Create a new branch +3. Commit your changes to this branch. +4. Make sure your code follows the coding guidelines below. +5. When your happy with you changes, push this branch to Github. +6. Create a Pull Request into the master branch. + +## Coding Guidelines + +Please ensure your pull request adheres to the following: + +* New code should be properly tested, and all tests must pass +* PSR-1: Basic coding standard +* PSR-2: Coding style guide +* PSR-4: Autoloader +* Semantic Versioning 2.0.0 + +Thank you for your support!
\ No newline at end of file diff --git a/vendor/stephenhill/base58/docs/index.md b/vendor/stephenhill/base58/docs/index.md new file mode 100644 index 000000000..9723a91dd --- /dev/null +++ b/vendor/stephenhill/base58/docs/index.md @@ -0,0 +1,145 @@ +# Base58 Encoding and Decoding Library for PHP + +[![Build Status](https://travis-ci.org/stephen-hill/base58php.png)](https://travis-ci.org/stephen-hill/base58php) +[![Packagist Release](http://img.shields.io/packagist/v/stephenhill/base58.svg)](https://packagist.org/packages/stephenhill/base58) +[![MIT License](http://img.shields.io/packagist/l/stephenhill/base58.svg)](https://github.com/stephen-hill/base58php/blob/master/license) +[![Flattr this](https://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=stephen-hill&url=https%3A%2F%2Fgithub.com%2Fstephen-hill%2Fbase58php) + +## Long Term Support + +Each major version of this library will be supported for 5 years after it's initial release. Support will be provided for security and bug fixes. + +Version 1 will therefore be supported until the 11th September 2019. + +## Background + +I wanted a replacement for Base64 encoded strings and the [Base58 encoding used by Bitcoin](https://en.bitcoin.it/wiki/Base58Check_encoding) looked ideal. I looked around for an existing PHP library which would directly convert a string into Base58 but I couldn't find one, or at least one that worked correctly and was also well tested. + +So I decided to create a library with the following goals: + +- Encode/Decode PHP Strings +- Simple and easy to use +- Fully Tested +- Available via Composer + +## Requirements + +This library has the following requirements: + +- PHP => 5.3 +- BC Math Extension + +## Installation + +I recommend you install this library via Composer. + +```json +{ + "require": { + "stephenhill/base58": "~1.0" + } +} +``` + +## Basic Usage + +```php +require_once('vendor/autoload.php'); + +$base58 = new StephenHill\Base58(); + +$base58->encode('Hello World'); +$base58->decode('JxF12TrwUP45BMd'); +``` + +## Advanced Usage + +By default this library chooses the encoding service provider to use, either GMPService or BCMathService (in that order). +If you want to specify one of the included services or your own, you can inject it into the constructor. + +```php +require_once('vendor/autoload.php'); + +$gmp = new StephenHill\GMPService(); +$base58 = new StephenHill\Base58(null, $gmp); + +$base58->encode('Hello World'); +$base58->decode('JxF12TrwUP45BMd'); +``` + +Also by default, this library uses Bitcoin's Base58 alphabet. If you want to use another variant, you can do this in the constructor. + +```php +require_once('vendor/autoload.php'); + +// Flickr's Base58 Alphabet +$base58 = new StephenHill\Base58('123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'); + +$base58->encode('Hello World'); +$base58->decode('iXf12sRWto45bmC'); +``` + +## Testing + +This library is tested using PHPUnit. + +```bash +$ bin/phpunit +``` + +## Benchmarking + +You can benchmark this library using [Athletic](https://github.com/polyfractal/athletic). +The benchmarking suite also benchmarks PHP's built-in Base64 and Base16 encoding for comparison. + +```bash +$ bin/athletic -p benchmarks +``` + +Example output. + +``` +StephenHill\Benchmarks\Base16Event + Method Name Iterations Average Time Ops/second + ------------ ------------ -------------- ------------- + encodeBase16: [10,000 ] [0.0000010839939] [922,514.40637] + decodeBase16: [10,000 ] [0.0000011516809] [868,296.03561] + + +StephenHill\Benchmarks\Base58BCMathEvent + Method Name Iterations Average Time Ops/second + ------------ ------------ -------------- ------------- + encodeBase58: [10,000 ] [0.0001500048161] [6,666.45263] + decodeBase58: [10,000 ] [0.0001741812706] [5,741.14540] + + +StephenHill\Benchmarks\Base58GMPEvent + Method Name Iterations Average Time Ops/second + ------------ ------------ -------------- ------------- + encodeBase58: [10,000 ] [0.0001168665648] [8,556.76730] + decodeBase58: [10,000 ] [0.0001385705233] [7,216.54199] + + +StephenHill\Benchmarks\Base64Event + Method Name Iterations Average Time Ops/second + ------------ ------------ -------------- ------------- + encodeBase64: [10,000 ] [0.0000009050369] [1,104,927.29189] + decodeBase64: [10,000 ] [0.0000009787321] [1,021,730.04312] +``` + +## Contributing + +I welcome everyone to contribute to this library. Please see the Contributing document for details. + +## License + +This library is license under the MIT License (MIT). Please see License File for more information. + +## Credits + +This library was forked from [Jeremy Johnstone's](https://github.com/jsjohnst) Base58 methods on Gist https://gist.github.com/jsjohnst/126883. + +Some of the unit tests were based on the following: + +- https://code.google.com/p/bitcoinj/source/browse/core/src/test/java/com/google/bitcoin/core/Base58Test.java +- https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/fixtures/base58.json diff --git a/vendor/stephenhill/base58/docs/navbar.md b/vendor/stephenhill/base58/docs/navbar.md new file mode 100644 index 000000000..e11873d09 --- /dev/null +++ b/vendor/stephenhill/base58/docs/navbar.md @@ -0,0 +1,5 @@ +# Menu + +* [Home][home] + +[home]: /index.md diff --git a/vendor/stephenhill/base58/license b/vendor/stephenhill/base58/license new file mode 100644 index 000000000..be1ccaa5c --- /dev/null +++ b/vendor/stephenhill/base58/license @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Stephen Hill <stephen@gatekiller.co.uk> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE.
\ No newline at end of file diff --git a/vendor/stephenhill/base58/phpunit.xml b/vendor/stephenhill/base58/phpunit.xml new file mode 100644 index 000000000..06362417f --- /dev/null +++ b/vendor/stephenhill/base58/phpunit.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8"?> +<phpunit + backupGlobals="false" + backupStaticAttributes="false" + bootstrap="tests/bootstrap.php" + colors="true" + convertErrorsToExceptions="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + processIsolation="true" + stopOnFailure="false" + syntaxCheck="true" + beStrictAboutTestsThatDoNotTestAnything="true" + beStrictAboutOutputDuringTests="true" + verbose="true" +> + <testsuites> + <testsuite name="Base58 Test Suite"> + <directory>./tests/</directory> + </testsuite> + </testsuites> + + <filter> + <whitelist addUncoveredFilesFromWhitelist="false"> + <directory suffix=".php">src</directory> + <exclude> + <directory suffix=".php">vendor</directory> + </exclude> + </whitelist> + </filter> +</phpunit> diff --git a/vendor/stephenhill/base58/readme.md b/vendor/stephenhill/base58/readme.md new file mode 100644 index 000000000..9723a91dd --- /dev/null +++ b/vendor/stephenhill/base58/readme.md @@ -0,0 +1,145 @@ +# Base58 Encoding and Decoding Library for PHP + +[![Build Status](https://travis-ci.org/stephen-hill/base58php.png)](https://travis-ci.org/stephen-hill/base58php) +[![Packagist Release](http://img.shields.io/packagist/v/stephenhill/base58.svg)](https://packagist.org/packages/stephenhill/base58) +[![MIT License](http://img.shields.io/packagist/l/stephenhill/base58.svg)](https://github.com/stephen-hill/base58php/blob/master/license) +[![Flattr this](https://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=stephen-hill&url=https%3A%2F%2Fgithub.com%2Fstephen-hill%2Fbase58php) + +## Long Term Support + +Each major version of this library will be supported for 5 years after it's initial release. Support will be provided for security and bug fixes. + +Version 1 will therefore be supported until the 11th September 2019. + +## Background + +I wanted a replacement for Base64 encoded strings and the [Base58 encoding used by Bitcoin](https://en.bitcoin.it/wiki/Base58Check_encoding) looked ideal. I looked around for an existing PHP library which would directly convert a string into Base58 but I couldn't find one, or at least one that worked correctly and was also well tested. + +So I decided to create a library with the following goals: + +- Encode/Decode PHP Strings +- Simple and easy to use +- Fully Tested +- Available via Composer + +## Requirements + +This library has the following requirements: + +- PHP => 5.3 +- BC Math Extension + +## Installation + +I recommend you install this library via Composer. + +```json +{ + "require": { + "stephenhill/base58": "~1.0" + } +} +``` + +## Basic Usage + +```php +require_once('vendor/autoload.php'); + +$base58 = new StephenHill\Base58(); + +$base58->encode('Hello World'); +$base58->decode('JxF12TrwUP45BMd'); +``` + +## Advanced Usage + +By default this library chooses the encoding service provider to use, either GMPService or BCMathService (in that order). +If you want to specify one of the included services or your own, you can inject it into the constructor. + +```php +require_once('vendor/autoload.php'); + +$gmp = new StephenHill\GMPService(); +$base58 = new StephenHill\Base58(null, $gmp); + +$base58->encode('Hello World'); +$base58->decode('JxF12TrwUP45BMd'); +``` + +Also by default, this library uses Bitcoin's Base58 alphabet. If you want to use another variant, you can do this in the constructor. + +```php +require_once('vendor/autoload.php'); + +// Flickr's Base58 Alphabet +$base58 = new StephenHill\Base58('123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'); + +$base58->encode('Hello World'); +$base58->decode('iXf12sRWto45bmC'); +``` + +## Testing + +This library is tested using PHPUnit. + +```bash +$ bin/phpunit +``` + +## Benchmarking + +You can benchmark this library using [Athletic](https://github.com/polyfractal/athletic). +The benchmarking suite also benchmarks PHP's built-in Base64 and Base16 encoding for comparison. + +```bash +$ bin/athletic -p benchmarks +``` + +Example output. + +``` +StephenHill\Benchmarks\Base16Event + Method Name Iterations Average Time Ops/second + ------------ ------------ -------------- ------------- + encodeBase16: [10,000 ] [0.0000010839939] [922,514.40637] + decodeBase16: [10,000 ] [0.0000011516809] [868,296.03561] + + +StephenHill\Benchmarks\Base58BCMathEvent + Method Name Iterations Average Time Ops/second + ------------ ------------ -------------- ------------- + encodeBase58: [10,000 ] [0.0001500048161] [6,666.45263] + decodeBase58: [10,000 ] [0.0001741812706] [5,741.14540] + + +StephenHill\Benchmarks\Base58GMPEvent + Method Name Iterations Average Time Ops/second + ------------ ------------ -------------- ------------- + encodeBase58: [10,000 ] [0.0001168665648] [8,556.76730] + decodeBase58: [10,000 ] [0.0001385705233] [7,216.54199] + + +StephenHill\Benchmarks\Base64Event + Method Name Iterations Average Time Ops/second + ------------ ------------ -------------- ------------- + encodeBase64: [10,000 ] [0.0000009050369] [1,104,927.29189] + decodeBase64: [10,000 ] [0.0000009787321] [1,021,730.04312] +``` + +## Contributing + +I welcome everyone to contribute to this library. Please see the Contributing document for details. + +## License + +This library is license under the MIT License (MIT). Please see License File for more information. + +## Credits + +This library was forked from [Jeremy Johnstone's](https://github.com/jsjohnst) Base58 methods on Gist https://gist.github.com/jsjohnst/126883. + +Some of the unit tests were based on the following: + +- https://code.google.com/p/bitcoinj/source/browse/core/src/test/java/com/google/bitcoin/core/Base58Test.java +- https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/fixtures/base58.json diff --git a/vendor/stephenhill/base58/src/BCMathService.php b/vendor/stephenhill/base58/src/BCMathService.php new file mode 100644 index 000000000..329e1ceba --- /dev/null +++ b/vendor/stephenhill/base58/src/BCMathService.php @@ -0,0 +1,165 @@ +<?php + +namespace StephenHill; + +use InvalidArgumentException; + +class BCMathService implements ServiceInterface +{ + /** + * @var string + * @since v1.1.0 + */ + protected $alphabet; + + /** + * @var int + * @since v1.1.0 + */ + protected $base; + + /** + * Constructor + * + * @param string $alphabet optional + * @since v1.1.0 + */ + public function __construct($alphabet = null) + { + // Handle null alphabet + if (is_null($alphabet) === true) { + $alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; + } + + // Type validation + if (is_string($alphabet) === false) { + throw new InvalidArgumentException('Argument $alphabet must be a string.'); + } + + // The alphabet must contain 58 characters + if (strlen($alphabet) !== 58) { + throw new InvalidArgumentException('Argument $alphabet must contain 58 characters.'); + } + + $this->alphabet = $alphabet; + $this->base = strlen($alphabet); + } + /** + * Encode a string into base58. + * + * @param string $string The string you wish to encode. + * @since Release v1.1.0 + * @return string The Base58 encoded string. + */ + public function encode($string) + { + // Type validation + if (is_string($string) === false) { + throw new InvalidArgumentException('Argument $string must be a string.'); + } + + // If the string is empty, then the encoded string is obviously empty + if (strlen($string) === 0) { + return ''; + } + + // Strings in PHP are essentially 8-bit byte arrays + // so lets convert the string into a PHP array + $bytes = array_values(unpack('C*', $string)); + + // Now we need to convert the byte array into an arbitrary-precision decimal + // We basically do this by performing a base256 to base10 conversion + $decimal = $bytes[0]; + + for ($i = 1, $l = count($bytes); $i < $l; $i++) { + $decimal = bcmul($decimal, 256); + $decimal = bcadd($decimal, $bytes[$i]); + } + + // This loop now performs base 10 to base 58 conversion + // The remainder or modulo on each loop becomes a base 58 character + $output = ''; + while ($decimal >= $this->base) { + $div = bcdiv($decimal, $this->base, 0); + $mod = (int) bcmod($decimal, $this->base); + $output .= $this->alphabet[$mod]; + $decimal = $div; + } + + // If there's still a remainder, append it + if ($decimal > 0) { + $output .= $this->alphabet[$decimal]; + } + + // Now we need to reverse the encoded data + $output = strrev($output); + + // Now we need to add leading zeros + foreach ($bytes as $byte) { + if ($byte === 0) { + $output = $this->alphabet[0] . $output; + continue; + } + break; + } + + return (string) $output; + } + + /** + * Decode base58 into a PHP string. + * + * @param string $base58 The base58 encoded string. + * @since Release v1.1.0 + * @return string Returns the decoded string. + */ + public function decode($base58) + { + // Type Validation + if (is_string($base58) === false) { + throw new InvalidArgumentException('Argument $base58 must be a string.'); + } + + // If the string is empty, then the decoded string is obviously empty + if (strlen($base58) === 0) { + return ''; + } + + $indexes = array_flip(str_split($this->alphabet)); + $chars = str_split($base58); + + // Check for invalid characters in the supplied base58 string + foreach ($chars as $char) { + if (isset($indexes[$char]) === false) { + throw new InvalidArgumentException('Argument $base58 contains invalid characters. ($char: "'.$char.'" | $base58: "'.$base58.'") '); + } + } + + // Convert from base58 to base10 + $decimal = $indexes[$chars[0]]; + + for ($i = 1, $l = count($chars); $i < $l; $i++) { + $decimal = bcmul($decimal, $this->base); + $decimal = bcadd($decimal, $indexes[$chars[$i]]); + } + + // Convert from base10 to base256 (8-bit byte array) + $output = ''; + while ($decimal > 0) { + $byte = (int) bcmod($decimal, 256); + $output = pack('C', $byte) . $output; + $decimal = bcdiv($decimal, 256, 0); + } + + // Now we need to add leading zeros + foreach ($chars as $char) { + if ($indexes[$char] === 0) { + $output = "\x00" . $output; + continue; + } + break; + } + + return $output; + } +} diff --git a/vendor/stephenhill/base58/src/Base58.php b/vendor/stephenhill/base58/src/Base58.php new file mode 100644 index 000000000..75a2e0de4 --- /dev/null +++ b/vendor/stephenhill/base58/src/Base58.php @@ -0,0 +1,90 @@ +<?php + +namespace StephenHill; + +use InvalidArgumentException; + +/** + * @package StephenHill\Base58 + * @author Stephen Hill <stephen@gatekiller.co.uk> + * @copyright 2014 Stephen Hill <stephen@gatekiller.co.uk> + * @license http://www.opensource.org/licenses/MIT The MIT License + * @link https://github.com/stephen-hill/base58php + * @since Release v1.0.0 + */ +class Base58 +{ + /** + * @var StephenHill\ServiceInterface; + * @since v1.1.0 + */ + protected $service; + + /** + * Constructor + * + * @param string $alphabet optional + * @param ServiceInterface $service optional + * @since v1.0.0 + * @since v1.1.0 Added the optional $service argument. + */ + public function __construct( + $alphabet = null, + ServiceInterface $service = null + ) { + // Handle null alphabet + if (is_null($alphabet) === true) { + $alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; + } + + // Type validation + if (is_string($alphabet) === false) { + throw new InvalidArgumentException('Argument $alphabet must be a string.'); + } + + // The alphabet must contain 58 characters + if (strlen($alphabet) !== 58) { + throw new InvalidArgumentException('Argument $alphabet must contain 58 characters.'); + } + + // Provide a default service if one isn't injected + if ($service === null) { + // Check for GMP support first + if (function_exists('\gmp_init') === true) { + $service = new GMPService($alphabet); + } + else if (function_exists('\bcmul') === true) { + $service = new BCMathService($alphabet); + } + else { + throw new \Exception('Please install the BC Math or GMP extension.'); + } + } + + $this->service = $service; + } + + /** + * Encode a string into base58. + * + * @param string $string The string you wish to encode. + * @since v1.0.0 + * @return string The Base58 encoded string. + */ + public function encode($string) + { + return $this->service->encode($string); + } + + /** + * Decode base58 into a PHP string. + * + * @param string $base58 The base58 encoded string. + * @since v1.0.0 + * @return string Returns the decoded string. + */ + public function decode($base58) + { + return $this->service->decode($base58); + } +} diff --git a/vendor/stephenhill/base58/src/GMPService.php b/vendor/stephenhill/base58/src/GMPService.php new file mode 100644 index 000000000..0dde30594 --- /dev/null +++ b/vendor/stephenhill/base58/src/GMPService.php @@ -0,0 +1,156 @@ +<?php + +namespace StephenHill; + +use InvalidArgumentException; + +class GMPService implements ServiceInterface +{ + /** + * @var string + * @since v1.1.0 + */ + protected $alphabet; + + /** + * @var int + * @since v1.1.0 + */ + protected $base; + + /** + * Constructor + * + * @param string $alphabet optional + * @since v1.1.0 + */ + public function __construct($alphabet = null) + { + // Handle null alphabet + if (is_null($alphabet) === true) { + $alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; + } + + // Type validation + if (is_string($alphabet) === false) { + throw new InvalidArgumentException('Argument $alphabet must be a string.'); + } + + // The alphabet must contain 58 characters + if (strlen($alphabet) !== 58) { + throw new InvalidArgumentException('Argument $alphabet must contain 58 characters.'); + } + + $this->alphabet = $alphabet; + $this->base = strlen($alphabet); + } + /** + * Encode a string into base58. + * + * @param string $string The string you wish to encode. + * @since Release v1.1.0 + * @return string The Base58 encoded string. + */ + public function encode($string) + { + // Type validation + if (is_string($string) === false) { + throw new InvalidArgumentException('Argument $string must be a string.'); + } + + // If the string is empty, then the encoded string is obviously empty + if (strlen($string) === 0) { + return ''; + } + + // Now we need to convert the byte array into an arbitrary-precision decimal + // We basically do this by performing a base256 to base10 conversion + $hex = unpack('H*', $string); + $hex = reset($hex); + $decimal = gmp_init($hex, 16); + + // This loop now performs base 10 to base 58 conversion + // The remainder or modulo on each loop becomes a base 58 character + $output = ''; + while (gmp_cmp($decimal, $this->base) >= 0) { + list($decimal, $mod) = gmp_div_qr($decimal, $this->base); + $output .= $this->alphabet[gmp_intval($mod)]; + } + + // If there's still a remainder, append it + if (gmp_cmp($decimal, 0) > 0) { + $output .= $this->alphabet[gmp_intval($decimal)]; + } + + // Now we need to reverse the encoded data + $output = strrev($output); + + // Now we need to add leading zeros + $bytes = str_split($string); + foreach ($bytes as $byte) { + if ($byte === "\x00") { + $output = $this->alphabet[0] . $output; + continue; + } + break; + } + + return (string) $output; + } + + /** + * Decode base58 into a PHP string. + * + * @param string $base58 The base58 encoded string. + * @since Release v1.1.0 + * @return string Returns the decoded string. + */ + public function decode($base58) + { + // Type Validation + if (is_string($base58) === false) { + throw new InvalidArgumentException('Argument $base58 must be a string.'); + } + + // If the string is empty, then the decoded string is obviously empty + if (strlen($base58) === 0) { + return ''; + } + + $indexes = array_flip(str_split($this->alphabet)); + $chars = str_split($base58); + + // Check for invalid characters in the supplied base58 string + foreach ($chars as $char) { + if (isset($indexes[$char]) === false) { + throw new InvalidArgumentException('Argument $base58 contains invalid characters.'); + } + } + + // Convert from base58 to base10 + $decimal = gmp_init($indexes[$chars[0]], 10); + + for ($i = 1, $l = count($chars); $i < $l; $i++) { + $decimal = gmp_mul($decimal, $this->base); + $decimal = gmp_add($decimal, $indexes[$chars[$i]]); + } + + // Convert from base10 to base256 (8-bit byte array) + $output = ''; + while (gmp_cmp($decimal, 0) > 0) { + list($decimal, $byte) = gmp_div_qr($decimal, 256); + $output = pack('C', gmp_intval($byte)) . $output; + } + + // Now we need to add leading zeros + foreach ($chars as $char) { + if ($indexes[$char] === 0) { + $output = "\x00" . $output; + continue; + } + break; + } + + return $output; + } +} diff --git a/vendor/stephenhill/base58/src/ServiceInterface.php b/vendor/stephenhill/base58/src/ServiceInterface.php new file mode 100644 index 000000000..0fe374524 --- /dev/null +++ b/vendor/stephenhill/base58/src/ServiceInterface.php @@ -0,0 +1,24 @@ +<?php + +namespace StephenHill; + +interface ServiceInterface +{ + /** + * Encode a string into base58. + * + * @param string $string The string you wish to encode. + * @since v1.1.0 + * @return string The Base58 encoded string. + */ + public function encode($string); + + /** + * Decode base58 into a PHP string. + * + * @param string $base58 The base58 encoded string. + * @since v1.1.0 + * @return string Returns the decoded string. + */ + public function decode($base58); +} |