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); $decrypted = SaferCrypto::decrypt($encrypted, $key); 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.60.0150.00318.68
8.3.50.0110.00416.66
8.3.40.0110.00718.78
8.3.30.0070.00718.71
8.3.20.0060.00320.22
8.3.10.0040.00421.76
8.3.00.0080.00017.80
8.2.180.0060.00918.18
8.2.170.0110.00722.96
8.2.160.0000.01419.45
8.2.150.0040.00424.18
8.2.140.0060.00324.66
8.2.130.0040.00426.16
8.2.120.0050.00320.93
8.2.110.0110.00019.39
8.2.100.0060.00618.16
8.2.90.0000.00720.64
8.2.80.0050.00517.97
8.2.70.0030.00618.18
8.2.60.0060.00318.28
8.2.50.0000.00818.10
8.2.40.0000.00818.55
8.2.30.0060.00619.42
8.2.20.0020.00618.64
8.2.10.0070.00617.87
8.2.00.0070.00617.85
8.1.280.0100.00625.92
8.1.270.0050.00323.99
8.1.260.0000.00826.35
8.1.250.0050.00328.09
8.1.240.0090.00024.02
8.1.230.0040.00721.15
8.1.220.0130.00618.00
8.1.210.0060.00319.02
8.1.200.0080.00317.25
8.1.190.0000.00817.36
8.1.180.0000.00818.10
8.1.170.0140.00418.89
8.1.160.0040.00918.33
8.1.150.0090.00318.17
8.1.140.0090.00417.57
8.1.130.0090.00418.27
8.1.120.0090.00318.40
8.1.110.0090.00418.15
8.1.100.0080.00618.11
8.1.90.0130.00217.78
8.1.80.0070.00717.76
8.1.70.0090.00517.69
8.1.60.0090.00717.80
8.1.50.0110.00517.77
8.1.40.0080.00817.82
8.1.30.0110.00517.87
8.1.20.0070.00917.86
8.1.10.0120.00517.77
8.1.00.0130.00317.74
8.0.300.0000.00720.01
8.0.290.0000.00816.88
8.0.280.0090.00318.03
8.0.270.0100.00117.84
8.0.260.0090.00317.50
8.0.250.0070.00517.63
8.0.240.0060.00717.47
8.0.230.0060.00817.39
8.0.220.0110.00317.33
8.0.210.0100.00417.26
8.0.200.0100.00417.32
8.0.190.0090.00617.30
8.0.180.0090.00617.26
8.0.170.0100.00417.30
8.0.160.0080.00717.26
8.0.150.0080.00717.35
8.0.140.0070.00717.22
8.0.130.0090.00616.66
8.0.120.0090.00617.23
8.0.110.0090.00517.25
8.0.100.0100.00417.25
8.0.90.0100.00317.17
8.0.80.0100.00517.20
8.0.70.0100.00417.23
8.0.60.0070.00617.22
8.0.50.0100.00417.23
8.0.30.0090.00917.16
8.0.20.0110.00617.24
8.0.10.0110.00717.26
8.0.00.0090.00717.21
7.4.330.0060.00315.83
7.4.320.0110.00317.15
7.4.300.0090.00416.95
7.4.290.0080.00616.94
7.4.280.0100.00616.84
7.4.270.0100.00516.95
7.4.260.0080.00616.81
7.4.250.0110.00416.91
7.4.240.0110.00416.95
7.4.230.0070.00616.85
7.4.220.0100.00416.88
7.4.210.0100.00416.91
7.4.200.0080.00316.85
7.4.190.0090.00416.97
7.4.180.0070.00616.93
7.4.160.0090.00816.83
7.4.150.0080.00916.88
7.4.140.0090.00816.93
7.4.130.0080.00816.88
7.4.120.0080.00716.81
7.4.110.0090.00516.70
7.4.100.0080.00716.93
7.4.90.0100.00616.83
7.4.80.0120.00816.88
7.4.70.0100.00816.68
7.4.60.0110.00716.68
7.4.50.0110.00716.73
7.4.40.0100.00816.73
7.4.30.0080.00816.76
7.4.20.0080.01016.73
7.4.10.0090.00816.59
7.4.00.0080.00816.43
7.3.330.0110.00216.58
7.3.320.0090.00616.36
7.3.310.0090.00516.83
7.3.300.0090.00416.79
7.3.290.0110.00216.74
7.3.280.0080.00616.74
7.3.270.0100.00616.75
7.3.260.0100.00616.67
7.3.250.0090.00816.63
7.3.240.0090.00616.68
7.3.230.0090.00716.63
7.3.220.0090.00616.60
7.3.210.0100.00716.55
7.3.200.0110.00816.58
7.3.190.0110.00716.58
7.3.180.0100.00916.56
7.3.170.0120.00916.58
7.3.160.0130.00616.58
7.3.150.0110.00716.55
7.3.140.0100.00816.54
7.3.130.0110.00616.36
7.3.120.0100.00816.32
7.3.110.0100.00816.26
7.3.100.0090.00816.11
7.3.90.0090.00916.22
7.3.80.0090.00916.09
7.3.70.0090.00816.09
7.3.60.0090.00716.11
7.3.50.0100.00816.14
7.3.40.0110.00716.06
7.3.30.0100.00916.09
7.3.20.0090.00916.67
7.3.10.0150.00716.58
7.3.00.0160.00916.49
7.2.340.0130.00816.60
7.2.330.0110.00916.65
7.2.320.0120.01016.66
7.2.310.0120.01016.60
7.2.300.0140.00716.60
7.2.290.0110.00916.61
7.2.280.0100.00916.64
7.2.270.0110.00916.59
7.2.260.0130.00716.43
7.2.250.0100.01016.40
7.2.240.0110.00916.37
7.2.230.0110.00916.19
7.2.220.0120.00816.22
7.2.210.0100.00916.19
7.2.200.0120.00816.30
7.2.190.0110.00816.24
7.2.180.0110.00916.25
7.2.170.0110.00916.24
7.2.160.0120.00816.30
7.2.150.0120.00816.88
7.2.140.0120.00916.89
7.2.130.0160.00816.88
7.2.120.0130.01016.78
7.2.110.0140.00916.82
7.2.100.0130.01016.80
7.2.90.0130.00916.78
7.2.80.0140.00816.79
7.2.70.0130.00816.79
7.2.60.0120.00916.76
7.2.50.0140.00816.82
7.2.40.0140.00816.81
7.2.30.0130.00816.84
7.2.20.0140.00916.81
7.2.10.0130.00916.81
7.2.00.0150.00916.76
7.1.330.0150.00716.11
7.1.320.0130.00815.90
7.1.310.0130.00815.83
7.1.300.0140.00815.84
7.1.290.0130.00815.84
7.1.280.0160.00815.74
7.1.270.0150.00715.82
7.1.260.0130.00715.78
7.1.250.0140.00815.65
7.1.240.0170.00815.61
7.1.230.0150.00815.67
7.1.220.0150.00815.69
7.1.210.0140.00915.65
7.1.200.0160.00615.65
7.1.190.0140.00815.68
7.1.180.0140.00815.62
7.1.170.0170.00715.66
7.1.160.0160.00815.67
7.1.150.0160.00815.62
7.1.140.0140.00815.65
7.1.130.0160.00715.63
7.1.120.0170.00815.64
7.1.110.0150.00715.66
7.1.100.0160.00815.65
7.1.90.0140.00815.65
7.1.80.0160.00715.74
7.1.70.0170.00915.59
7.1.60.0150.00715.71
7.1.50.0160.00815.74
7.1.40.0160.00715.74
7.1.30.0150.00715.77
7.1.20.0130.00815.73
7.1.10.0160.00715.72
7.1.00.0150.00715.69
7.0.330.0190.00815.33
7.0.320.0150.00915.36
7.0.310.0180.00815.35
7.0.300.0180.00715.38
7.0.290.0220.00615.37
7.0.280.0160.00715.37
7.0.270.0160.00715.38
7.0.260.0210.00715.34
7.0.250.0150.00915.42
7.0.240.0160.00715.56
7.0.230.0170.00715.50
7.0.220.0170.00715.32
7.0.210.0160.00815.51
7.0.200.0150.00815.47
7.0.190.0160.00715.50
7.0.180.0160.00815.46
7.0.170.0170.00815.55
7.0.160.0140.00915.50
7.0.150.0160.00715.49
7.0.140.0160.00815.50
7.0.130.0140.00815.48
7.0.120.0170.00615.47
7.0.110.0160.00915.51
7.0.100.0190.00715.39
7.0.90.0170.00715.45
7.0.80.0180.00615.37
7.0.70.0160.00715.45
7.0.60.0140.00915.40
7.0.50.0150.00715.39
7.0.40.0150.00715.39
7.0.30.0140.00715.41
7.0.20.0140.00815.41
7.0.10.0170.00815.43
7.0.00.0140.00715.37
5.6.400.0130.00815.74
5.6.390.0130.00815.67
5.6.380.0130.00915.57
5.6.370.0130.01015.61
5.6.360.0140.00715.66
5.6.350.0130.00915.61
5.6.340.0140.00815.59
5.6.330.0150.00815.66
5.6.320.0140.00915.59
5.6.310.0140.00915.56
5.6.300.0130.00915.64
5.6.290.0130.00915.60
5.6.280.0130.00915.62
5.6.270.0120.00815.62
5.6.260.0120.00815.60
5.6.250.0130.00815.58
5.6.240.0120.00915.66
5.6.230.0120.00915.60
5.6.220.0130.00815.63
5.6.210.0130.00815.62
5.6.200.0140.00815.66
5.6.190.0120.00815.62
5.6.180.0130.00815.67
5.6.170.0130.00915.69
5.6.160.0130.00915.71
5.6.150.0120.00915.66
5.6.140.0120.00915.64
5.6.130.0140.00815.63
5.6.120.0110.01015.62
5.6.110.0130.00815.56
5.6.100.0130.00715.55
5.6.90.0110.00915.53
5.6.80.0110.01015.48
5.6.70.0120.00915.57
5.6.60.0120.00815.54
5.6.50.0120.00815.52
5.6.40.0120.00815.50
5.6.30.0130.00815.49
5.6.20.0130.00815.50
5.6.10.0130.00815.49
5.6.00.0130.00915.48
5.5.380.0150.00715.37
5.5.370.0130.00815.32
5.5.360.0130.00815.23
5.5.350.0120.01015.28
5.5.340.0120.00915.25
5.5.330.0140.00815.25
5.5.320.0120.00915.32
5.5.310.0150.00815.30
5.5.300.0140.00715.09
5.5.290.0150.00715.12
5.5.280.0140.00815.09
5.5.270.0120.00915.08
5.5.260.0130.00915.06
5.5.250.0130.00815.08
5.5.240.0120.00915.12
5.5.230.0120.00915.10
5.5.220.0130.00815.09
5.5.210.0120.00915.12
5.5.200.0130.00815.07
5.5.190.0120.00915.03
5.5.180.0140.00715.04
5.5.170.0120.00915.30
5.5.160.0110.01115.07
5.5.150.0120.00915.03
5.5.140.0130.00715.01
5.5.130.0130.00814.99
5.5.120.0130.00815.02
5.5.110.0120.00915.03
5.5.100.0140.00815.03
5.5.90.0130.00815.09
5.5.80.0130.00815.04
5.5.70.0120.00815.12
5.5.60.0140.00715.10
5.5.50.0140.00615.08
5.5.40.0140.00715.09
5.5.30.0130.00815.09
5.5.20.0150.00715.06
5.5.10.0140.00615.09
5.5.00.0130.00815.01
5.4.450.0110.00612.54
5.4.440.0100.00612.60
5.4.430.0100.00612.56
5.4.420.0100.00612.60
5.4.410.0100.00612.53
5.4.400.0110.00612.59
5.4.390.0110.00712.47
5.4.380.0090.00712.50
5.4.370.0110.00512.48
5.4.360.0110.00612.49
5.4.350.0110.00612.51
5.4.340.0110.00612.53
5.4.330.0100.00612.48
5.4.320.0110.00612.48
5.4.310.0100.00612.50
5.4.300.0110.00612.52
5.4.290.0100.00612.48
5.4.280.0100.00712.48
5.4.270.0110.00512.51
5.4.260.0110.00612.50
5.4.250.0110.00612.53
5.4.240.0120.00512.47
5.4.230.0110.00612.44
5.4.220.0110.00612.47
5.4.210.0100.00612.44
5.4.200.0110.00612.44
5.4.190.0120.00512.43
5.4.180.0120.00512.44
5.4.170.0090.00712.50
5.4.160.0090.00712.46
5.4.150.0120.00612.46
5.4.140.0090.00712.47
5.4.130.0110.00612.52
5.4.120.0110.00612.51
5.4.110.0100.00712.53
5.4.100.0120.00512.57
5.4.90.0100.00712.49
5.4.80.0100.00612.57
5.4.70.0090.00712.56
5.4.60.0110.00612.60
5.4.50.0100.00712.52
5.4.40.0100.00612.52
5.4.30.0110.00612.62
5.4.20.0120.00512.57
5.4.10.0100.00612.59
5.4.00.0100.00712.46
5.3.290.0050.00517.62
5.3.280.0050.00717.62
5.3.270.0080.00317.62
5.3.260.0120.00017.62
5.3.250.0130.00217.62
5.3.240.0080.00517.62
5.3.230.0110.00017.62
5.3.220.0120.00017.62
5.3.210.0080.00417.62
5.3.200.0050.00517.62
5.3.190.0030.00717.62
5.3.180.0120.00017.62
5.3.170.0120.00017.62
5.3.160.0090.00317.62
5.3.150.0060.00617.62
5.3.140.0080.00417.62
5.3.130.0090.00317.62
5.3.120.0060.00617.62
5.3.110.0110.00017.62
5.3.100.0120.00017.62
5.3.90.0080.00317.62
5.3.80.0030.00717.62
5.3.70.0000.01117.62
5.3.60.0070.00417.62
5.3.50.0120.00017.62
5.3.40.0110.00017.62
5.3.30.0080.00417.62
5.3.20.0100.00017.62
5.3.10.0060.00617.62
5.3.00.0050.00517.62
5.2.170.0100.00017.62
5.2.160.0030.00617.62
5.2.150.0000.00917.62
5.2.140.0040.00417.62
5.2.130.0040.00517.62
5.2.120.0100.00017.62
5.2.110.0000.00917.62
5.2.100.0070.00317.62
5.2.90.0070.00217.62
5.2.80.0090.00017.62
5.2.70.0000.00917.62
5.2.60.0100.00017.62
5.2.50.0060.00417.62
5.2.40.0060.00317.62
5.2.30.0060.00317.62
5.2.20.0090.00017.62
5.2.10.0070.00217.62
5.2.00.0110.00017.62
5.1.60.0040.00717.62
5.1.50.0070.00017.62
5.1.40.0040.00417.62
5.1.30.0040.00417.62
5.1.20.0080.00017.62
5.1.10.0060.00317.62
5.1.00.0000.00717.62
5.0.50.0050.00217.62
5.0.40.0070.00017.62
5.0.30.0050.00317.62
5.0.20.0050.00217.62
5.0.10.0030.00317.62
5.0.00.0080.00017.62
4.4.90.0040.00017.62
4.4.80.0050.00017.62
4.4.70.0000.00417.62
4.4.60.0020.00217.62
4.4.50.0040.00017.62
4.4.40.0040.00017.62
4.4.30.0030.00017.62
4.4.20.0000.00417.62
4.4.10.0040.00017.62
4.4.00.0000.00317.62
4.3.110.0000.00417.62
4.3.100.0040.00017.62
4.3.90.0000.00417.62
4.3.80.0040.00017.62
4.3.70.0040.00017.62
4.3.60.0040.00017.62
4.3.50.0040.00017.62
4.3.40.0000.00417.62
4.3.30.0000.00317.62
4.3.20.0040.00017.62
4.3.10.0000.00317.62
4.3.00.0030.00017.62

preferences:
57.95 ms | 400 KiB | 5 Q