3v4l.org

run code in 300+ PHP versions simultaneously
<?php function m24g($bArr, $modulus, $exponent) { $publicKey = m23h($modulus, $exponent); if (openssl_public_encrypt($bArr, $encryptedData, $publicKey, OPENSSL_PKCS1_PADDING)) { return $encryptedData; } else { throw new Exception('Encryption failed: ' . openssl_error_string()); } } function m23h($modulus, $exponent) { $modulus = ensure_even_length($modulus); $exponent = ensure_even_length($exponent); $rsa = [ 'n' => base64url_encode(hex2bin($modulus)), 'e' => base64url_encode(hex2bin($exponent)), ]; $keyDetails = [ 'kty' => 'RSA', 'n' => $rsa['n'], 'e' => $rsa['e'], ]; return jwkToPem($keyDetails); } function ensure_even_length($hex) { return (strlen($hex) % 2 != 0) ? '0' . $hex : $hex; } function base64url_encode($data) { return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); } function base64url_decode($data) { return base64_decode(strtr($data, '-_', '+/')); } function jwkToPem(array $jwk) { $modulus = base64url_decode($jwk['n']); $publicExponent = base64url_decode($jwk['e']); $components = [ 'modulus' => $modulus, 'publicExponent' => $publicExponent, ]; $asn1Sequence = createPublicKey($components); $pem = "-----BEGIN PUBLIC KEY-----\n" . chunk_split(base64_encode($asn1Sequence), 64, "\n") . "-----END PUBLIC KEY-----\n"; return $pem; } function createPublicKey($components) { $modulus = $components['modulus']; $publicExponent = $components['publicExponent']; $modulus = "\x00" . $modulus; // Ensure positive number $sequence = pack('Ca*a*', 0x30, createLengthPrefix(strlen($modulus) + strlen($publicExponent) + 4), pack('Ca*', 0x02, createLengthPrefix(strlen($modulus)) . $modulus) . pack('Ca*', 0x02, createLengthPrefix(strlen($publicExponent)) . $publicExponent)); return pack('Ca*a*', 0x30, createLengthPrefix(strlen($sequence) + 13), pack('H*', '300D06092A864886F70D0101010500') . pack('Ca*', 0x03, createLengthPrefix(strlen($sequence) + 1) . "\x00" . $sequence)); } function createLengthPrefix($length) { if ($length < 128) { return chr($length); } $temp = ltrim(pack('N', $length), "\x00"); return chr(0x80 | strlen($temp)) . $temp; } // Usage example try { $modulus = '113744026899259137585420519641795445506522315843166624587737104823905995993308512746423780711220091509477248711684203860721990053300351123157272120027826616226336455748256059222713368430653450802331260182403534624073361236909650202772541076543992882238347687352553725072578344237693663853480535496066481835463'; // Hexadecimal string of modulus $exponent = '65537'; // Hexadecimal string of exponent $dataToEncrypt = "912c1366-d5e0-c63a-e91d-7d6a495da509|147963"; $encryptedData = m24g($dataToEncrypt, $modulus, $exponent); echo "Encrypted Data: " . base64_encode($encryptedData) . "\n"; } catch (Exception $e) { echo 'Error: ' . $e->getMessage() . "\n"; } ?>

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.90.0040.01126.77
8.3.80.0040.00418.55
8.3.70.0340.01016.58
8.3.60.0410.00716.74
8.3.50.0120.00316.75
8.3.40.0240.01217.59
8.3.30.0260.00317.61
8.3.20.0230.01217.50
8.3.10.0250.00717.48
8.3.00.0260.00717.63
8.2.210.0040.01126.77
8.2.200.0060.00318.18
8.2.190.0250.00416.58
8.2.180.0260.00516.63
8.2.170.0270.00317.50
8.2.160.0230.00017.47
8.2.150.0210.00917.63
8.2.140.0170.01117.38
8.2.130.0220.00317.38
8.2.120.0230.00017.73
8.2.110.0130.01017.63
8.2.100.0150.01117.63
8.2.90.0230.00317.63
8.2.80.0240.00017.75
8.2.70.0160.01217.50
8.2.60.0220.00617.62
8.2.50.0180.00617.50
8.2.40.0250.00517.23
8.2.30.0180.00617.38
8.2.20.0200.00317.75
8.2.10.0210.00017.50
8.2.00.0210.00017.75
8.1.290.0090.00030.84
8.1.280.0180.00816.58
8.1.270.0180.00517.48
8.1.260.0140.00917.38
8.1.250.0230.00517.50
8.1.240.0190.00517.38
8.1.230.0140.01017.36
8.1.220.0170.00617.38
8.1.210.0230.00617.25
8.1.200.0190.00517.23
8.1.190.0160.00817.23
8.1.180.0210.00417.10
8.1.170.0210.00017.35
8.1.160.0100.01317.23
8.1.150.0200.00417.25
8.1.140.0170.00717.34
8.1.130.0150.00717.11
8.1.120.0170.00617.38
8.1.110.0160.00417.46
8.1.100.0100.01017.35
8.1.90.0150.00817.47
8.1.80.0280.00017.47
8.1.70.0170.00417.47
8.1.60.0240.00917.59
8.1.50.0360.00417.25
8.1.40.0390.00317.38
8.1.30.0200.00717.59
8.1.20.0320.00917.60
8.1.10.0330.01117.46
8.1.00.0290.01517.00

preferences:
36.99 ms | 402 KiB | 5 Q