3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * http://stackoverflow.com/questions/9262109/php-simplest-two-way-encryption/30189841#30189841 * * This is not safe to use */ class UnsafeCrypto { const METHOD = 'aes-256-ctr'; /** * Encrypts (but does not authenticate) a message * * @param string $message - plaintext message * @param string $key - encryption key (raw binary expected) * @param boolean $encode - set to TRUE to return a base64-encoded * @return string (raw binary) */ public static function encrypt($message, $key, $encode = false) { $nonceSize = openssl_cipher_iv_length(self::METHOD); $nonce = openssl_random_pseudo_bytes($nonceSize); $ciphertext = openssl_encrypt( $message, self::METHOD, $key, OPENSSL_RAW_DATA, $nonce ); // Now let's pack the IV and the ciphertext together // Naively, we can just concatenate if ($encode) { return base64_encode($nonce.$ciphertext); } return $nonce.$ciphertext; } /** * Decrypts (but does not verify) a message * * @param string $message - ciphertext message * @param string $key - encryption key (raw binary expected) * @param boolean $encoded - are we expecting an encoded string? * @return string */ public static function decrypt($message, $key, $encoded = false) { if ($encoded) { $message = base64_decode($message, true); if ($message === false) { throw new Exception('Encryption failure'); } } $nonceSize = openssl_cipher_iv_length(self::METHOD); $nonce = mb_substr($message, 0, $nonceSize, '8bit'); $ciphertext = mb_substr($message, $nonceSize, null, '8bit'); $plaintext = openssl_decrypt( $ciphertext, self::METHOD, $key, OPENSSL_RAW_DATA, $nonce ); return $plaintext; } } class SaferCrypto extends UnsafeCrypto { const HASH_ALGO = 'sha256'; /** * Encrypts then MACs a message * * @param string $message - plaintext message * @param string $key - encryption key (raw binary expected) * @param boolean $encode - set to TRUE to return a base64-encoded string * @return string (raw binary) */ public static function encrypt($message, $key, $encode = false) { list($encKey, $authKey) = self::splitKeys($key); // Pass to UnsafeCrypto::encrypt $ciphertext = parent::encrypt($message, $encKey); // Calculate a MAC of the IV and ciphertext $mac = hash_hmac(self::HASH_ALGO, $ciphertext, $authKey, true); if ($encode) { return base64_encode($mac.$ciphertext); } // Prepend MAC to the ciphertext and return to caller return $mac.$ciphertext; } /** * Decrypts a message (after verifying integrity) * * @param string $message - ciphertext message * @param string $key - encryption key (raw binary expected) * @param boolean $encoded - are we expecting an encoded string? * @return string (raw binary) */ public static function decrypt($message, $key, $encoded = false) { list($encKey, $authKey) = self::splitKeys($key); if ($encoded) { $message = base64_decode($message, true); if ($message === false) { throw new Exception('Encryption failure'); } } // Hash Size -- in case HASH_ALGO is changed $hs = mb_strlen(hash(self::HASH_ALGO, '', true), '8bit'); $mac = mb_substr($message, 0, $hs, '8bit'); $ciphertext = mb_substr($message, $hs, null, '8bit'); $calculated = hash_hmac( self::HASH_ALGO, $ciphertext, $authKey, true ); if (!self::hashEquals($mac, $calculated)) { throw new Exception('Encryption failure'); } // Pass to UnsafeCrypto::decrypt $plaintext = parent::decrypt($ciphertext, $encKey); return $plaintext; } /** * Splits a key into two separate keys; one for encryption * and the other for authenticaiton * * @param string $masterKey (raw binary) * @return array (two raw binary strings) */ protected static function splitKeys($masterKey) { // You really want to implement HKDF here instead! return [ hash_hmac(self::HASH_ALGO, 'ENCRYPTION', $masterKey, true), hash_hmac(self::HASH_ALGO, 'AUTHENTICATION', $masterKey, true) ]; } /** * Compare two strings without leaking timing information * * @param string $a * @param string $b * @return boolean */ protected static function hashEquals($a, $b) { if (function_exists('hash_equals')) { return hash_equals($a, $b); } $nonce = openssl_random_pseudo_bytes(32); return hash_hmac(self::HASH_ALGO, $a, $nonce) === hash_hmac(self::HASH_ALGO, $b, $nonce); } } $message = 'Ready your ammunition; we attack at dawn.'; $key = hex2bin('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'); $encrypted = SaferCrypto::encrypt($message, $key, true); $decrypted = SaferCrypto::decrypt($encrypted, $key, true); var_dump($encrypted, $decrypted);

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.140.0110.00716.80
8.3.130.0120.00918.63
8.3.120.0070.01118.83
8.3.110.0060.00320.94
8.3.100.0040.00424.06
8.3.90.0050.00526.77
8.3.80.0210.00726.66
8.3.70.0130.00916.88
8.3.60.0070.01323.47
8.3.50.0260.00921.10
8.3.40.0160.00520.35
8.3.30.0110.00918.97
8.3.20.0120.00221.03
8.3.10.0060.01021.26
8.3.00.0090.00718.64
8.2.250.0000.00816.82
8.2.240.0100.00618.74
8.2.230.0060.00322.58
8.2.220.0100.00037.54
8.2.210.0150.00426.77
8.2.200.0030.00616.88
8.2.190.0000.01418.54
8.2.180.0140.00718.54
8.2.170.0070.01121.00
8.2.160.0090.00920.30
8.2.150.0110.00421.64
8.2.140.0120.00819.82
8.2.130.0120.00421.98
8.2.120.0060.01018.78
8.2.110.0100.00719.86
8.2.100.0150.00518.65
8.2.90.0140.00618.67
8.2.80.0130.00718.96
8.2.70.0110.00528.47
8.2.60.0120.00628.46
8.2.50.0130.00628.87
8.2.40.0110.00628.99
8.2.30.0100.00729.03
8.2.20.0130.00428.48
8.2.10.0100.00424.06
8.2.00.0090.00625.89
8.1.300.0080.00018.47
8.1.290.0070.00430.84
8.1.280.0090.00625.92
8.1.270.0080.00522.84
8.1.260.0060.00423.54
8.1.250.0060.00724.19
8.1.240.0100.00718.87
8.1.230.0120.00419.60
8.1.220.0120.00518.48
8.1.210.0110.00518.60
8.1.200.0130.00430.51
8.1.190.0100.00628.29
8.1.180.0100.00528.32
8.1.170.0110.00528.89
8.1.160.0130.00428.83
8.1.150.0110.00528.82
8.1.140.0090.00426.24
8.1.130.0090.00728.70
8.1.120.0100.00628.27
8.1.110.0110.00528.27
8.1.100.0100.00628.53
8.1.90.0120.00428.26
8.1.80.0110.00628.55
8.1.70.0110.00528.36
8.1.60.0100.00526.83
8.1.50.0080.00626.83
8.1.40.0110.00424.24
8.1.30.0120.00423.22
8.1.20.0140.00524.38
8.1.10.0150.00524.10
8.1.00.0160.00523.98
8.0.300.0120.00418.67
8.0.290.0150.00330.45
8.0.280.0130.00428.07
8.0.270.0130.00428.02
8.0.260.0130.00627.91
8.0.250.0140.00528.21
8.0.240.0100.00728.20
8.0.230.0100.00728.22
8.0.220.0120.00728.16
8.0.210.0120.00628.16
8.0.200.0140.00628.20
8.0.190.0150.00326.60
8.0.180.0130.00526.60
8.0.170.0130.00525.38
8.0.160.0140.00523.73
8.0.150.0150.00523.66
8.0.140.0150.00523.64
8.0.130.0130.00623.30
8.0.120.0130.00723.67
8.0.110.0150.00423.72
8.0.100.0120.00522.52
8.0.90.0130.00622.86
8.0.80.0160.00422.40
8.0.70.0130.00622.87
8.0.60.0130.00722.92
8.0.50.0130.00722.92
8.0.30.0120.00721.20
8.0.20.0140.00720.96
8.0.10.0150.00621.75
8.0.00.0110.00721.08
7.4.330.0100.00435.37
7.4.320.0140.00627.56
7.4.300.0160.00627.62
7.4.290.0140.00324.14
7.4.280.0160.00422.77
7.4.270.0150.00523.36
7.4.260.0120.00722.25
7.4.250.0160.00523.20
7.4.240.0130.00722.74
7.4.230.0150.00623.22
7.4.220.0140.00522.60
7.4.210.0130.00521.32
7.4.200.0120.00621.96
7.4.190.0130.00622.69
7.4.180.0150.00422.70
7.4.160.0120.00821.05
7.4.150.0150.00621.10
7.4.140.0140.00620.84
7.4.130.0120.00620.36
7.4.120.0130.00620.67
7.4.110.0120.00620.93
7.4.100.0150.00520.44
7.4.90.0140.00620.23
7.4.80.0120.00720.47
7.4.70.0110.00820.32
7.4.60.0130.00720.24
7.4.50.0110.00720.16
7.4.40.0130.00720.18
7.4.30.0110.00920.30
7.4.20.0110.00819.38
7.4.10.0140.00619.69
7.4.00.0120.00819.31
7.3.330.0140.00422.94
7.3.320.0150.00423.01
7.3.310.0140.00422.66
7.3.300.0140.00523.20
7.3.290.0140.00521.98
7.3.280.0130.00721.22
7.3.270.0140.00620.97
7.3.260.0150.00620.90
7.3.250.0130.00620.36
7.3.240.0140.00620.90
7.3.230.0140.00620.90
7.3.220.0150.00620.59
7.3.210.0140.00520.13
7.3.200.0120.00720.21
7.3.190.0130.00820.12
7.3.180.0140.00620.09
7.3.170.0130.00720.17
7.3.160.0130.00720.18
7.3.150.0130.00720.10
7.3.140.0130.00819.91
7.3.130.0130.00719.59
7.3.120.0130.00619.36
7.3.110.0120.00719.32
7.3.100.0130.00719.12
7.3.90.0110.00819.30
7.3.80.0140.00619.14
7.3.70.0110.00818.81
7.3.60.0120.00618.72
7.3.50.0120.00718.50
7.3.40.0110.00818.54
7.3.30.0120.00718.49
7.3.20.0160.00518.82
7.3.10.0160.00718.60
7.3.00.0150.00718.65
7.2.340.0160.00721.14
7.2.330.0160.00820.18
7.2.320.0160.00720.15
7.2.310.0150.00720.26
7.2.300.0150.00720.14
7.2.290.0150.00720.14
7.2.280.0140.00720.16
7.2.270.0120.00919.98
7.2.260.0160.00719.69
7.2.250.0160.00619.46
7.2.240.0150.00719.42
7.2.230.0130.00819.27
7.2.220.0140.00819.23
7.2.210.0140.00719.23
7.2.200.0150.00618.99
7.2.190.0130.00818.76
7.2.180.0130.00918.58
7.2.170.0130.00818.73
7.2.160.0140.00818.81
7.2.150.0170.00919.09
7.2.140.0160.00919.15
7.2.130.0170.00818.90
7.2.120.0220.00818.90
7.2.110.0160.00818.78
7.2.100.0190.00718.78
7.2.90.0160.00718.79
7.2.80.0160.00818.77
7.2.70.0170.00718.76
7.2.60.0180.00618.73
7.2.50.0180.00718.73
7.2.40.0170.00818.79
7.2.30.0170.00818.79
7.2.20.0160.00918.73
7.2.10.0180.00718.78
7.2.00.0190.00818.74
7.1.330.0140.00818.99
7.1.320.0170.00818.91
7.1.310.0150.00618.90
7.1.300.0150.00918.37
7.1.290.0190.00718.38
7.1.280.0160.00818.19
7.1.270.0170.00618.27
7.1.260.0140.00718.22
7.1.250.0170.00717.82
7.1.240.0170.00817.99
7.1.230.0190.00717.94
7.1.220.0180.00717.91
7.1.210.0190.00917.97
7.1.200.0190.00917.84
7.1.190.0190.00717.89
7.1.180.0180.00717.84
7.1.170.0190.00717.84
7.1.160.0200.00717.89
7.1.150.0180.00717.85
7.1.140.0150.00917.85
7.1.130.0190.00817.85
7.1.120.0220.00717.83
7.1.110.0190.00817.85
7.1.100.0200.00717.85
7.1.90.0190.00817.82
7.1.80.0210.00717.86
7.1.70.0180.00817.79
7.1.60.0210.00617.79
7.1.50.0180.00817.90
7.1.40.0180.00817.89
7.1.30.0190.00717.82
7.1.20.0190.00717.89
7.1.10.0160.00817.94
7.1.00.0190.00817.82
7.0.330.0210.00617.67
7.0.320.0180.00717.49
7.0.310.0190.00617.59
7.0.300.0190.00817.61
7.0.290.0180.00717.55
7.0.280.0200.00717.55
7.0.270.0180.00717.58
7.0.260.0200.00717.58
7.0.250.0190.00717.67
7.0.240.0190.00717.65
7.0.230.0180.00717.66
7.0.220.0190.00917.63
7.0.210.0200.00717.59
7.0.200.0200.00717.56
7.0.190.0230.00717.61
7.0.180.0170.00817.60
7.0.170.0180.00717.60
7.0.160.0200.00617.69
7.0.150.0220.00817.56
7.0.140.0200.00617.53
7.0.130.0180.00817.66
7.0.120.0190.00717.62
7.0.110.0180.00917.72
7.0.100.0210.00617.66
7.0.90.0210.00617.56
7.0.80.0180.00717.61
7.0.70.0210.00817.56
7.0.60.0190.00817.66
7.0.50.0170.00717.60
7.0.40.0170.00717.61
7.0.30.0180.00717.65
7.0.20.0190.00717.61
7.0.10.0220.00717.66
7.0.00.0210.00817.62
5.6.400.0140.00818.21
5.6.390.0140.00918.22
5.6.380.0140.00917.74
5.6.370.0140.00817.77
5.6.360.0160.00717.77
5.6.350.0140.00817.82
5.6.340.0140.00917.82
5.6.330.0150.00817.78
5.6.320.0160.00817.82
5.6.310.0140.00817.82
5.6.300.0140.00917.72
5.6.290.0150.00717.82
5.6.280.0140.00817.71
5.6.270.0140.00917.83
5.6.260.0150.00817.80
5.6.250.0140.01017.84
5.6.240.0140.00917.84
5.6.230.0130.00917.84
5.6.220.0150.00917.79
5.6.210.0150.00817.86
5.6.200.0170.00617.86
5.6.190.0160.00717.88
5.6.180.0140.00817.85
5.6.170.0130.01017.86
5.6.160.0140.00817.89
5.6.150.0150.00917.93
5.6.140.0150.00717.71
5.6.130.0150.00917.75
5.6.120.0140.00817.71
5.6.110.0150.00817.66
5.6.100.0150.00717.71
5.6.90.0140.00917.66
5.6.80.0150.00817.65
5.6.70.0130.01017.68
5.6.60.0130.00817.67
5.6.50.0140.00817.66
5.6.40.0130.01017.67
5.6.30.0140.00817.68
5.6.20.0140.00817.63
5.6.10.0130.00917.63
5.6.00.0120.01017.64
5.5.380.0140.00817.48
5.5.370.0130.00817.50
5.5.360.0140.00817.44
5.5.350.0160.00817.44
5.5.340.0140.00917.40
5.5.330.0150.00717.39
5.5.320.0120.00917.40
5.5.310.0150.00717.39
5.5.300.0140.00917.17
5.5.290.0140.00817.21
5.5.280.0140.00817.15
5.5.270.0130.00917.18
5.5.260.0140.00817.18
5.5.250.0140.00817.24
5.5.240.0130.00817.24
5.5.230.0130.00817.17
5.5.220.0150.00817.18
5.5.210.0130.00817.25
5.5.200.0130.00817.16
5.5.190.0150.00717.16
5.5.180.0140.00817.17
5.5.170.0130.01017.33
5.5.160.0140.00817.15
5.5.150.0120.00917.18
5.5.140.0140.00717.16
5.5.130.0130.00717.06
5.5.120.0140.00717.11
5.5.110.0140.00817.04
5.5.100.0130.00817.14
5.5.90.0140.00817.19
5.5.80.0130.00717.18
5.5.70.0140.00817.20
5.5.60.0140.00717.19
5.5.50.0130.00917.21
5.5.40.0130.00917.19
5.5.30.0140.00717.19
5.5.20.0140.00717.16
5.5.10.0130.00817.14
5.5.00.0140.00817.15
5.4.450.0110.00514.96
5.4.440.0100.00715.13
5.4.430.0100.00715.10
5.4.420.0100.00715.15
5.4.410.0100.00615.13
5.4.400.0110.00515.08
5.4.390.0120.00515.08
5.4.380.0110.00615.05
5.4.370.0110.00615.08
5.4.360.0120.00615.03
5.4.350.0110.00615.07
5.4.340.0110.00615.04
5.4.330.0110.00615.07
5.4.320.0110.00615.07
5.4.310.0110.00515.10
5.4.300.0110.00515.08
5.4.290.0110.00615.04
5.4.280.0110.00515.09
5.4.270.0110.00615.08
5.4.260.0110.00615.03
5.4.250.0120.00615.04
5.4.240.0110.00615.07
5.4.230.0090.00715.02
5.4.220.0110.00515.03
5.4.210.0110.00615.05
5.4.200.0120.00614.99
5.4.190.0100.00815.03
5.4.180.0100.00615.01
5.4.170.0100.00615.04
5.4.160.0120.00615.05
5.4.150.0110.00615.00
5.4.140.0110.00715.08
5.4.130.0110.00615.09
5.4.120.0100.00715.09
5.4.110.0100.00715.12
5.4.100.0110.00515.10
5.4.90.0120.00515.12
5.4.80.0090.00715.11
5.4.70.0100.00615.08
5.4.60.0100.00615.16
5.4.50.0120.00515.07
5.4.40.0110.00515.08
5.4.30.0110.00515.15
5.4.20.0110.00615.11
5.4.10.0100.00615.11
5.4.00.0110.00515.07

preferences:
15.7 ms | 403 KiB | 5 Q