3v4l.org

run code in 300+ PHP versions simultaneously
<?php $password = 'My Plaintext Password'; $plaintext = 'This is my message'; $secretBox = new Secretbox(); $ciphertext = $secretBox->encrypt($plaintext, $password); var_dump($ciphertext); $decryptedPlaintext = $secretBox->decrypt($ciphertext, $password); var_dump($decryptedPlaintext); assertThat($decryptedPlaintext === $plaintext, 'Can decrypt'); assertThat( $secretBox->encrypt($plaintext, $password) !== $secretBox->encrypt($plaintext, $password), 'Should not result in same ciphertext' ); try { $failed = false; // modify ciphertext $ciphertext[65] = 'f'; $secretBox->decrypt($ciphertext, $password); } catch (\Exception $e) { $failed = true; } finally { assertThat($failed, 'Tempered chiphertext should result in exception'); } class Secretbox { public function encrypt(string $plaintext, string $password): string { // create a random salt for key derivation $salt = random_bytes(SODIUM_CRYPTO_PWHASH_SALTBYTES); $key = $this->deriveKeyFromUserPassword($password, $salt); $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $ciphertext = sodium_crypto_secretbox($plaintext, $nonce, $key); sodium_memzero($password); sodium_memzero($key); return $salt.$nonce.$ciphertext; } public function decrypt(string $ciphertext, string $password): string { $salt = substr($ciphertext, 0, SODIUM_CRYPTO_PWHASH_SALTBYTES); $nonce = substr($ciphertext, SODIUM_CRYPTO_PWHASH_SALTBYTES, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $ciphertext = substr($ciphertext, SODIUM_CRYPTO_PWHASH_SALTBYTES + SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $key = $this->deriveKeyFromUserPassword($password, $salt); $plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $key); sodium_memzero($password); sodium_memzero($key); sodium_memzero($nonce); if ($plaintext === false) { throw new \InvalidArgumentException('Bad ciphertext'); } return $plaintext; } private function deriveKeyFromUserPassword(string $password, string $salt): string { $key = sodium_crypto_pwhash( SODIUM_CRYPTO_SECRETBOX_KEYBYTES, $password, $salt, SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE, SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE ); sodium_memzero($password); return $key; } } function assertThat(bool $expressionResult, string $message) { if (!$expressionResult) { throw new \RuntimeException($message); } }

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)
7.4.00.0830.55346.82
7.3.120.1060.65047.17
7.3.110.0900.69147.01
7.3.100.0760.53746.92
7.3.90.0760.55047.10
7.3.80.0730.57746.86
7.3.70.0730.55347.00
7.3.60.0660.60746.64
7.3.50.0830.51846.79
7.3.40.0400.50446.98
7.3.30.0660.54146.94
7.3.20.1000.50648.62
7.3.10.0660.53148.59
7.3.00.0800.45148.69
7.2.250.0760.64147.12
7.2.240.0930.69347.11
7.2.230.0630.53747.05
7.2.220.0660.52147.16
7.2.210.0600.51047.09
7.2.200.0930.61947.26
7.2.190.0730.51347.00
7.2.180.0600.49747.09
7.2.170.0700.57547.10
7.2.00.1340.80284.09
7.1.330.0000.01515.82
7.1.320.0000.01115.80
7.1.310.0030.01015.70
7.1.300.0030.00815.79
7.1.290.0080.00315.44
7.1.280.0050.00715.89
7.1.270.0030.00615.68
7.1.260.0080.00415.81
7.1.120.0060.00818.69
7.1.110.0060.00718.24
7.1.100.0090.00617.88
7.1.90.0060.00618.11
7.1.80.0060.00918.34
7.1.70.0060.00917.02
7.1.60.0130.01335.18
7.1.50.0100.01634.94
7.1.40.0130.01334.39
7.1.30.0130.01334.42
7.1.20.0170.00734.63
7.1.10.0060.00616.45
7.1.00.0060.00916.67

preferences:
36.74 ms | 403 KiB | 5 Q