Total Complexity | 3 |
Total Lines | 31 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php declare(strict_types=1); |
||
11 | final class TokenMasker |
||
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 | 5 | public static function mask(string $token): string |
|
21 | { |
||
22 | // The number of bytes in a mask is always equal to the number of bytes in a token. |
||
23 | 5 | $mask = random_bytes(StringHelper::byteLength($token)); |
|
24 | 4 | 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 | 6 | public static function unmask(string $maskedToken): string |
|
42 | } |
||
43 | } |
||
44 |