1 | <?php |
||
34 | final class Mcrypt extends Cryptobase |
||
35 | { |
||
36 | |||
37 | /** |
||
38 | * Decrypt cyphertext |
||
39 | * |
||
40 | * @param string $cyphertext Cypher text to decrypt |
||
41 | * @param string $password Password that should be used to decrypt input data |
||
42 | * @param int $cost Number of HMAC iterations to perform on key |
||
43 | * @param string $cipher Mcrypt cipher |
||
44 | * @param string $mode Mcrypt mode |
||
45 | * @param string $algo Hashing algorithm to use for internal operations |
||
46 | * |
||
47 | * @return string|boolean Returns false on checksum validation failure |
||
48 | */ |
||
49 | 4 | public static function decrypt($cyphertext, $password, $cost = 0, $cipher = MCRYPT_RIJNDAEL_128, $mode = MCRYPT_MODE_CBC, $algo = 'sha256') |
|
50 | { |
||
51 | // Determine that size of the IV in bytes |
||
52 | 4 | $ivsize = \mcrypt_get_iv_size($cipher, $mode); |
|
53 | |||
54 | // Find the IV at the beginning of the cypher text |
||
55 | 4 | $iv = Str::substr($cyphertext, 0, $ivsize); |
|
56 | |||
57 | // Gather the checksum portion of the cypher text |
||
58 | 4 | $chksum = Str::substr($cyphertext, $ivsize, Str::hashSize($algo)); |
|
59 | |||
60 | // Gather message portion of cyphertext after iv and checksum |
||
61 | 4 | $message = Str::substr($cyphertext, $ivsize + Str::hashSize($algo)); |
|
62 | |||
63 | // Derive key from password |
||
64 | 4 | $key = self::key($password, $iv, $cost, $cipher, $mode, $algo); |
|
65 | |||
66 | // Calculate verification checksum |
||
67 | 4 | $verify = self::checksum($message, $iv, $key, $cipher, $mode, $algo); |
|
68 | |||
69 | // If checksum could not be verified return false |
||
70 | 4 | self::checksumVerify($verify, $chksum); |
|
71 | |||
72 | // Decrypt unpad return |
||
73 | 4 | return Pkcs7::unpad(\mcrypt_decrypt($cipher, $key, $message, $mode, $iv)); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * Encrypt plaintext |
||
78 | * |
||
79 | * @param string $plaintext Plaintext string to encrypt |
||
80 | * @param string $password Key used to encrypt data |
||
81 | * @param int $cost Number of HMAC iterations to perform on key |
||
82 | * @param string $cipher Mcrypt cipher |
||
83 | * @param string $mode Mcrypt mode |
||
84 | * @param string $algo Hashing algorithm to use for internal operations |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 1 | public static function encrypt($plaintext, $password, $cost = 0, $cipher = MCRYPT_RIJNDAEL_128, $mode = MCRYPT_MODE_CBC, $algo = 'sha256') |
|
108 | |||
109 | } |
||
110 |