3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Illuminate\Encryption; use Symfony\Component\Security\Core\Util\StringUtils; use Symfony\Component\Security\Core\Util\SecureRandom; class DecryptException extends \RuntimeException {} class Encrypter { /** * The encryption key. * * @var string */ protected $key; /** * The algorithm used for encryption. * * @var string */ protected $cipher = 'rijndael-256'; /** * The mode used for encryption. * * @var string */ protected $mode = 'cbc'; /** * The block size of the cipher. * * @var int */ protected $block = 32; /** * Create a new encrypter instance. * * @param string $key * @return void */ public function __construct($key) { $this->key = $key; } /** * Encrypt the given value. * * @param string $value * @return string */ public function encrypt($value) { $iv = mcrypt_create_iv($this->getIvSize(), $this->getRandomizer()); $value = base64_encode($this->padAndMcrypt($value, $iv)); // Once we have the encrypted value we will go ahead base64_encode the input // vector and create the MAC for the encrypted value so we can verify its // authenticity. Then, we'll JSON encode the data in a "payload" array. $mac = $this->hash($iv = base64_encode($iv), $value); return base64_encode(json_encode(compact('iv', 'value', 'mac'))); } /** * Pad and use mcrypt on the given value and input vector. * * @param string $value * @param string $iv * @return string */ protected function padAndMcrypt($value, $iv) { $value = $this->addPadding(serialize($value)); return mcrypt_encrypt($this->cipher, $this->key, $value, $this->mode, $iv); } /** * Decrypt the given value. * * @param string $payload * @return string */ public function decrypt($payload) { $payload = $this->getJsonPayload($payload); // We'll go ahead and remove the PKCS7 padding from the encrypted value before // we decrypt it. Once we have the de-padded value, we will grab the vector // and decrypt the data, passing back the unserialized from of the value. $value = base64_decode($payload['value']); $iv = base64_decode($payload['iv']); return unserialize($this->stripPadding($this->mcryptDecrypt($value, $iv))); } /** * Run the mcrypt decryption routine for the value. * * @param string $value * @param string $iv * @return string */ protected function mcryptDecrypt($value, $iv) { return mcrypt_decrypt($this->cipher, $this->key, $value, $this->mode, $iv); } /** * Get the JSON array from the given payload. * * @param string $payload * @return array * * @throws DecryptException */ protected function getJsonPayload($payload) { $payload = json_decode(base64_decode($payload), true); // If the payload is not valid JSON or does not have the proper keys set we will // assume it is invalid and bail out of the routine since we will not be able // to decrypt the given value. We'll also check the MAC for this encryption. if ( ! $payload || $this->invalidPayload($payload)) { throw new DecryptException("Invalid data."); } if ( ! $this->validMac($payload)) { throw new DecryptException("MAC is invalid."); } return $payload; } /** * Determine if the MAC for the given payload is valid. * * @param array $payload * @return bool */ protected function validMac(array $payload) { $bytes = with(new SecureRandom)->nextBytes(16); $calcMac = hash_hmac('sha256', $this->hash($payload['iv'], $payload['value']), $bytes, true); return StringUtils::equals(hash_hmac('sha256', $payload['mac'], $bytes, true), $calcMac); } /** * Create a MAC for the given value. * * @param string $iv * @param string $value * @return string */ protected function hash($iv, $value) { return hash_hmac('sha256', $iv.$value, $this->key); } /** * Add PKCS7 padding to a given value. * * @param string $value * @return string */ protected function addPadding($value) { $pad = $this->block - (strlen($value) % $this->block); return $value.str_repeat(chr($pad), $pad); } /** * Remove the padding from the given value. * * @param string $value * @return string */ protected function stripPadding($value) { $pad = ord($value[($len = strlen($value)) - 1]); return $this->paddingIsValid($pad, $value) ? substr($value, 0, $len - $pad) : $value; } /** * Determine if the given padding for a value is valid. * * @param string $pad * @param string $value * @return bool */ protected function paddingIsValid($pad, $value) { $beforePad = strlen($value) - $pad; return substr($value, $beforePad) == str_repeat(substr($value, -1), $pad); } /** * Verify that the encryption payload is valid. * * @param array|mixed $data * @return bool */ protected function invalidPayload($data) { return ! is_array($data) || ! isset($data['iv']) || ! isset($data['value']) || ! isset($data['mac']); } /** * Get the IV size for the cipher. * * @return int */ protected function getIvSize() { return mcrypt_get_iv_size($this->cipher, $this->mode); } /** * Get the random data source available for the OS. * * @return int */ protected function getRandomizer() { if (defined('MCRYPT_DEV_URANDOM')) return MCRYPT_DEV_URANDOM; if (defined('MCRYPT_DEV_RANDOM')) return MCRYPT_DEV_RANDOM; mt_srand(); return MCRYPT_RAND; } /** * Set the encryption key. * * @param string $key * @return void */ public function setKey($key) { $this->key = $key; } /** * Set the encryption cipher. * * @param string $cipher * @return void */ public function setCipher($cipher) { $this->cipher = $cipher; } /** * Set the encryption mode. * * @param string $mode * @return void */ public function setMode($mode) { $this->mode = $mode; } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0100.01018.07
8.3.50.0130.00522.75
8.3.40.0000.01518.77
8.3.30.0070.00719.13
8.3.20.0040.00420.34
8.3.10.0140.00023.64
8.3.00.0080.00017.38
8.2.180.0100.00716.50
8.2.170.0130.00322.96
8.2.160.0090.00620.55
8.2.150.0040.00424.18
8.2.140.0040.00424.66
8.2.130.0040.00426.16
8.2.120.0080.00021.00
8.2.110.0070.00322.26
8.2.100.0060.00617.89
8.2.90.0030.00617.75
8.2.80.0030.00517.97
8.2.70.0050.00318.03
8.2.60.0050.00318.22
8.2.50.0050.00518.10
8.2.40.0050.00718.95
8.2.30.0060.00318.02
8.2.20.0000.00717.75
8.2.10.0040.00418.13
8.2.00.0000.00817.78
8.1.280.0150.00425.92
8.1.270.0080.00023.96
8.1.260.0040.00426.35
8.1.250.0070.00028.09
8.1.240.0060.00323.96
8.1.230.0030.00719.13
8.1.220.0050.00517.79
8.1.210.0000.00818.77
8.1.200.0060.00317.22
8.1.190.0040.00417.22
8.1.180.0040.00418.10
8.1.170.0000.00818.63
8.1.160.0040.00422.03
8.1.150.0070.00018.75
8.1.140.0040.00417.37
8.1.130.0070.00017.84
8.1.120.0040.00417.32
8.1.110.0030.00517.30
8.1.100.0030.00317.45
8.1.90.0000.00717.30
8.1.80.0040.00417.40
8.1.70.0000.00717.38
8.1.60.0030.00517.54
8.1.50.0080.00017.44
8.1.40.0060.00317.49
8.1.30.0080.00017.50
8.1.20.0000.00917.48
8.1.10.0040.00417.35
8.1.00.0030.00617.27
8.0.300.0060.00319.90
8.0.290.0000.00816.75
8.0.280.0040.00418.37
8.0.270.0000.00717.19
8.0.260.0030.00317.22
8.0.250.0050.00316.98
8.0.240.0100.00016.96
8.0.230.0030.00316.88
8.0.220.0070.00016.86
8.0.210.0040.00416.86
8.0.200.0060.00016.88
8.0.190.0000.00716.95
8.0.180.0050.00316.83
8.0.170.0040.00416.85
8.0.160.0000.00716.72
8.0.150.0090.00016.82
8.0.140.0050.00316.77
8.0.130.0070.00013.16
8.0.120.0000.00816.88
8.0.110.0000.00816.77
8.0.100.0000.00716.85
8.0.90.0040.00416.76
8.0.80.0130.00616.91
8.0.70.0000.00816.82
8.0.60.0000.00716.73
8.0.50.0050.00216.72
8.0.30.0090.01016.94
8.0.20.0110.01017.40
8.0.10.0000.00717.00
8.0.00.0090.00916.75
7.4.330.0020.00215.03
7.4.320.0030.00316.57
7.4.300.0000.00816.57
7.4.290.0020.00516.42
7.4.280.0050.00216.52
7.4.270.0030.00316.48
7.4.260.0080.00016.60
7.4.250.0000.00716.52
7.4.240.0050.00216.46
7.4.230.0070.00016.49
7.4.220.0120.01216.34
7.4.210.0110.00416.65
7.4.200.0030.00616.68
7.4.160.0070.00716.39
7.4.150.0200.00017.40
7.4.140.0070.01217.86
7.4.130.0060.01216.40
7.4.120.0120.00516.39
7.4.110.0030.01416.29
7.4.100.0120.00516.29
7.4.90.0130.00616.57
7.4.80.0150.00719.39
7.4.70.0100.00716.58
7.4.60.0130.00416.38
7.4.50.0020.00216.07
7.4.40.0130.00316.60
7.4.30.0140.00716.39
7.4.00.0110.00514.97
7.3.330.0060.00313.13
7.3.320.0030.00313.02
7.3.310.0030.00316.16
7.3.300.0030.00316.22
7.3.290.0100.01016.20
7.3.280.0070.01016.29
7.3.270.0110.00617.40
7.3.260.0100.00616.25
7.3.240.0100.00616.48
7.3.230.0120.00616.39
7.3.210.0140.00516.45
7.3.200.0120.00419.39
7.3.190.0100.00716.51
7.3.180.0080.00816.39
7.3.170.0130.00316.61
7.3.160.0030.01416.30
7.3.120.0050.01314.78
7.3.110.0070.00814.89
7.3.100.0050.00814.91
7.3.90.0060.00514.83
7.3.80.0020.01314.60
7.3.70.0020.01214.66
7.3.60.0040.00914.85
7.3.50.0070.00714.73
7.3.40.0050.01014.82
7.3.30.0020.01414.75
7.3.20.0050.00816.50
7.3.10.0040.01016.43
7.3.00.0080.00616.59
7.2.330.0040.01416.60
7.2.320.0100.01016.70
7.2.310.0030.01916.43
7.2.300.0100.00616.21
7.2.290.0100.00616.52
7.2.250.0070.01114.85
7.2.240.0090.01015.02
7.2.230.0100.00714.91
7.2.220.0030.00914.81
7.2.210.0020.01215.13
7.2.200.0020.00914.91
7.2.190.0050.00914.73
7.2.180.0080.00714.65
7.2.170.0050.00915.01
7.2.160.0130.00314.83
7.2.150.0000.01416.73
7.2.140.0030.01016.68
7.2.130.0030.01016.55
7.2.120.0090.00616.76
7.2.110.0100.00316.71
7.2.100.0070.00716.74
7.2.90.0080.00416.43
7.2.80.0030.01016.68
7.2.70.0040.01116.44
7.2.60.0030.00716.75
7.2.50.0030.01016.84
7.2.40.0000.01416.69
7.2.30.0090.00316.67
7.2.20.0140.00016.59
7.2.10.0110.00416.72
7.2.00.0070.00716.54
7.1.330.0070.00715.48
7.1.320.0070.00815.50
7.1.310.0050.00915.60
7.1.300.0050.00915.69
7.1.290.0040.01115.44
7.1.280.0000.01215.37
7.1.270.0080.00415.61
7.1.260.0080.00615.52
7.1.250.0110.00415.38
7.1.200.0000.01515.43
7.1.100.0000.00917.98
7.1.70.0030.01317.08
7.1.60.0070.01719.40
7.1.50.0110.01116.82
7.1.00.0030.07722.37
7.0.200.0070.00316.81
7.0.140.0070.06721.98
7.0.120.0000.05021.92
7.0.110.0030.04321.98
7.0.100.0030.04722.20
7.0.90.0000.04322.00
7.0.80.0000.08322.00
7.0.70.0030.05722.00
7.0.60.0130.07021.99
7.0.50.0030.04322.07
7.0.40.0000.07720.02
7.0.30.0000.04020.16
7.0.20.0000.04019.96
7.0.10.0000.04020.00
7.0.00.0070.03720.15
5.6.280.0130.06020.98
5.6.270.0000.06020.90
5.6.260.0030.04321.13
5.6.250.0030.05021.05
5.6.240.0030.04720.93
5.6.230.0000.04020.85
5.6.220.0030.06320.81
5.6.210.0000.05320.94
5.6.200.0100.03320.94
5.6.190.0000.07320.95
5.6.180.0000.07020.80
5.6.170.0030.04021.08
5.6.160.0000.04020.93
5.6.150.0100.03720.83
5.6.140.0030.04020.85
5.6.130.0000.04321.00
5.6.120.0070.07721.03
5.6.110.0000.05320.84
5.6.100.0000.05021.05
5.6.90.0070.04320.81
5.6.80.0000.03720.24
5.6.70.0070.05320.31
5.6.60.0000.07020.40
5.6.50.0000.04020.45
5.6.40.0000.06320.45
5.6.30.0100.04020.45
5.6.20.0030.04020.40
5.6.10.0030.05320.36
5.6.00.0030.03720.14
5.5.380.0000.05017.65
5.5.370.0030.04317.57
5.5.360.0030.07017.52
5.5.350.0000.07717.68
5.5.340.0030.04017.99
5.5.330.0030.03718.00
5.5.320.0070.03317.91
5.5.310.0030.04017.97
5.5.300.0070.03317.97
5.5.290.0030.04017.90
5.5.280.0070.06318.25
5.5.270.0030.07317.98
5.5.260.0000.04317.95
5.5.250.0000.07017.98
5.5.240.0000.03717.67
5.5.230.0000.04317.38
5.5.220.0030.05017.37
5.5.210.0000.04017.46
5.5.200.0000.04017.46
5.5.190.0000.04017.52
5.5.180.0030.06317.55
5.5.160.0000.06017.48
5.5.150.0000.03717.51
5.5.140.0000.04017.55
5.5.130.0030.04017.63
5.5.120.0000.08717.38
5.5.110.0030.04317.47
5.5.100.0170.06317.25
5.5.90.0000.07017.41
5.5.80.0000.03717.51
5.5.70.0000.08017.21
5.5.60.0000.03717.22
5.5.50.0030.03717.52
5.5.40.0100.05017.23
5.5.30.0000.04017.37
5.5.20.0070.03017.43
5.5.10.0030.07317.21
5.5.00.0030.05017.39
5.4.450.0000.04019.59
5.4.440.0070.06319.58
5.4.430.0000.04719.26
5.4.420.0030.04019.25
5.4.410.0000.04019.32
5.4.400.0000.05319.26
5.4.390.0000.04018.95
5.4.380.0070.07319.11
5.4.370.0000.04018.98
5.4.360.0000.09019.13
5.4.350.0030.04319.13
5.4.340.0030.03718.94
5.4.320.0000.07718.96
5.4.310.0000.06719.17
5.4.300.0030.03318.94
5.4.290.0030.04018.97
5.4.280.0000.07719.03
5.4.270.0030.07719.26
5.4.260.0030.07719.11
5.4.250.0070.06718.96
5.4.240.0000.06319.25
5.4.230.0030.07319.08
5.4.220.0000.04019.17
5.4.210.0070.06718.96
5.4.200.0030.05719.12
5.4.190.0070.03319.10
5.4.180.0030.08019.10
5.4.170.0100.07018.97
5.4.160.0000.05318.93
5.4.150.0070.07319.23
5.4.140.0000.04016.38
5.4.130.0000.07316.38
5.4.120.0000.03716.59
5.4.110.0000.05716.66
5.4.100.0100.06716.50
5.4.90.0030.06716.54
5.4.80.0030.06716.55
5.4.70.0030.04316.41
5.4.60.0000.06716.25
5.4.50.0000.04716.29
5.4.40.0000.05316.38
5.4.30.0000.04316.53
5.4.20.0000.05016.47
5.4.10.0030.04016.38
5.4.00.0070.04716.07

preferences:
51.71 ms | 400 KiB | 5 Q