| Total Complexity | 3 |
| Total Lines | 31 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php declare(strict_types=1); |
||
| 11 | final class TokenMask |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Masks a token to make it uncompressible. |
||
| 15 | * Applies a random mask to the token and prepends the mask used to the result making the string always unique. |
||
| 16 | * @param string $token An unmasked token. |
||
| 17 | * @return string A masked token. |
||
| 18 | * @throws \Exception if unable to securely generate random bytes |
||
| 19 | */ |
||
| 20 | public static function apply(string $token): string |
||
| 21 | { |
||
| 22 | // The number of bytes in a mask is always equal to the number of bytes in a token. |
||
| 23 | $mask = random_bytes(StringHelper::byteLength($token)); |
||
| 24 | return StringHelper::base64UrlEncode($mask . ($mask ^ $token)); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Unmasks a token previously masked by `mask`. |
||
| 29 | * @param string $maskedToken A masked token. |
||
| 30 | * @return string An unmasked token, or an empty string in case of token format is invalid. |
||
| 31 | */ |
||
| 32 | public static function unmask(string $maskedToken): string |
||
| 42 | } |
||
| 43 | } |
||
| 44 |