3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); namespace ParagonIE\BlogExampleCode; /** * Implements encryption RSA with PKCS1v1.5 padding * using the Anti-BB'98 dance. * * @ref https://paragonie.com/b/_8qH-xPuGPUoSSsc */ class RSAPKCS1v15 { public static function encrypt(string $message, string $publicKey): array { $key = \random_bytes(32); $C = ''; // This defaults to PKCS1v1.5: \openssl_public_encrypt($key, $C, $publicKey); $iv = \random_bytes(16); $cipher = \openssl_encrypt( $message, 'aes-256-ctr', $key, OPENSSL_RAW_DATA, $iv ); $mac = \hash_hmac('sha256', $iv . $cipher, $key, true); $D = $mac . $iv . $cipher; return [\bin2hex($C), \bin2hex($D)]; } public static function decrypt(string $CHex, string $DHex, string $privateKey): string { $kPrime = \random_bytes(32); $C = \hex2bin($CHex); $D = \hex2bin($DHex); $k = ''; $success = \openssl_private_decrypt($C, $k, $privateKey); // Do a constant-time swap: $key = self::cswap32($k, (string) $kPrime, $success); $mac = \mb_substr($D, 0, 32, '8bit'); $iv = \mb_substr($D, 32, 16, '8bit'); $cipher = \mb_substr($D, 48, null, '8bit'); $recalc = \hash_hmac('sha256', $iv . $cipher, $key, true); if (!\hash_equals($recalc, $mac)) { throw new \Exception('Invalid MAC'); } return \openssl_decrypt( $cipher, 'aes-256-ctr', $key, OPENSSL_RAW_DATA, $iv ); } /** * Branch-less, timing safe version of * $A = ($switch === 0 ? $A : $B); * * @param string $A * @param string $B * @param bool $success * @return string */ public static function cswap32(string $A, string $B, bool $success): string { $A .= \str_repeat("\x00", 32); $B .= \str_repeat("\x00", 32); /** @var int $mask 0 if $success is true, otherwise 255 */ $mask = (((int) $success) - 1) & 0xff; $C = ''; for ($i = 0; $i < 32; ++$i) { $C .= self::intToChr( (self::chrToInt($A[$i]) ^ self::chrToInt($B[$i])) & $mask ); } return (string) mb_substr($A ^ $C, 0, 32, '8bit'); } public static function chrToInt(string $chr): int { $chunk = \unpack('C', $chr); return (int) ($chunk[1]); } public static function intToChr(int $int): string { return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J1q95v7NdygFIEIW+qbIKbc6Wz0MCgYBsUZdQD+qx/xAhELX364I2 epTryHMUrs+tGygQVrqdiJX5dcDgM1TUJkdQV6jLsKjPs4Vt6OgZRMrnuLMsk02R 3M8gGQ25ok4f4nyyEZxGGWnVujn55KzUiYWhGWmhgp18UCkoYa59/Q9ss+gocV9h B9j9Q43vD80QUjiF4z0DQQKBgC7XQX1VibkMim93QAnXGDcAS0ij+w02qKVBjcHk b9mMBhz8GAxGOIu7ZJafYmxhwMyVGB0I1FQeEczYCJUKnBYN6Clsjg6bnBT/z5bJ x/Jx1qCzX3Uh6vLjpjc5sf4L39Tyye1u2NXQmZPwB5x9BdcsFConSq/s4K1LJtUT 3KFxAoGBANGcQ8nObi3m4wROyKrkCWcWxFFMnpwxv0pW727Hn9wuaOs4UbesCnwm pcMTfzGUDuzYXCtAq2pJl64HG6wsdkWmjBTJEpm6b9ibOBN3qFV2zQ0HyyKlMWxI uVSj9gOo61hF7UH9XB6R4HRdlpBOuIbgAWZ46dkj9/HM9ovdP0Iy -----END RSA PRIVATE KEY-----'; $public_key = '-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyaTgTt53ph3p5GHgwoGW wz5hRfWXSQA08NCOwe0FEgALWos9GCjNFCd723nCHxBtN1qd74MSh/uN88JPIbwx KheDp4kxo4YMN5trPaF0e9G6Bj1N02HnanxFLW+gmLbgYO/SZYfWF/M8yLBcu5Y1 Ot0ZxDDDXS9wIQTtBE0ne3YbxgZJAZTU5XqyQ1DxdzYyC5lF6yBaR5UQtCYTnXAA pVRuUI2Sd6L1E2vl9bSBumZ5IpNxkRnAwIMjeTJB/0AIELh0mE5vwdihOCbdV6al UyhKC1+1w/FW6HWcp/JG1kKC8DPIidZ78Bbqv9YFzkAbNni5eSBOsXVBKG78Zsc8 owIDAQAB -----END PUBLIC KEY-----'; $in = 'This is a test message!'; list ($C, $D) = RSAPKCS1v15::encrypt($in, $public_key); $start = $end = microtime(true); $out = RSAPKCS1v15::decrypt($C, $D, $private_key); $end = microtime(true); var_dump($in, $out); var_dump($end - $start); // Flip the bits in one character of C to force an error $Corig = $D; $C = hex2bin($C); $C[0] = RSAPKCS1v15::intToChr( RSAPKCS1v15::chrToInt($C[0]) ^ 0xff ); $C = bin2hex($C); $start = $end = microtime(true); try { $out = RSAPKCS1v15::decrypt($C, $D, $private_key); } catch (\Exception $ex) { $end = microtime(true); var_dump($end - $start); if ($ex->getMessage() === 'Invalid MAC') { echo 'Got the expected error message. No RSA hints given.', PHP_EOL; } } $C = $Corig; // Flip the bits in one character of D to force an error $Dorig = $D; $D = hex2bin($D); $D[0] = RSAPKCS1v15::intToChr( RSAPKCS1v15::chrToInt($D[0]) ^ 0xff ); $D = bin2hex($D); $start = $end = microtime(true); try { $out = RSAPKCS1v15::decrypt($C, $D, $private_key); } catch (\Exception $ex) { $end = microtime(true); var_dump($end - $start); if ($ex->getMessage() === 'Invalid MAC') { echo 'Got the expected error message. No RSA hints given.', PHP_EOL; } } $D = $Dorig;

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.110.0290.01416.66
8.3.100.0350.01316.58
8.3.90.0300.01716.62
8.3.80.0340.01116.60
8.3.70.0320.01116.77
8.3.60.0350.00616.57
8.3.50.0280.00316.85
8.3.40.0180.00917.58
8.3.30.0360.00317.54
8.3.20.0210.01717.47
8.3.10.0300.01317.41
8.3.00.0260.00717.80
8.2.230.0310.01116.64
8.2.220.0270.00816.76
8.2.210.0230.01516.52
8.2.200.0330.00716.86
8.2.190.0260.00716.66
8.2.180.0280.00716.91
8.2.170.0240.01517.75
8.2.160.0320.00717.76
8.2.150.0380.00317.78
8.2.140.0310.00717.54
8.2.130.0360.00717.45
8.2.120.0390.00417.57
8.2.110.0300.01317.63
8.2.100.0360.00717.36
8.2.90.0220.00717.67
8.2.80.0340.00017.55
8.2.70.0390.00317.46
8.2.60.0310.01017.39
8.2.50.0220.00718.50
8.2.40.0260.00018.54
8.2.30.0170.00618.49
8.2.20.0140.00318.44
8.2.10.0210.00618.43
8.2.00.0210.00318.61
8.1.290.0080.01616.10
8.1.280.0220.00316.30
8.1.270.0140.00817.15
8.1.260.0350.00316.72
8.1.250.0330.01117.28
8.1.240.0250.01117.39
8.1.230.0300.00617.39
8.1.220.0310.00017.36
8.1.210.0250.00417.07
8.1.200.0250.00616.98
8.1.190.0120.00617.32
8.1.180.0150.00418.19
8.1.170.0160.00518.34
8.1.160.0120.00518.38
8.1.150.0140.00318.30
8.1.140.0160.00118.33
8.1.130.0200.00218.32
8.1.120.0100.00718.40
8.1.110.0200.00718.38
8.1.100.0220.00718.27
8.1.90.0170.01118.45
8.1.80.0200.00518.28
8.1.70.0180.00618.37
8.1.60.0200.00718.43
8.1.50.0200.00318.38
8.1.40.0140.00518.47
8.1.30.0210.00618.48
8.1.20.0200.00618.59
8.1.10.0190.01018.53
8.1.00.0200.00918.48
8.0.300.0270.00716.52
8.0.290.0300.00316.77
8.0.280.0210.00418.18
8.0.270.0190.00618.15
8.0.260.0170.00818.03
8.0.250.0150.00718.04
8.0.240.0170.00818.10
8.0.230.0220.00418.17
8.0.220.0210.00618.01
8.0.210.0220.00617.99
8.0.200.0170.00918.07
8.0.190.0180.00717.97
8.0.180.0240.00318.15
8.0.170.0210.00618.10
8.0.160.0180.00818.01
8.0.150.0140.00917.97
8.0.140.0210.00318.08
8.0.130.0170.00418.14
8.0.120.0110.00618.12
8.0.110.0120.00418.11
8.0.100.0160.00218.12
8.0.90.0130.00518.12
8.0.80.0190.00018.12
8.0.70.0150.00318.08
8.0.60.0100.00618.12
8.0.50.0130.00218.11
8.0.30.0120.00418.12
8.0.20.0170.00818.15
8.0.10.0130.00618.15
8.0.00.0130.00517.72
7.4.330.0210.00517.12
7.4.320.0280.00917.89
7.4.300.0210.00617.80
7.4.290.0170.00917.94
7.4.280.0220.00517.97
7.4.270.0270.00017.88
7.4.260.0170.01117.87
7.4.250.0130.00415.59
7.4.240.0140.00517.97
7.4.230.0090.01217.98
7.4.220.0140.00917.80
7.4.210.0200.00617.80
7.4.200.0170.00917.80
7.4.190.0160.00917.97
7.4.180.0140.00717.90
7.4.160.0210.00217.93
7.4.150.0160.00917.80
7.4.140.0130.00917.80
7.4.130.0170.00317.44
7.4.120.0140.00717.49
7.4.110.0100.01017.42
7.4.100.0140.00717.49
7.4.90.0110.01117.40
7.4.80.0170.00617.52
7.4.70.0160.00617.40
7.4.60.0160.00717.41
7.4.50.0160.00617.49
7.4.40.0130.00917.45
7.4.30.0140.00817.43
7.4.20.0140.00717.46
7.4.10.0120.00617.45
7.4.00.0170.00217.51
7.3.330.0130.00317.89
7.3.320.0210.00717.93
7.3.310.0180.00717.90
7.3.300.0210.00317.80
7.3.290.0200.00417.86
7.3.280.0150.00817.83
7.3.270.0210.00317.93
7.3.260.0210.00417.86
7.3.250.0110.01017.41
7.3.240.0130.00917.48
7.3.230.0130.00917.38
7.3.220.0200.00417.41
7.3.210.0150.00717.41
7.3.200.0150.00717.35
7.3.190.0090.00917.32
7.3.180.0150.00717.36
7.3.170.0130.00617.33
7.3.160.0110.00917.36
7.3.150.0120.00917.39
7.3.140.0170.00417.42
7.3.130.0160.00417.45
7.3.120.0160.00817.46
7.3.110.0100.00917.38
7.3.100.0100.00716.76
7.3.90.0110.00716.77
7.3.80.0140.01116.78
7.3.70.0160.00616.76
7.3.60.0110.01116.62
7.3.50.0160.00716.59
7.3.40.0160.00616.60
7.3.30.0150.00716.79
7.3.20.0150.01017.12
7.3.10.0210.00517.00
7.3.00.0150.00917.16
7.2.340.0130.00917.38
7.2.330.0180.00517.51
7.2.320.0160.00817.34
7.2.310.0120.01217.52
7.2.300.0170.00817.41
7.2.290.0190.00817.42
7.2.280.0150.00817.42
7.2.270.0120.01217.45
7.2.260.0140.00917.51
7.2.250.0150.01117.38
7.2.240.0140.01017.34
7.2.230.0170.00617.51
7.2.220.0120.01016.92
7.2.210.0180.00516.87
7.2.200.0130.00816.87
7.2.190.0170.00516.80
7.2.180.0100.00916.84
7.2.170.0120.00516.87
7.2.160.0130.00516.84
7.2.150.0120.00817.35
7.2.140.0110.00617.27
7.2.130.0140.00717.33
7.2.120.0130.00917.31
7.2.110.0170.00617.31
7.2.100.0110.00917.28
7.2.90.0240.00916.84
7.2.80.0280.00816.87
7.2.70.0290.00816.39
7.2.60.0250.00816.30
7.2.50.0320.00716.33
7.2.40.0230.00916.37
7.2.30.0300.00816.40
7.2.20.0240.00916.34
7.2.10.0370.00516.33
7.2.00.0210.00816.30
7.1.330.0210.00917.30
7.1.320.0190.00816.62
7.1.310.0280.00916.60
7.1.300.0170.00816.57
7.1.290.0170.01016.54
7.1.280.0140.01016.56
7.1.270.0140.01016.47
7.1.260.0170.00616.55
7.1.250.0180.00816.60
7.1.240.0190.00816.50
7.1.230.0200.00616.40
7.1.220.0190.00716.49
7.1.210.0340.00816.03
7.1.200.0430.00716.06
7.1.190.0340.00415.99
7.1.180.0300.00716.01
7.1.170.0310.01015.32
7.1.160.0270.00715.42
7.1.150.0340.00815.37
7.1.140.0420.00915.39
7.1.130.0290.00815.40
7.1.120.0280.00715.40
7.1.110.0360.00615.40
7.1.100.0180.00915.36
7.1.90.0330.00615.41
7.1.80.0260.00915.45
7.1.70.0210.01015.42
7.1.60.0290.00923.08
7.1.50.0230.01023.08
7.1.40.0370.00823.09
7.1.30.0310.00923.10
7.1.20.0370.00823.08
7.1.10.0190.00715.45
7.1.00.0190.00815.40
7.0.330.0180.01116.28
7.0.320.0250.00717.15
7.0.310.0220.00417.24
7.0.300.0150.00917.22
7.0.290.0200.00517.22
7.0.280.0190.00517.21
7.0.270.0160.00917.20
7.0.260.0180.00917.22
7.0.250.0240.00517.21
7.0.240.0180.00717.31
7.0.230.0220.00417.16
7.0.220.0170.00817.14
7.0.210.0210.00517.25
7.0.200.0220.00317.26
7.0.190.0170.00917.27
7.0.180.0170.00917.25
7.0.170.0170.00717.17
7.0.160.0190.00817.28
7.0.150.0270.00617.32
7.0.140.0230.00617.18
7.0.130.0190.00617.13
7.0.120.0150.01217.17
7.0.110.0200.00717.19
7.0.100.0190.00917.18
7.0.90.0210.00317.12
7.0.80.0230.00217.23
7.0.70.0160.00717.10
7.0.60.0200.00517.18
7.0.50.0200.01017.11
7.0.40.0170.00917.14
7.0.30.0170.00617.24
7.0.20.0170.00517.05
7.0.10.0210.00317.11
7.0.00.0170.00617.17
5.6.400.0170.00816.20
5.6.390.0160.00916.41
5.6.380.0160.00916.31
5.6.370.0210.00717.13
5.6.360.0220.00417.03
5.6.350.0200.00617.17
5.6.340.0160.01017.03
5.6.330.0220.00517.14
5.6.320.0170.00717.07
5.6.310.0220.00417.08
5.6.300.0240.00617.07
5.6.290.0160.01017.07
5.6.280.0150.00817.05
5.6.270.0150.00517.08
5.6.260.0120.00817.11
5.6.250.0140.00717.05
5.6.240.0130.00717.09
5.6.230.0130.00717.08
5.6.220.0120.00817.07
5.6.210.0120.00917.14
5.6.200.0130.00617.13
5.6.190.0120.00917.13
5.6.180.0120.00817.10
5.6.170.0150.00717.06
5.6.160.0160.00417.07
5.6.150.0150.00517.08
5.6.140.0140.00717.10
5.6.130.0180.00217.07
5.6.120.0160.00317.09
5.6.110.0140.00617.12
5.6.100.0180.00117.11
5.6.90.0130.00717.06
5.6.80.0150.00617.04
5.6.70.0140.00517.10
5.6.60.0140.00617.07
5.6.50.0120.00716.96
5.6.40.0120.00616.97
5.6.30.0100.00916.99
5.6.20.0090.00816.96
5.6.10.0120.00716.96
5.6.00.0130.00517.06

preferences:
36.7 ms | 403 KiB | 5 Q