3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CodeBook { /** PKCS#5でパディング */ const PAD_PKCS5 = 'PAD_PKCS5'; /** Null文字でパディング */ const PAD_NULL = "\0"; /** スペースでパディング */ const PAD_SPACE = ' '; private $_cipher; private $_mode; private $_padding; /** * コンストラクタ * デフォルトは、AES(ブロック長128bit)、CBCモード、PKCS#5でパディング * @param string $cipher (省略可)暗号アルゴリズム * @param string $mode (省略可)暗号モード * @param string $padding (省略可)パディング方法(このクラスの定数 or 文字) * @see http://www.php.net/manual/ja/mcrypt.ciphers.php * @see http://www.php.net/manual/ja/mcrypt.constants.php */ public function __construct($cipher = MCRYPT_RIJNDAEL_128, $mode = MCRYPT_MODE_CBC, $padding = self::PAD_PKCS5) { $this->_cipher = $cipher; $this->_mode = $mode; $this->_padding = $padding; } /** * 暗号化する * 初期化ベクトル(IV)を渡さない場合、ランダムな初期化ベクトルを生成する * @param string $key 暗号鍵 * @param string $encryptee 暗号化するデータ * @param string $iv (省略可)初期化ベクトル(ECBでは不要) * @return array hex化した暗号化済みデータと、hex化した初期化ベクトル(IV) */ public function encrypt($key, $encryptee, $iv = null) { $this->_checkKeySize($key); if (!$iv) { $iv = $this->_getRandIv(); } if ($this->_padding === self::PAD_PKCS5) { $encryptee = $this->padPkcs5($encryptee); } else { $encryptee = $this->pad($encryptee, $this->_padding); } $bin = mcrypt_encrypt($this->_cipher, $key, $encryptee, $this->_mode, $iv); return array(bin2hex($bin), bin2hex($iv)); } /** * 復号する * @param string $key 暗号鍵 * @param string $encrypted 暗号化されてhex化されたデータ * @param string $iv (省略可)hex化した初期化ベクトル(ECBでは不要) * @return string 復号したデータ */ public function decrypt($key, $encrypted, $iv = null) { $this->_checkKeySize($key); $bin = $this->hex2bin($encrypted); if ($iv) { $iv = $this->hex2bin($iv); } else { $iv = $this->_getRandIv(); //Warningを出さないためのダミーのIV } $decrypted = mcrypt_decrypt($this->_cipher, $key, $bin, $this->_mode, $iv); if ($this->_padding === self::PAD_PKCS5) { $decrypted = $this->trimPkcs5($decrypted); } else { $decrypted = rtrim($decrypted, $this->_padding); } return $decrypted; } /** * ブロック長に合わせてパディングする * @param string $data パディング対象のデータ * @param string $padChar パディング文字 * @return string パディングしたデータ */ public function pad($data, $padChar) { $size = $this->_getBlockSize(); return str_pad($data, ceil(strlen($data) / $size) * $size, $padChar); } /** * PKCS#5でパディングする * @param string $data パディング対象のデータ * @return string パディングしたデータ */ public function padPkcs5($data) { $size = $this->_getBlockSize(); $padLen = $size - (strlen($data) % $size); return $data . str_repeat(chr($padLen), $padLen); } /** * PKCS#5のパディングを除去する * @param string $data PKCS#5でパディングされたデータ * @return string パディングしたデータ */ public function trimPkcs5($data) { return substr($data, 0, ord(substr($data, -1, 1)) * -1); } /** * hex化したデータをバイナリに変換する(bin2hex()の反対) * @param string $hex hex化されたデータ * @return string バイナリになったデータ */ public function hex2bin($hex) { return pack('H*', $hex); } /** * 暗号鍵の長さをチェックする * @param string $key 暗号鍵 * @throws Exception 長さが不正な場合に例外を投げる */ private function _checkKeySize($key) { $sizes = mcrypt_module_get_supported_key_sizes($this->_cipher); //可変の場合は空なのでチェックしない if ($sizes && !in_array(strlen($key), $sizes)) { throw new Exception("Invalid key length ($key)"); } } /** * ランダムな初期化ベクトル(IV)を生成する * @return string 生成した初期化ベクトル */ private function _getRandIv() { srand(); return mcrypt_create_iv($this->_getBlockSize(), MCRYPT_RAND); } /** * 暗号アルゴリズムと暗号モードに応じたブロックサイズを取得する * @return integer ブロックサイズ */ private function _getBlockSize() { return mcrypt_get_iv_size($this->_cipher, $this->_mode); } } $key = '秘密の鍵128b'; $text = '秘密のメッセージ'; /* AES(RIJNDAEL128)、CBC、PKCS#5でPAD */ $codeBook = new CodeBook(); list($encrypted, $iv) = $codeBook->encrypt($key, $text); //暗号化 $decrypted = $codeBook->decrypt($key, $encrypted, $iv); //復号 // => "秘密のメッセージ => (略) => 秘密のメッセージ" echo $text . ' => ' . $encrypted . ' => ' . $decrypted; echo '<br />'; echo var_dump($text === $decrypted); // => bool(true) echo '<hr />'; /* RIJNDAEL256、ECB、Null文字でPAD */ $codeBook = new CodeBook(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB, CodeBook::PAD_NULL); list($encrypted) = $codeBook->encrypt($key, $text); //暗号化(ECBはIV不要) $decrypted = $codeBook->decrypt($key, $encrypted); //復号 // => "秘密のメッセージ => (略) => 秘密のメッセージ" echo $text . ' => ' . $encrypted . ' => ' . $decrypted; echo '<br />'; echo var_dump($text === $decrypted); // => bool(true) echo '<hr />'; /* Blowfish、CBC、スペースでPAD */ $codeBook = new CodeBook(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC, CodeBook::PAD_SPACE); list($encrypted, $iv) = $codeBook->encrypt($key, $text); //暗号化 $decrypted = $codeBook->decrypt($key, $encrypted, $iv); //復号 // => "秘密のメッセージ => (略) => 秘密のメッセージ" echo $text . ' => ' . $encrypted . ' => ' . $decrypted; echo '<br />'; echo var_dump($text === $decrypted); // => bool(true)

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.40.0060.00918.84
8.3.30.0060.00820.37
8.3.20.0040.00420.21
8.3.10.0030.00622.15
8.3.00.0040.00422.58
8.2.170.0090.00622.96
8.2.160.0100.00322.07
8.2.150.0080.00824.18
8.2.140.0060.00324.66
8.2.130.0140.00326.16
8.2.120.0030.00620.83
8.2.110.0060.00620.43
8.2.100.0070.00417.91
8.2.90.0040.00417.82
8.2.80.0000.00819.24
8.2.70.0090.00317.72
8.2.60.0060.00317.93
8.2.50.0050.00318.07
8.2.40.0000.00818.22
8.2.30.0000.00719.43
8.2.20.0080.00017.65
8.2.10.0050.00317.87
8.2.00.0000.00817.81
8.1.270.0080.00023.99
8.1.260.0080.00026.35
8.1.250.0040.00428.09
8.1.240.0060.00322.31
8.1.230.0030.01021.10
8.1.220.0080.00017.77
8.1.210.0090.00019.03
8.1.200.0070.00317.35
8.1.190.0030.00517.43
8.1.180.0060.00318.10
8.1.170.0000.00818.50
8.1.160.0040.00418.88
8.1.150.0040.00718.52
8.1.140.0000.00817.52
8.1.130.0000.00717.89
8.1.120.0050.00217.53
8.1.110.0060.00317.34
8.1.100.0040.00417.46
8.1.90.0040.00417.45
8.1.80.0040.00417.53
8.1.70.0040.00417.41
8.1.60.0040.00417.64
8.1.50.0040.00417.60
8.1.40.0000.00817.55
8.1.30.0060.00317.64
8.1.20.0080.00017.71
8.1.10.0000.00817.52
8.1.00.0040.00717.48
8.0.300.0040.00420.16
8.0.290.0040.00417.18
8.0.280.0000.00718.32
8.0.270.0000.00817.18
8.0.260.0030.00316.75
8.0.250.0070.00017.02
8.0.240.0000.00817.03
8.0.230.0030.00317.08
8.0.220.0000.00717.00
8.0.210.0050.00316.84
8.0.200.0050.00517.08
8.0.190.0000.00916.95
8.0.180.0000.00716.93
8.0.170.0080.00016.86
8.0.160.0040.00416.90
8.0.150.0060.00316.90
8.0.140.0050.00216.80
8.0.130.0030.00313.42
8.0.120.0030.00517.00
8.0.110.0030.00516.87
8.0.100.0080.00016.84
8.0.90.0040.00416.90
8.0.80.0110.00417.00
8.0.70.0050.00216.84
8.0.60.0040.00417.08
8.0.50.0020.00516.83
8.0.30.0120.00817.21
8.0.20.0090.01017.40
8.0.10.0030.00517.14
8.0.00.0160.00416.74
7.4.330.0060.00015.00
7.4.320.0030.00316.60
7.4.300.0030.00316.55
7.4.290.0000.00716.58
7.4.280.0000.00716.63
7.4.270.0030.00316.65
7.4.260.0050.00213.32
7.4.250.0040.00416.52
7.4.240.0060.00116.52
7.4.230.0000.00716.66
7.4.220.0040.01516.57
7.4.210.0070.00916.64
7.4.200.0040.00416.64
7.4.190.0070.00016.79
7.4.160.0030.01316.66
7.4.150.0120.00617.40
7.4.140.0080.01117.86
7.4.130.0080.00916.59
7.4.120.0080.01116.51
7.4.110.0090.00916.44
7.4.100.0180.00016.43
7.4.90.0120.00616.64
7.4.80.0150.00619.39
7.4.70.0100.00716.37
7.4.60.0100.01316.39
7.4.50.0060.00316.57
7.4.40.0060.00822.77
7.4.30.0090.01316.50
7.4.00.0040.01115.02
7.3.330.0060.00013.44
7.3.320.0000.00613.35
7.3.310.0070.00016.47
7.3.300.0040.00416.43
7.3.290.0090.00616.39
7.3.280.0050.01216.41
7.3.270.0060.01217.40
7.3.260.0160.00316.68
7.3.250.0110.01016.35
7.3.240.0100.01116.33
7.3.230.0110.00616.61
7.3.210.0130.00316.47
7.3.200.0110.01119.39
7.3.190.0140.00316.37
7.3.180.0050.01816.67
7.3.170.0030.01416.60
7.3.160.0180.00016.56
7.3.120.0030.01414.87
7.3.10.0080.00416.25
7.3.00.0070.00416.51
7.2.330.0130.00516.82
7.2.320.0070.01016.64
7.2.310.0120.00916.47
7.2.300.0020.01716.79
7.2.290.0130.00316.82
7.2.130.0000.01416.99
7.2.120.0070.00316.58
7.2.110.0040.01117.01
7.2.100.0030.00616.82
7.2.90.0090.00616.94
7.2.80.0030.01416.73
7.2.70.0030.01416.62
7.2.60.0030.00717.05
7.2.50.0110.00416.80
7.2.40.0100.00316.75
7.2.30.0000.00917.10
7.2.20.0060.00616.89
7.2.10.0000.01016.68
7.2.00.0070.00617.99
7.1.250.0030.00715.53
7.1.200.0040.00815.59
7.1.100.0070.00718.00
7.1.70.0000.01217.07
7.1.60.0100.00719.32
7.1.50.0100.01316.95
7.1.00.0030.04322.47
7.0.200.0040.00416.45
7.0.140.0100.06722.18
7.0.60.0000.03321.71
7.0.50.0000.04322.12
7.0.40.0000.04020.16
7.0.30.0070.08320.09
7.0.20.0100.07320.14
7.0.10.0030.07720.10
7.0.00.0070.07720.11
5.6.280.0070.07021.06
5.6.210.0030.03320.55
5.6.200.0030.03321.08
5.6.190.0000.03321.15
5.6.180.0030.08321.15
5.6.170.0030.08321.15
5.6.160.0100.07321.17
5.6.150.0070.07721.13
5.6.140.0000.09021.28
5.6.130.0130.07321.14
5.6.120.0030.07721.02
5.6.110.0100.08321.20
5.6.100.0070.08321.21
5.6.90.0070.08321.13
5.6.80.0070.07720.52
5.6.70.0000.08020.53
5.6.60.0070.08020.49
5.6.50.0030.05720.58
5.6.40.0070.08020.48
5.6.30.0100.06020.64
5.6.20.0130.06020.57
5.6.10.0070.07320.49
5.6.00.0000.07720.51
5.5.350.0030.03320.57
5.5.340.0030.04320.97
5.5.330.0000.03321.03
5.5.320.0070.07720.98
5.5.310.0030.08320.98
5.5.300.0070.08320.94
5.5.290.0070.08320.89
5.5.280.0000.08321.03
5.5.270.0070.06720.94
5.5.260.0070.08020.91
5.5.250.0070.07320.81
5.5.240.0070.07720.38
5.5.230.0130.07320.50
5.5.220.0070.06720.37
5.5.210.0070.07720.32
5.5.200.0070.07320.27
5.5.190.0000.09020.46
5.5.180.0030.08020.48
5.5.160.0100.07320.18
5.5.150.0030.08020.38
5.5.140.0070.07320.31
5.5.130.0030.07720.28
5.5.120.0070.07720.31
5.5.110.0100.07720.36
5.5.100.0070.07020.27
5.5.90.0130.05720.19
5.5.80.0030.07720.16
5.5.70.0100.07020.25
5.5.60.0070.07020.24
5.5.50.0000.07320.24
5.5.40.0070.07320.34
5.5.30.0030.08320.16
5.5.20.0030.08020.34
5.5.10.0070.07320.16
5.5.00.0030.08020.18
5.4.450.0070.08019.42
5.4.440.0070.08019.31
5.4.430.0100.07319.38
5.4.420.0000.07719.41
5.4.410.0030.07019.45
5.4.400.0030.08019.01
5.4.390.0030.08019.25
5.4.380.0100.07019.09
5.4.370.0070.05019.05
5.4.360.0000.07319.09
5.4.350.0100.06719.14
5.4.340.0170.06018.99
5.4.320.0130.06719.11
5.4.310.0070.07318.99
5.4.300.0030.07319.03
5.4.290.0100.07019.17
5.4.280.0070.07719.13
5.4.270.0070.07719.15
5.4.260.0070.06719.10
5.4.250.0000.07719.19
5.4.240.0130.06019.21
5.4.230.0000.07719.12
5.4.220.0130.07019.21
5.4.210.0100.07718.98
5.4.200.0000.07319.09
5.4.190.0030.07319.00
5.4.180.0070.07319.24
5.4.170.0070.05019.23
5.4.160.0100.06719.08
5.4.150.0000.08019.07
5.4.140.0070.07016.54
5.4.130.0000.05316.44
5.4.120.0000.08016.52
5.4.110.0070.06716.51
5.4.100.0100.06716.43
5.4.90.0070.07016.55
5.4.80.0000.05716.40
5.4.70.0000.07716.43
5.4.60.0100.07016.49
5.4.50.0100.06716.58
5.4.40.0030.07016.45
5.4.30.0000.05716.46
5.4.20.0070.06316.46
5.4.10.0000.07716.63
5.4.00.0000.06716.00
5.3.290.0070.07714.76
5.3.280.0000.08014.55
5.3.270.0100.07014.57
5.3.260.0000.07714.66
5.3.250.0070.05714.70
5.3.240.0030.07314.69
5.3.230.0070.07714.70
5.3.220.0030.06014.67
5.3.210.0000.09714.70
5.3.200.0070.07314.62
5.3.190.0070.07314.68
5.3.180.0100.05714.69
5.3.170.0100.07314.52
5.3.160.0100.07014.66
5.3.150.0000.06714.52
5.3.140.0030.07714.66
5.3.130.0070.07714.64
5.3.120.0030.07314.64
5.3.110.0030.08014.64
5.3.100.0070.06714.13
5.3.90.0030.07314.04
5.3.80.0070.06314.11
5.3.70.0000.06314.00
5.3.60.0000.07714.09
5.3.50.0030.06314.05
5.3.40.0030.06313.98
5.3.30.0000.07313.87
5.3.20.0000.06713.81
5.3.10.0070.04713.70
5.3.00.0000.07713.70
5.2.170.0030.06311.11
5.2.160.0030.04711.25
5.2.150.0000.06011.26
5.2.140.0000.06011.26
5.2.130.0000.06311.21
5.2.120.0030.05311.21
5.2.110.0030.06011.23
5.2.100.0070.05311.26
5.2.90.0030.05311.06
5.2.80.0000.06711.05
5.2.70.0070.03311.05
5.2.60.0000.03310.96
5.2.50.0070.05710.93
5.2.40.0030.06311.06
5.2.30.0030.05711.12
5.2.20.0030.06011.13
5.2.10.0030.06710.88
5.2.00.0030.04710.83
5.1.60.0000.05710.50
5.1.50.0030.04710.50
5.1.40.0030.05310.50
5.1.30.0030.05010.50
5.1.20.0030.05310.50
5.1.10.0170.04310.50
5.1.00.0030.04310.50
5.0.50.0030.04310.50
5.0.40.0030.04710.50
5.0.30.0000.05310.50
5.0.20.0000.03710.50
5.0.10.0000.04310.50
5.0.00.0000.06310.50
4.4.90.0030.03310.50
4.4.80.0030.03310.50
4.4.70.0000.03710.50
4.4.60.0070.02710.50
4.4.50.0070.03010.50
4.4.40.0000.05710.50
4.4.30.0000.02310.50
4.4.20.0030.03310.50
4.4.10.0100.02710.50
4.4.00.0000.05310.50
4.3.110.0030.02710.50
4.3.100.0000.03310.50
4.3.90.0070.02710.50
4.3.80.0070.05010.50
4.3.70.0030.02310.50
4.3.60.0030.03310.50
4.3.50.0030.03010.50
4.3.40.0000.04710.50
4.3.30.0030.03310.50
4.3.20.0000.03310.50
4.3.10.0070.03010.50
4.3.00.0000.03310.50

preferences:
44.4 ms | 400 KiB | 5 Q