Completed
Push — master ( 42aba3...e42700 )
by Michael
04:29
created

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