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