First Commit
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
# editorconfig.org
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
indent_style = tab
|
||||
trim_trailing_whitespace = true
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
|
||||
[*.md]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
# IntelliJ
|
||||
.idea/
|
||||
|
||||
# Composer
|
||||
vendor/
|
||||
composer.phar
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) delight.im (https://www.delight.im/)
|
||||
|
||||
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.
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
# PHP-Base64
|
||||
|
||||
Simple and convenient Base64 encoding and decoding for PHP
|
||||
|
||||
## Requirements
|
||||
|
||||
* PHP 5.3.0+
|
||||
|
||||
## Installation
|
||||
|
||||
1. Include the library via Composer [[?]](https://github.com/delight-im/Knowledge/blob/master/Composer%20(PHP).md):
|
||||
|
||||
```
|
||||
$ composer require delight-im/base64
|
||||
```
|
||||
|
||||
1. Include the Composer autoloader:
|
||||
|
||||
```php
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Standard
|
||||
|
||||
* Encoding data
|
||||
|
||||
```php
|
||||
\Delight\Base64\Base64::encode('Gallia est omnis divisa in partes tres');
|
||||
// string(52) "R2FsbGlhIGVzdCBvbW5pcyBkaXZpc2EgaW4gcGFydGVzIHRyZXM="
|
||||
```
|
||||
|
||||
* Decoding data
|
||||
|
||||
```php
|
||||
\Delight\Base64\Base64::decode('R2FsbGlhIGVzdCBvbW5pcyBkaXZpc2EgaW4gcGFydGVzIHRyZXM=');
|
||||
// string(38) "Gallia est omnis divisa in partes tres"
|
||||
```
|
||||
|
||||
### URL-safe
|
||||
|
||||
* Encoding data
|
||||
|
||||
```php
|
||||
\Delight\Base64\Base64::encodeUrlSafe('πάντα χωρεῖ καὶ οὐδὲν μένει …');
|
||||
// string(80) "z4DOrM69z4TOsSDPh8-Jz4HOteG_liDOus6x4b22IM6_4b2QzrThvbLOvSDOvM6tzr3Otc65IOKApg~~"
|
||||
```
|
||||
|
||||
* Decoding data
|
||||
|
||||
```php
|
||||
\Delight\Base64\Base64::decodeUrlSafe('z4DOrM69z4TOsSDPh8-Jz4HOteG_liDOus6x4b22IM6_4b2QzrThvbLOvSDOvM6tzr3Otc65IOKApg~~');
|
||||
// string(58) "πάντα χωρεῖ καὶ οὐδὲν μένει …"
|
||||
```
|
||||
|
||||
### URL-safe without padding
|
||||
|
||||
* Encoding data
|
||||
|
||||
```php
|
||||
\Delight\Base64\Base64::encodeUrlSafeWithoutPadding('πάντα χωρεῖ καὶ οὐδὲν μένει …');
|
||||
// string(78) "z4DOrM69z4TOsSDPh8-Jz4HOteG_liDOus6x4b22IM6_4b2QzrThvbLOvSDOvM6tzr3Otc65IOKApg"
|
||||
```
|
||||
|
||||
* Decoding data
|
||||
|
||||
```php
|
||||
\Delight\Base64\Base64::decodeUrlSafeWithoutPadding('z4DOrM69z4TOsSDPh8-Jz4HOteG_liDOus6x4b22IM6_4b2QzrThvbLOvSDOvM6tzr3Otc65IOKApg');
|
||||
// string(58) "πάντα χωρεῖ καὶ οὐδὲν μένει …"
|
||||
```
|
||||
|
||||
## Specifications
|
||||
|
||||
* [RFC 4648](https://tools.ietf.org/html/rfc4648)
|
||||
* [RFC 3548](https://tools.ietf.org/html/rfc3548)
|
||||
* [RFC 2045](https://tools.ietf.org/html/rfc2045)
|
||||
* [RFC 1421](https://tools.ietf.org/html/rfc1421)
|
||||
* [RFC 7515](https://tools.ietf.org/html/rfc7515)
|
||||
|
||||
## Contributing
|
||||
|
||||
All contributions are welcome! If you wish to contribute, please create an issue first so that your feature, problem or question can be discussed.
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "delight-im/base64",
|
||||
"description": "Simple and convenient Base64 encoding and decoding for PHP",
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"type": "library",
|
||||
"keywords": [ "base64", "base-64", "encode", "decode", "encoding", "decoding", "url-safe", "url" ],
|
||||
"homepage": "https://github.com/delight-im/PHP-Base64",
|
||||
"license": "MIT",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Delight\\Base64\\": "src/"
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "d69f90769e62bab4c7f053c4a3030138",
|
||||
"packages": [],
|
||||
"packages-dev": [],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [],
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"platform-dev": []
|
||||
}
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Base64 (https://github.com/delight-im/PHP-Base64)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Base64;
|
||||
|
||||
use Delight\Base64\Throwable\DecodingError;
|
||||
use Delight\Base64\Throwable\EncodingError;
|
||||
|
||||
/** Utilities for encoding and decoding data using Base64 and variants thereof */
|
||||
final class Base64 {
|
||||
|
||||
/**
|
||||
* The last three characters from the alphabet of the standard implementation
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const LAST_THREE_STANDARD = '+/=';
|
||||
|
||||
/**
|
||||
* The last three characters from the alphabet of the URL-safe implementation
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const LAST_THREE_URL_SAFE = '-_~';
|
||||
|
||||
/**
|
||||
* Encodes the supplied data to Base64
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
* @throws EncodingError if the input has been invalid
|
||||
*/
|
||||
public static function encode($data) {
|
||||
$encoded = \base64_encode($data);
|
||||
|
||||
if ($encoded === false) {
|
||||
throw new EncodingError();
|
||||
}
|
||||
|
||||
return $encoded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes the supplied data from Base64
|
||||
*
|
||||
* @param string $data
|
||||
* @return mixed
|
||||
* @throws DecodingError if the input has been invalid
|
||||
*/
|
||||
public static function decode($data) {
|
||||
$decoded = \base64_decode($data, true);
|
||||
|
||||
if ($decoded === false) {
|
||||
throw new DecodingError();
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes the supplied data to a URL-safe variant of Base64
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
* @throws EncodingError if the input has been invalid
|
||||
*/
|
||||
public static function encodeUrlSafe($data) {
|
||||
$encoded = self::encode($data);
|
||||
|
||||
return \strtr(
|
||||
$encoded,
|
||||
self::LAST_THREE_STANDARD,
|
||||
self::LAST_THREE_URL_SAFE
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes the supplied data from a URL-safe variant of Base64
|
||||
*
|
||||
* @param string $data
|
||||
* @return mixed
|
||||
* @throws DecodingError if the input has been invalid
|
||||
*/
|
||||
public static function decodeUrlSafe($data) {
|
||||
$data = \strtr(
|
||||
$data,
|
||||
self::LAST_THREE_URL_SAFE,
|
||||
self::LAST_THREE_STANDARD
|
||||
);
|
||||
|
||||
return self::decode($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes the supplied data to a URL-safe variant of Base64 without padding
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
* @throws EncodingError if the input has been invalid
|
||||
*/
|
||||
public static function encodeUrlSafeWithoutPadding($data) {
|
||||
$encoded = self::encode($data);
|
||||
|
||||
$encoded = \rtrim(
|
||||
$encoded,
|
||||
\substr(self::LAST_THREE_STANDARD, -1)
|
||||
);
|
||||
|
||||
return \strtr(
|
||||
$encoded,
|
||||
\substr(self::LAST_THREE_STANDARD, 0, -1),
|
||||
\substr(self::LAST_THREE_URL_SAFE, 0, -1)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes the supplied data from a URL-safe variant of Base64 without padding
|
||||
*
|
||||
* @param string $data
|
||||
* @return mixed
|
||||
* @throws DecodingError if the input has been invalid
|
||||
*/
|
||||
public static function decodeUrlSafeWithoutPadding($data) {
|
||||
return self::decodeUrlSafe($data);
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Base64 (https://github.com/delight-im/PHP-Base64)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Base64\Throwable;
|
||||
|
||||
/** Error that is thrown when an attempt is being made to decode illegal input */
|
||||
class DecodingError extends Error {}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Base64 (https://github.com/delight-im/PHP-Base64)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Base64\Throwable;
|
||||
|
||||
/** Error that is thrown when an attempt is being made to encode illegal input */
|
||||
class EncodingError extends Error {}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Base64 (https://github.com/delight-im/PHP-Base64)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Base64\Throwable;
|
||||
|
||||
/** Base class for all (unchecked) errors */
|
||||
abstract class Error extends \Exception {}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Base64 (https://github.com/delight-im/PHP-Base64)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Base64\Throwable;
|
||||
|
||||
/** Base class for all (checked) exceptions */
|
||||
abstract class Exception extends \Exception {}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Base64 (https://github.com/delight-im/PHP-Base64)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
\error_reporting(E_ALL);
|
||||
\ini_set('display_errors', 'stdout');
|
||||
|
||||
\header('Content-type: text/plain; charset=utf-8');
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
(\Delight\Base64\Base64::encode('Gallia est omnis divisa in partes tres') === 'R2FsbGlhIGVzdCBvbW5pcyBkaXZpc2EgaW4gcGFydGVzIHRyZXM=') or exit('Failed on line ' . __LINE__);
|
||||
(\Delight\Base64\Base64::decode('R2FsbGlhIGVzdCBvbW5pcyBkaXZpc2EgaW4gcGFydGVzIHRyZXM=') === 'Gallia est omnis divisa in partes tres') or exit('Failed on line ' . __LINE__);
|
||||
(\Delight\Base64\Base64::encodeUrlSafe('πάντα χωρεῖ καὶ οὐδὲν μένει …') === 'z4DOrM69z4TOsSDPh8-Jz4HOteG_liDOus6x4b22IM6_4b2QzrThvbLOvSDOvM6tzr3Otc65IOKApg~~') or exit('Failed on line ' . __LINE__);
|
||||
(\Delight\Base64\Base64::decodeUrlSafe('z4DOrM69z4TOsSDPh8-Jz4HOteG_liDOus6x4b22IM6_4b2QzrThvbLOvSDOvM6tzr3Otc65IOKApg~~') === 'πάντα χωρεῖ καὶ οὐδὲν μένει …') or exit('Failed on line ' . __LINE__);
|
||||
(\Delight\Base64\Base64::encodeUrlSafeWithoutPadding('πάντα χωρεῖ καὶ οὐδὲν μένει …') === 'z4DOrM69z4TOsSDPh8-Jz4HOteG_liDOus6x4b22IM6_4b2QzrThvbLOvSDOvM6tzr3Otc65IOKApg') or exit('Failed on line ' . __LINE__);
|
||||
(\Delight\Base64\Base64::decodeUrlSafeWithoutPadding('z4DOrM69z4TOsSDPh8-Jz4HOteG_liDOus6x4b22IM6_4b2QzrThvbLOvSDOvM6tzr3Otc65IOKApg') === 'πάντα χωρεῖ καὶ οὐδὲν μένει …') or exit('Failed on line ' . __LINE__);
|
||||
|
||||
for ($i = 0; $i < 1000; $i++) {
|
||||
$data = \openssl_random_pseudo_bytes(
|
||||
\mt_rand(12, 24)
|
||||
);
|
||||
|
||||
(\Delight\Base64\Base64::decode(\Delight\Base64\Base64::encode($data)) === $data) or exit('Failed on line ' . __LINE__);
|
||||
(\Delight\Base64\Base64::decodeUrlSafe(\Delight\Base64\Base64::encodeUrlSafe($data)) === $data) or exit('Failed on line ' . __LINE__);
|
||||
(\Delight\Base64\Base64::decodeUrlSafeWithoutPadding(\Delight\Base64\Base64::encodeUrlSafeWithoutPadding($data)) === $data) or exit('Failed on line ' . __LINE__);
|
||||
}
|
||||
|
||||
echo 'ALL TESTS PASSED';
|
||||
echo "\n";
|
||||
Reference in New Issue
Block a user