Completed
Push — master ( cf496c...a42f10 )
by Michael
17:56 queued 16:33
created

Str::strlen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Str.php.
7
 *
8
 * PHP version 7
9
 *
10
 * @category Dcrypt
11
 *
12
 * @author   Michael Meyer (mmeyer2k) <[email protected]>
13
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
14
 *
15
 * @link     https://github.com/mmeyer2k/dcrypt
16
 */
17
18
namespace Dcrypt;
19
20
/**
21
 * Provides time-safe string comparison facilities, and safe string operations
22
 * on systems that have mb_* function overloading enabled.
23
 *
24
 * The functions in this class were inspired by the symfony's StringUtils class.
25
 *
26
 * @category Dcrypt
27
 *
28
 * @author   Michael Meyer (mmeyer2k) <[email protected]>
29
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
30
 *
31
 * @link     https://github.com/mmeyer2k/dcrypt
32
 * @link     https://github.com/symfony/Security/blob/master/Core/Util/StringUtils.php
33
 * @link     https://php.net/manual/en/mbstring.overload.php
34
 */
35
final class Str
36
{
37
    /**
38
     * Compares two strings in constant time. Strings are hashed before
39
     * comparison so information is not leaked when strings are not of
40
     * equal length.
41
     *
42
     * @param string $known The string of known length to compare against
43
     * @param string $given The string that the user can control
44
     *
45
     * @return bool
46
     */
47 34
    public static function equal(string $known, string $given): bool
48
    {
49
        // Create some entropy
50 34
        $nonce = \random_bytes(16);
51
52
        // Prehash the input strings with the nonce
53 34
        $known = \hash_hmac('sha256', $known, $nonce, true);
54 34
        $given = \hash_hmac('sha256', $given, $nonce, true);
55
56 34
        return \hash_equals($known, $given);
57
    }
58
59
    /**
60
     * Determine the length of the output of a given hash algorithm in bytes.
61
     *
62
     * @param string $algo Name of algorithm to look up
63
     *
64
     * @return int
65
     */
66 35
    public static function hashSize(string $algo): int
67
    {
68 35
        return self::strlen(\hash($algo, 'hash me', true));
69
    }
70
71
    /**
72
     * Returns the number of bytes in a string.
73
     *
74
     * @param string $string The string whose length we wish to obtain
75
     *
76
     * @return int
77
     */
78 44
    public static function strlen(string $string): int
79
    {
80 44
        return \mb_strlen($string, '8bit');
81
    }
82
83
    /**
84
     * Returns part of a string.
85
     *
86
     * @param string $string The string whose length we wish to obtain
87
     * @param int    $start  Offset to start gathering output
88
     * @param int    $length Distance from starting offset to gather
89
     *
90
     * @return string
91
     */
92 33
    public static function substr(
93
        string $string,
94
        int $start,
95
        int $length = null
96
    ): string {
97 33
        return \mb_substr($string, $start, $length, '8bit');
98
    }
99
}
100