Completed
Push — php71-cleanup ( 2808ea )
by Alexander
12:27
created

Security::hashData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
cc 2
eloc 5
nc 2
nop 3
crap 2.032
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\base;
9
10
use Yii;
11
use yii\helpers\StringHelper;
12
13
/**
14
 * Security provides a set of methods to handle common security-related tasks.
15
 *
16
 * In particular, Security supports the following features:
17
 *
18
 * - Encryption/decryption: [[encryptByKey()]], [[decryptByKey()]], [[encryptByPassword()]] and [[decryptByPassword()]]
19
 * - Key derivation using standard algorithms: [[pbkdf2()]] and [[hkdf()]]
20
 * - Data tampering prevention: [[hashData()]] and [[validateData()]]
21
 * - Password validation: [[generatePasswordHash()]] and [[validatePassword()]]
22
 *
23
 * > Note: this class requires 'OpenSSL' PHP extension for random key/string generation on Windows and
24
 * for encryption/decryption on all platforms. For the highest security level PHP version >= 5.5.0 is recommended.
25
 *
26
 * For more details and usage information on Security, see the [guide article on security](guide:security-overview).
27
 *
28
 * @author Qiang Xue <[email protected]>
29
 * @author Tom Worster <[email protected]>
30
 * @author Klimov Paul <[email protected]>
31
 * @since 2.0
32
 */
33
class Security extends Component
34
{
35
    /**
36
     * @var string The cipher to use for encryption and decryption.
37
     */
38
    public $cipher = 'AES-128-CBC';
39
    /**
40
     * @var array[] Look-up table of block sizes and key sizes for each supported OpenSSL cipher.
41
     *
42
     * In each element, the key is one of the ciphers supported by OpenSSL (@see openssl_get_cipher_methods()).
43
     * The value is an array of two integers, the first is the cipher's block size in bytes and the second is
44
     * the key size in bytes.
45
     *
46
     * > Warning: All OpenSSL ciphers that we recommend are in the default value, i.e. AES in CBC mode.
47
     *
48
     * > Note: Yii's encryption protocol uses the same size for cipher key, HMAC signature key and key
49
     * derivation salt.
50
     */
51
    public $allowedCiphers = [
52
        'AES-128-CBC' => [16, 16],
53
        'AES-192-CBC' => [16, 24],
54
        'AES-256-CBC' => [16, 32],
55
    ];
56
    /**
57
     * @var string Hash algorithm for key derivation. Recommend sha256, sha384 or sha512.
58
     * @see [hash_algos()](http://php.net/manual/en/function.hash-algos.php)
59
     */
60
    public $kdfHash = 'sha256';
61
    /**
62
     * @var string Hash algorithm for message authentication. Recommend sha256, sha384 or sha512.
63
     * @see [hash_algos()](http://php.net/manual/en/function.hash-algos.php)
64
     */
65
    public $macHash = 'sha256';
66
    /**
67
     * @var string HKDF info value for derivation of message authentication key.
68
     * @see hkdf()
69
     */
70
    public $authKeyInfo = 'AuthorizationKey';
71
    /**
72
     * @var int derivation iterations count.
73
     * Set as high as possible to hinder dictionary password attacks.
74
     */
75
    public $derivationIterations = 100000;
76
77
    /**
78
     * @var int Default cost used for password hashing.
79
     * Allowed value is between 4 and 31.
80
     * @see generatePasswordHash()
81
     * @since 2.0.6
82
     */
83
    public $passwordHashCost = 13;
84
85
86
    /**
87
     * Encrypts data using a password.
88
     * Derives keys for encryption and authentication from the password using PBKDF2 and a random salt,
89
     * which is deliberately slow to protect against dictionary attacks. Use [[encryptByKey()]] to
90
     * encrypt fast using a cryptographic key rather than a password. Key derivation time is
91
     * determined by [[$derivationIterations]], which should be set as high as possible.
92
     * The encrypted data includes a keyed message authentication code (MAC) so there is no need
93
     * to hash input or output data.
94
     * > Note: Avoid encrypting with passwords wherever possible. Nothing can protect against
95
     * poor-quality or compromised passwords.
96
     * @param string $data the data to encrypt
97
     * @param string $password the password to use for encryption
98
     * @return string the encrypted data
99
     * @see decryptByPassword()
100
     * @see encryptByKey()
101
     */
102 1
    public function encryptByPassword($data, $password)
103
    {
104 1
        return $this->encrypt($data, true, $password, null);
105
    }
106
107
    /**
108
     * Encrypts data using a cryptographic key.
109
     * Derives keys for encryption and authentication from the input key using HKDF and a random salt,
110
     * which is very fast relative to [[encryptByPassword()]]. The input key must be properly
111
     * random -- use [[generateRandomKey()]] to generate keys.
112
     * The encrypted data includes a keyed message authentication code (MAC) so there is no need
113
     * to hash input or output data.
114
     * @param string $data the data to encrypt
115
     * @param string $inputKey the input to use for encryption and authentication
116
     * @param string $info optional context and application specific information, see [[hkdf()]]
117
     * @return string the encrypted data
118
     * @see decryptByKey()
119
     * @see encryptByPassword()
120
     */
121 1
    public function encryptByKey($data, $inputKey, $info = null)
122
    {
123 1
        return $this->encrypt($data, false, $inputKey, $info);
124
    }
125
126
    /**
127
     * Verifies and decrypts data encrypted with [[encryptByPassword()]].
128
     * @param string $data the encrypted data to decrypt
129
     * @param string $password the password to use for decryption
130
     * @return bool|string the decrypted data or false on authentication failure
131
     * @see encryptByPassword()
132
     */
133 10
    public function decryptByPassword($data, $password)
134
    {
135 10
        return $this->decrypt($data, true, $password, null);
136
    }
137
138
    /**
139
     * Verifies and decrypts data encrypted with [[encryptByKey()]].
140
     * @param string $data the encrypted data to decrypt
141
     * @param string $inputKey the input to use for encryption and authentication
142
     * @param string $info optional context and application specific information, see [[hkdf()]]
143
     * @return bool|string the decrypted data or false on authentication failure
144
     * @see encryptByKey()
145
     */
146 10
    public function decryptByKey($data, $inputKey, $info = null)
147
    {
148 10
        return $this->decrypt($data, false, $inputKey, $info);
149
    }
150
151
    /**
152
     * Encrypts data.
153
     *
154
     * @param string $data data to be encrypted
155
     * @param bool $passwordBased set true to use password-based key derivation
156
     * @param string $secret the encryption password or key
157
     * @param string|null $info context/application specific information, e.g. a user ID
158
     * See [RFC 5869 Section 3.2](https://tools.ietf.org/html/rfc5869#section-3.2) for more details.
159
     *
160
     * @return string the encrypted data
161
     * @throws InvalidConfigException on OpenSSL not loaded
162
     * @throws Exception on OpenSSL error
163
     * @see decrypt()
164
     */
165 2
    protected function encrypt($data, $passwordBased, $secret, $info)
166
    {
167 2
        if (!extension_loaded('openssl')) {
168
            throw new InvalidConfigException('Encryption requires the OpenSSL PHP extension');
169
        }
170 2
        if (!isset($this->allowedCiphers[$this->cipher][0], $this->allowedCiphers[$this->cipher][1])) {
171
            throw new InvalidConfigException($this->cipher . ' is not an allowed cipher');
172
        }
173
174 2
        [$blockSize, $keySize] = $this->allowedCiphers[$this->cipher];
0 ignored issues
show
Bug introduced by
The variable $blockSize does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $keySize does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
175
176 2
        $keySalt = $this->generateRandomKey($keySize);
177 2
        if ($passwordBased) {
178 1
            $key = $this->pbkdf2($this->kdfHash, $secret, $keySalt, $this->derivationIterations, $keySize);
179
        } else {
180 1
            $key = $this->hkdf($this->kdfHash, $secret, $keySalt, $info, $keySize);
181
        }
182
183 2
        $iv = $this->generateRandomKey($blockSize);
184
185 2
        $encrypted = openssl_encrypt($data, $this->cipher, $key, OPENSSL_RAW_DATA, $iv);
186 2
        if ($encrypted === false) {
187
            throw new \yii\base\Exception('OpenSSL failure on encryption: ' . openssl_error_string());
188
        }
189
190 2
        $authKey = $this->hkdf($this->kdfHash, $key, null, $this->authKeyInfo, $keySize);
191 2
        $hashed = $this->hashData($iv . $encrypted, $authKey);
192
193
        /*
194
         * Output: [keySalt][MAC][IV][ciphertext]
195
         * - keySalt is KEY_SIZE bytes long
196
         * - MAC: message authentication code, length same as the output of MAC_HASH
197
         * - IV: initialization vector, length $blockSize
198
         */
199 2
        return $keySalt . $hashed;
200
    }
201
202
    /**
203
     * Decrypts data.
204
     *
205
     * @param string $data encrypted data to be decrypted.
206
     * @param bool $passwordBased set true to use password-based key derivation
207
     * @param string $secret the decryption password or key
208
     * @param string|null $info context/application specific information, @see encrypt()
209
     *
210
     * @return bool|string the decrypted data or false on authentication failure
211
     * @throws InvalidConfigException on OpenSSL not loaded
212
     * @throws Exception on OpenSSL error
213
     * @see encrypt()
214
     */
215 20
    protected function decrypt($data, $passwordBased, $secret, $info)
216
    {
217 20
        if (!extension_loaded('openssl')) {
218
            throw new InvalidConfigException('Encryption requires the OpenSSL PHP extension');
219
        }
220 20
        if (!isset($this->allowedCiphers[$this->cipher][0], $this->allowedCiphers[$this->cipher][1])) {
221
            throw new InvalidConfigException($this->cipher . ' is not an allowed cipher');
222
        }
223
224 20
        [$blockSize, $keySize] = $this->allowedCiphers[$this->cipher];
0 ignored issues
show
Bug introduced by
The variable $blockSize does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $keySize does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
225
226 20
        $keySalt = StringHelper::byteSubstr($data, 0, $keySize);
227 20
        if ($passwordBased) {
228 10
            $key = $this->pbkdf2($this->kdfHash, $secret, $keySalt, $this->derivationIterations, $keySize);
229
        } else {
230 10
            $key = $this->hkdf($this->kdfHash, $secret, $keySalt, $info, $keySize);
231
        }
232
233 20
        $authKey = $this->hkdf($this->kdfHash, $key, null, $this->authKeyInfo, $keySize);
234 20
        $data = $this->validateData(StringHelper::byteSubstr($data, $keySize), $authKey);
235 20
        if ($data === false) {
236 2
            return false;
237
        }
238
239 20
        $iv = StringHelper::byteSubstr($data, 0, $blockSize);
240 20
        $encrypted = StringHelper::byteSubstr($data, $blockSize);
241
242 20
        $decrypted = openssl_decrypt($encrypted, $this->cipher, $key, OPENSSL_RAW_DATA, $iv);
243 20
        if ($decrypted === false) {
244
            throw new \yii\base\Exception('OpenSSL failure on decryption: ' . openssl_error_string());
245
        }
246
247 20
        return $decrypted;
248
    }
249
250
    /**
251
     * Derives a key from the given input key using the standard HKDF algorithm.
252
     * Implements HKDF specified in [RFC 5869](https://tools.ietf.org/html/rfc5869).
253
     * Recommend use one of the SHA-2 hash algorithms: sha224, sha256, sha384 or sha512.
254
     * @param string $algo a hash algorithm supported by `hash_hmac()`, e.g. 'SHA-256'
255
     * @param string $inputKey the source key
256
     * @param string $salt the random salt
257
     * @param string $info optional info to bind the derived key material to application-
258
     * and context-specific information, e.g. a user ID or API version, see
259
     * [RFC 5869](https://tools.ietf.org/html/rfc5869)
260
     * @param int $length length of the output key in bytes. If 0, the output key is
261
     * the length of the hash algorithm output.
262
     * @throws InvalidArgumentException when HMAC generation fails.
263
     * @return string the derived key
264
     */
265 27
    public function hkdf($algo, $inputKey, $salt = null, $info = null, $length = 0)
266
    {
267 27
        if (function_exists('hash_hkdf')) {
268 27
            $outputKey = hash_hkdf($algo, $inputKey, $length, $info, $salt);
269 27
            if ($outputKey === false) {
270
                throw new InvalidArgumentException('Invalid parameters to hash_hkdf()');
271
            }
272 27
            return $outputKey;
273
        }
274
275
        $test = @hash_hmac($algo, '', '', true);
276
        if (!$test) {
277
            throw new InvalidArgumentException('Failed to generate HMAC with hash algorithm: ' . $algo);
278
        }
279
        $hashLength = StringHelper::byteLength($test);
280
        if (is_string($length) && preg_match('{^\d{1,16}$}', $length)) {
281
            $length = (int) $length;
282
        }
283
        if (!is_int($length) || $length < 0 || $length > 255 * $hashLength) {
284
            throw new InvalidArgumentException('Invalid length');
285
        }
286
        $blocks = $length !== 0 ? ceil($length / $hashLength) : 1;
287
288
        if ($salt === null) {
289
            $salt = str_repeat("\0", $hashLength);
290
        }
291
        $prKey = hash_hmac($algo, $inputKey, $salt, true);
292
293
        $hmac = '';
294
        $outputKey = '';
295
        for ($i = 1; $i <= $blocks; $i++) {
296
            $hmac = hash_hmac($algo, $hmac . $info . chr($i), $prKey, true);
297
            $outputKey .= $hmac;
298
        }
299
300
        if ($length !== 0) {
301
            $outputKey = StringHelper::byteSubstr($outputKey, 0, $length);
302
        }
303
        return $outputKey;
304
    }
305
306
    /**
307
     * Derives a key from the given password using the standard PBKDF2 algorithm.
308
     * Implements HKDF2 specified in [RFC 2898](http://tools.ietf.org/html/rfc2898#section-5.2)
309
     * Recommend use one of the SHA-2 hash algorithms: sha224, sha256, sha384 or sha512.
310
     * @param string $algo a hash algorithm supported by `hash_hmac()`, e.g. 'SHA-256'
311
     * @param string $password the source password
312
     * @param string $salt the random salt
313
     * @param int $iterations the number of iterations of the hash algorithm. Set as high as
314
     * possible to hinder dictionary password attacks.
315
     * @param int $length length of the output key in bytes. If 0, the output key is
316
     * the length of the hash algorithm output.
317
     * @return string the derived key
318
     * @throws InvalidArgumentException when hash generation fails due to invalid params given.
319
     */
320 19
    public function pbkdf2($algo, $password, $salt, $iterations, $length = 0)
321
    {
322 19
        $outputKey = hash_pbkdf2($algo, $password, $salt, $iterations, $length, true);
323 19
        if ($outputKey === false) {
324
            throw new InvalidArgumentException('Invalid parameters to hash_pbkdf2()');
325
        }
326 19
        return $outputKey;
327
    }
328
329
    /**
330
     * Prefixes data with a keyed hash value so that it can later be detected if it is tampered.
331
     * There is no need to hash inputs or outputs of [[encryptByKey()]] or [[encryptByPassword()]]
332
     * as those methods perform the task.
333
     * @param string $data the data to be protected
334
     * @param string $key the secret key to be used for generating hash. Should be a secure
335
     * cryptographic key.
336
     * @param bool $rawHash whether the generated hash value is in raw binary format. If false, lowercase
337
     * hex digits will be generated.
338
     * @return string the data prefixed with the keyed hash
339
     * @throws InvalidConfigException when HMAC generation fails.
340
     * @see validateData()
341
     * @see generateRandomKey()
342
     * @see hkdf()
343
     * @see pbkdf2()
344
     */
345 3
    public function hashData($data, $key, $rawHash = false)
346
    {
347 3
        $hash = hash_hmac($this->macHash, $data, $key, $rawHash);
348 3
        if (!$hash) {
349
            throw new InvalidConfigException('Failed to generate HMAC with hash algorithm: ' . $this->macHash);
350
        }
351 3
        return $hash . $data;
352
    }
353
354
    /**
355
     * Validates if the given data is tampered.
356
     * @param string $data the data to be validated. The data must be previously
357
     * generated by [[hashData()]].
358
     * @param string $key the secret key that was previously used to generate the hash for the data in [[hashData()]].
359
     * function to see the supported hashing algorithms on your system. This must be the same
360
     * as the value passed to [[hashData()]] when generating the hash for the data.
361
     * @param bool $rawHash this should take the same value as when you generate the data using [[hashData()]].
362
     * It indicates whether the hash value in the data is in binary format. If false, it means the hash value consists
363
     * of lowercase hex digits only.
364
     * hex digits will be generated.
365
     * @return string|false the real data with the hash stripped off. False if the data is tampered.
366
     * @throws InvalidConfigException when HMAC generation fails.
367
     * @see hashData()
368
     */
369 21
    public function validateData($data, $key, $rawHash = false)
370
    {
371 21
        $test = @hash_hmac($this->macHash, '', '', $rawHash);
372 21
        if (!$test) {
373
            throw new InvalidConfigException('Failed to generate HMAC with hash algorithm: ' . $this->macHash);
374
        }
375 21
        $hashLength = StringHelper::byteLength($test);
376 21
        if (StringHelper::byteLength($data) >= $hashLength) {
377 21
            $hash = StringHelper::byteSubstr($data, 0, $hashLength);
378 21
            $pureData = StringHelper::byteSubstr($data, $hashLength);
379
380 21
            $calculatedHash = hash_hmac($this->macHash, $pureData, $key, $rawHash);
381
382 21
            if (hash_equals($hash, $calculatedHash)) {
383 21
                return $pureData;
384
            }
385
        }
386 3
        return false;
387
    }
388
389
    /**
390
     * Generates specified number of random bytes.
391
     * Note that output may not be ASCII.
392
     * @see generateRandomString() if you need a string.
393
     *
394
     * @param int $length the number of bytes to generate
395
     * @return string the generated random bytes
396
     * @throws InvalidArgumentException if wrong length is specified
397
     * @throws Exception on failure.
398
     */
399 67
    public function generateRandomKey($length = 32)
400
    {
401 67
        if (!is_int($length)) {
402 3
            throw new InvalidArgumentException('First parameter ($length) must be an integer');
403
        }
404
405 64
        if ($length < 1) {
406 3
            throw new InvalidArgumentException('First parameter ($length) must be greater than 0');
407
        }
408
409 61
        return random_bytes($length);
410
    }
411
412
    /**
413
     * Generates a random string of specified length.
414
     * The string generated matches [A-Za-z0-9_-]+ and is transparent to URL-encoding.
415
     *
416
     * @param int $length the length of the key in characters
417
     * @return string the generated random key
418
     * @throws Exception on failure.
419
     */
420 14
    public function generateRandomString($length = 32)
421
    {
422 14
        if (!is_int($length)) {
423
            throw new InvalidArgumentException('First parameter ($length) must be an integer');
424
        }
425
426 14
        if ($length < 1) {
427
            throw new InvalidArgumentException('First parameter ($length) must be greater than 0');
428
        }
429
430 14
        $bytes = $this->generateRandomKey($length);
431 14
        return substr(StringHelper::base64UrlEncode($bytes), 0, $length);
432
    }
433
434
    /**
435
     * Generates a secure hash from a password and a random salt.
436
     *
437
     * The generated hash can be stored in database.
438
     * Later when a password needs to be validated, the hash can be fetched and passed
439
     * to [[validatePassword()]]. For example,
440
     *
441
     * ```php
442
     * // generates the hash (usually done during user registration or when the password is changed)
443
     * $hash = Yii::$app->getSecurity()->generatePasswordHash($password);
444
     * // ...save $hash in database...
445
     *
446
     * // during login, validate if the password entered is correct using $hash fetched from database
447
     * if (Yii::$app->getSecurity()->validatePassword($password, $hash) {
448
     *     // password is good
449
     * } else {
450
     *     // password is bad
451
     * }
452
     * ```
453
     *
454
     * @param string $password The password to be hashed.
455
     * @param int $cost Cost parameter used by the Blowfish hash algorithm.
456
     * The higher the value of cost,
457
     * the longer it takes to generate the hash and to verify a password against it. Higher cost
458
     * therefore slows down a brute-force attack. For best protection against brute-force attacks,
459
     * set it to the highest value that is tolerable on production servers. The time taken to
460
     * compute the hash doubles for every increment by one of $cost.
461
     * @return string The password hash string. When [[passwordHashStrategy]] is set to 'crypt',
462
     * the output is always 60 ASCII characters, when set to 'password_hash' the output length
463
     * might increase in future versions of PHP (http://php.net/manual/en/function.password-hash.php)
464
     * @throws Exception on bad password parameter or cost parameter.
465
     * @see validatePassword()
466
     */
467 1
    public function generatePasswordHash($password, $cost = null)
468
    {
469 1
        if ($cost === null) {
470 1
            $cost = $this->passwordHashCost;
471
        }
472
473
        /* @noinspection PhpUndefinedConstantInspection */
474 1
        return password_hash($password, PASSWORD_DEFAULT, ['cost' => $cost]);
475
    }
476
477
    /**
478
     * Verifies a password against a hash.
479
     * @param string $password The password to verify.
480
     * @param string $hash The hash to verify the password against.
481
     * @return bool whether the password is correct.
482
     * @throws InvalidArgumentException on bad password/hash parameters or if crypt() with Blowfish hash is not
483
     * available.
484
     * @see generatePasswordHash()
485
     */
486 1
    public function validatePassword($password, $hash)
487
    {
488 1
        if (!is_string($password) || $password === '') {
489
            throw new InvalidArgumentException('Password must be a string and cannot be empty.');
490
        }
491
492 1
        if (!preg_match('/^\$2[axy]\$(\d\d)\$[\.\/0-9A-Za-z]{22}/', $hash, $matches)
493 1
            || $matches[1] < 4
494 1
            || $matches[1] > 30
495
        ) {
496
            throw new InvalidArgumentException('Hash is invalid.');
497
        }
498
499 1
        return password_verify($password, $hash);
500
    }
501
502
    /**
503
     * Performs string comparison using timing attack resistant approach.
504
     *
505
     * @param string $expected string to compare.
506
     * @param string $actual user-supplied string.
507
     * @return bool whether strings are equal.
508
     */
509
    public function compareString($expected, $actual)
510
    {
511
        return hash_equals($expected, $actual);
512
    }
513
514
    /**
515
     * Masks a token to make it uncompressible.
516
     * Applies a random mask to the token and prepends the mask used to the result making the string always unique.
517
     * Used to mitigate BREACH attack by randomizing how token is outputted on each request.
518
     * @param string $token An unmasked token.
519
     * @return string A masked token.
520
     * @since 2.0.12
521
     */
522 38
    public function maskToken($token)
523
    {
524
        // The number of bytes in a mask is always equal to the number of bytes in a token.
525 38
        $mask = $this->generateRandomKey(StringHelper::byteLength($token));
526 37
        return StringHelper::base64UrlEncode($mask . ($mask ^ $token));
527
    }
528
529
    /**
530
     * Unmasks a token previously masked by `maskToken`.
531
     * @param string $maskedToken A masked token.
532
     * @return string An unmasked token, or an empty string in case of token format is invalid.
533
     * @since 2.0.12
534
     */
535 8
    public function unmaskToken($maskedToken)
536
    {
537 8
        $decoded = StringHelper::base64UrlDecode($maskedToken);
538 8
        $length = StringHelper::byteLength($decoded) / 2;
539
        // Check if the masked token has an even length.
540 8
        if (!is_int($length)) {
541 1
            return '';
542
        }
543 8
        return StringHelper::byteSubstr($decoded, $length, $length) ^ StringHelper::byteSubstr($decoded, 0, $length);
544
    }
545
}
546