| 1 |
# Constant-Time Encoding |
| 2 |
|
| 3 |
[](https://github.com/paragonie/constant_time_encoding/actions) |
| 4 |
[](https://github.com/paragonie/constant_time_encoding/actions) |
| 5 |
[](https://packagist.org/packages/paragonie/constant_time_encoding) |
| 6 |
[](https://packagist.org/packages/paragonie/constant_time_encoding) |
| 7 |
[](https://packagist.org/packages/paragonie/constant_time_encoding) |
| 8 |
[](https://packagist.org/packages/paragonie/constant_time_encoding) |
| 9 |
|
| 10 |
Based on the [constant-time base64 implementation made by Steve "Sc00bz" Thomas](https://github.com/Sc00bz/ConstTimeEncoding), |
| 11 |
this library aims to offer character encoding functions that do not leak |
| 12 |
information about what you are encoding/decoding via processor cache |
| 13 |
misses. Further reading on [cache-timing attacks](http://blog.ircmaxell.com/2014/11/its-all-about-time.html). |
| 14 |
|
| 15 |
Our fork offers the following enhancements: |
| 16 |
|
| 17 |
* `mbstring.func_overload` resistance |
| 18 |
* Unit tests |
| 19 |
* Composer- and Packagist-ready |
| 20 |
* Base16 encoding |
| 21 |
* Base32 encoding |
| 22 |
* Uses `pack()` and `unpack()` instead of `chr()` and `ord()` |
| 23 |
|
| 24 |
## PHP Version Requirements |
| 25 |
|
| 26 |
Version 3 of this library should work on **PHP 8** or newer. |
| 27 |
|
| 28 |
Version 2 of this library should work on **PHP 7** or newer. See [the v2.x branch](https://github.com/paragonie/constant_time_encoding/tree/v2.x). |
| 29 |
|
| 30 |
For PHP 5 support, see [the v1.x branch](https://github.com/paragonie/constant_time_encoding/tree/v1.x). |
| 31 |
|
| 32 |
If you are adding this as a dependency to a project intended to work on PHP 5 through 8.4, please set the required version to `^1|^2|^3`. |
| 33 |
|
| 34 |
## How to Install |
| 35 |
|
| 36 |
```sh |
| 37 |
composer require paragonie/constant_time_encoding |
| 38 |
``` |
| 39 |
|
| 40 |
## How to Use |
| 41 |
|
| 42 |
```php |
| 43 |
use ParagonIE\ConstantTime\Encoding; |
| 44 |
|
| 45 |
// possibly (if applicable): |
| 46 |
// require 'vendor/autoload.php'; |
| 47 |
|
| 48 |
$data = random_bytes(32); |
| 49 |
echo Encoding::base64Encode($data), "\n"; |
| 50 |
echo Encoding::base32EncodeUpper($data), "\n"; |
| 51 |
echo Encoding::base32Encode($data), "\n"; |
| 52 |
echo Encoding::hexEncode($data), "\n"; |
| 53 |
echo Encoding::hexEncodeUpper($data), "\n"; |
| 54 |
``` |
| 55 |
|
| 56 |
Example output: |
| 57 |
|
| 58 |
``` |
| 59 |
1VilPkeVqirlPifk5scbzcTTbMT2clp+Zkyv9VFFasE= |
| 60 |
2VMKKPSHSWVCVZJ6E7SONRY3ZXCNG3GE6ZZFU7TGJSX7KUKFNLAQ==== |
| 61 |
2vmkkpshswvcvzj6e7sonry3zxcng3ge6zzfu7tgjsx7kukfnlaq==== |
| 62 |
d558a53e4795aa2ae53e27e4e6c71bcdc4d36cc4f6725a7e664caff551456ac1 |
| 63 |
D558A53E4795AA2AE53E27E4E6C71BDCC4D36CC4F6725A7E664CAFF551456AC1 |
| 64 |
``` |
| 65 |
|
| 66 |
If you only need a particular variant, you can just reference the |
| 67 |
required class like so: |
| 68 |
|
| 69 |
```php |
| 70 |
use ParagonIE\ConstantTime\Base64; |
| 71 |
use ParagonIE\ConstantTime\Base32; |
| 72 |
|
| 73 |
$data = random_bytes(32); |
| 74 |
echo Base64::encode($data), "\n"; |
| 75 |
echo Base32::encode($data), "\n"; |
| 76 |
``` |
| 77 |
|
| 78 |
Example output: |
| 79 |
|
| 80 |
``` |
| 81 |
1VilPkeVqirlPifk5scbzcTTbMT2clp+Zkyv9VFFasE= |
| 82 |
2vmkkpshswvcvzj6e7sonry3zxcng3ge6zzfu7tgjsx7kukfnlaq==== |
| 83 |
``` |
| 84 |
|
| 85 |
## Support Contracts |
| 86 |
|
| 87 |
If your company uses this library in their products or services, you may be |
| 88 |
interested in [purchasing a support contract from Paragon Initiative Enterprises](https://paragonie.com/enterprise). |