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 10Based on the [constant-time base64 implementation made by Steve "Sc00bz" Thomas](https://github.com/Sc00bz/ConstTimeEncoding), 11this library aims to offer character encoding functions that do not leak 12information about what you are encoding/decoding via processor cache 13misses. Further reading on [cache-timing attacks](http://blog.ircmaxell.com/2014/11/its-all-about-time.html). 14 15Our 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 26Version 3 of this library should work on **PHP 8** or newer. 27 28Version 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 30For PHP 5 support, see [the v1.x branch](https://github.com/paragonie/constant_time_encoding/tree/v1.x). 31 32If 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 37composer require paragonie/constant_time_encoding 38``` 39 40## How to Use 41 42```php 43use ParagonIE\ConstantTime\Encoding; 44 45// possibly (if applicable): 46// require 'vendor/autoload.php'; 47 48$data = random_bytes(32); 49echo Encoding::base64Encode($data), "\n"; 50echo Encoding::base32EncodeUpper($data), "\n"; 51echo Encoding::base32Encode($data), "\n"; 52echo Encoding::hexEncode($data), "\n"; 53echo Encoding::hexEncodeUpper($data), "\n"; 54``` 55 56Example output: 57 58``` 591VilPkeVqirlPifk5scbzcTTbMT2clp+Zkyv9VFFasE= 602VMKKPSHSWVCVZJ6E7SONRY3ZXCNG3GE6ZZFU7TGJSX7KUKFNLAQ==== 612vmkkpshswvcvzj6e7sonry3zxcng3ge6zzfu7tgjsx7kukfnlaq==== 62d558a53e4795aa2ae53e27e4e6c71bcdc4d36cc4f6725a7e664caff551456ac1 63D558A53E4795AA2AE53E27E4E6C71BDCC4D36CC4F6725A7E664CAFF551456AC1 64``` 65 66If you only need a particular variant, you can just reference the 67required class like so: 68 69```php 70use ParagonIE\ConstantTime\Base64; 71use ParagonIE\ConstantTime\Base32; 72 73$data = random_bytes(32); 74echo Base64::encode($data), "\n"; 75echo Base32::encode($data), "\n"; 76``` 77 78Example output: 79 80``` 811VilPkeVqirlPifk5scbzcTTbMT2clp+Zkyv9VFFasE= 822vmkkpshswvcvzj6e7sonry3zxcng3ge6zzfu7tgjsx7kukfnlaq==== 83``` 84 85## Support Contracts 86 87If your company uses this library in their products or services, you may be 88interested in [purchasing a support contract from Paragon Initiative Enterprises](https://paragonie.com/enterprise). 89