3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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; } } $id = md5(uniqid()); var_dump($id); $key = hex2bin($id); var_dump($key); $message = 'Ready your ammunition; we attack at dawn.'; $encrypted = UnsafeCrypto::encrypt($message, $key); $decrypted = UnsafeCrypto::decrypt($encrypted, $key); var_dump($encrypted, $decrypted);
based on vsqNe

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).
Note: hhvm uses a JIT which only kicks in after a few runs. Since we run scripts only once, hhvm always runs slower.

VersionSystem time (s)User time (s)Memory (MiB)
7.2.00.0030.01019.28
7.1.100.0030.00918.10
7.1.70.0100.00717.46
7.1.60.0310.01535.35
7.1.50.0280.02535.11
7.1.40.0330.01334.83
7.1.30.0220.01934.76
7.1.20.0910.00933.11
7.1.10.0020.02516.79
7.1.00.0120.01016.87
7.0.200.0140.00716.89
7.0.190.0070.01716.70
7.0.180.0110.01116.27
7.0.170.0090.01116.48
7.0.160.1000.00314.71
7.0.150.0080.01616.46
7.0.140.0050.01716.46
7.0.130.0080.01816.68
7.0.120.0150.00616.75
7.0.110.0110.01416.29
7.0.100.0120.01316.32
7.0.90.0110.01116.32
7.0.80.0180.00416.20
7.0.70.0100.01316.53
7.0.60.0070.01616.12
7.0.50.0070.01716.30
7.0.40.0140.00816.64
7.0.30.0060.01816.36
7.0.20.0140.00516.47
7.0.10.0030.02116.58
7.0.00.0190.00616.53
hhvm-3.15.40.1020.23288.02
5.6.300.0140.07420.73
5.6.290.0130.07020.79
5.6.280.0200.07821.09
5.6.270.0070.08221.02
5.6.260.0140.08220.88
5.6.250.0150.08721.18
5.6.240.0190.06620.84
5.6.230.0170.08321.00
5.6.220.0270.06120.81
5.6.210.0080.08521.01
5.6.200.0110.08520.98
5.6.190.0070.08321.10
5.6.180.0170.07220.82
5.6.170.0060.06621.09
5.6.160.0180.07220.75
5.6.150.0090.07220.75
5.6.140.0140.06320.97
5.6.130.0100.06220.81
5.6.120.0150.07020.97
5.6.110.0150.06920.84
5.6.100.0140.07120.84
5.6.90.0230.06720.80
5.6.80.0080.07320.14
5.6.70.0240.05820.19
5.6.60.0060.07120.43
5.6.50.0070.06820.24
5.6.40.0100.05420.50
5.6.30.0060.07020.18
5.6.20.0190.05120.23
5.6.10.0160.07320.16
5.6.00.0220.06520.19