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

Cryptobase   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 61
ccs 9
cts 9
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checksum() 0 14 2
A key() 0 5 1
A checksumVerify() 0 7 2
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