Completed
Push — master ( b6a777...9f567c )
by Michael
02:15
created

Cryptobase::checksumVerify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 6
1
<?php
2
3
/**
4
 * Cryptobase.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 functionality common to Dcrypt's block ciphers.
19
 *
20
 * @category Dcrypt
21
 * @package  Dcrypt
22
 * @author   Michael Meyer (mmeyer2k) <[email protected]>
23
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
24
 * @link     https://github.com/mmeyer2k/dcrypt
25
 * @link     https://apigen.ci/github/mmeyer2k/dcrypt/namespace-Dcrypt.html
26
 */
27
class Cryptobase
28
{
29
    /**
30
     * Create a message authentication checksum.
31
     *
32
     * @param string $cyphertext Cyphertext that needs a checksum.
33
     * @param string $iv         Initialization vector.
34
     * @param string $key        HMAC key
35
     * @param string $cipher     Cipher string
36
     * @param string $mode       Cipher mode string
37
     * @param string $algo       Hashing algorithm to use for internal operations
38
     *
39
     * @return string
40
     */
41 10
    protected static function checksum(string $cyphertext, string $iv, string $key, string $cipher = 'rijndael-128', string $mode = 'cbc', string $algo = 'sha256'): string
42
    {
43
        // Prevent potentially large string concat by hmac-ing the cyphertext
44
        // by itself...
45 10
        $sum = \hash_hmac($algo, $cyphertext, $key, true);
46
        
47
        // If algo is unknown, throw an exception
48 10
        if ($sum === false) {
49
            throw new \exception("$algo is not supported by hash_hmac"); // @codeCoverageIgnore
50
        }
51
52
        // ... then hash other elements with previous hmac and return
53 10
        return \hash_hmac($algo, $sum . $iv . $mode . $cipher, $key, true);
54
    }
55
56
    /**
57
     * Transform password into key and perform iterative HMAC (if specified)
58
     *
59
     * @param string $password Encryption key
60
     * @param string $iv       Initialization vector
61
     * @param int    $cost     Number of HMAC iterations to perform on key
62
     * @param string $cipher   Mcrypt cipher
63
     * @param string $mode     Mcrypt block mode
64
     * @param string $algo     Hashing algorithm to use for internal operations
65
     *
66
     * @return string
67
     */
68
    protected static function key(string $password, string $iv, int $cost, string $cipher = 'rijndael-128', string $mode = 'cbc', string $algo = 'sha256'): string
69 10
    {
70
        // Perform key derivation
71
        return Hash::ihmac($iv . $cipher . $mode, $password, $cost, $algo);
72 10
    }
73 1
74 1
    /**
75
     * Verify checksum during decryption step and throw error if mismatching.
76
     *
77 10
     * @param string $calculated
78
     * @param string $supplied
79
     */
80
    protected static function checksumVerify(string $calculated, string $supplied)
81
    {
82
        if (!Str::equal($calculated, $supplied)) {
83
            $e = 'Decryption can not proceed due to invalid cyphertext checksum.';
84
            throw new \InvalidArgumentException($e);
85
        }
86
    }
87
}
88