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 = gmp_import(hex2bin($modulus)); $exponent = gmp_import(hex2bin($exponent)); $rsa = [ 'n' => gmp_strval($modulus, 16), 'e' => gmp_strval($exponent, 16), ]; $keyDetails = [ 'kty' => 'RSA', 'n' => base64url_encode(hex2bin($rsa['n'])), 'e' => base64url_encode(hex2bin($rsa['e'])), ]; $pem = jwkToPem($keyDetails); return $pem; } function base64url_encode($data) { return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); } function jwkToPem(array $jwk) { $components = array( 'modulus' => base64url_decode($jwk['n']), 'publicExponent' => base64url_decode($jwk['e']) ); $pem = "-----BEGIN PUBLIC KEY-----\n" . chunk_split(base64_encode(createPublicKey($components)), 64, "\n") . "-----END PUBLIC KEY-----\n"; return $pem; } function base64url_decode($data) { return base64_decode(strtr($data, '-_', '+/')); } function createPublicKey($components) { $modulus = $components['modulus']; $publicExponent = $components['publicExponent']; $sequence = new \FG\ASN1\Universal\Sequence(); $sequence->addChild(new \FG\ASN1\Universal\Integer($modulus)); $sequence->addChild(new \FG\ASN1\Universal\Integer($publicExponent)); $bitString = new \FG\ASN1\Universal\BitString($sequence->getBinary()); $outerSequence = new \FG\ASN1\Universal\Sequence(); $outerSequence->addChild(new \FG\ASN1\Universal\Sequence()); $outerSequence->addChild($bitString); return $outerSequence->getBinary(); } // 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.80.0060.00318.80
8.3.70.0480.00316.84
8.3.60.0550.00616.75
8.3.50.0100.01516.95
8.3.40.0380.01317.73
8.3.30.0350.01317.88
8.3.20.0280.00817.38
8.3.10.0080.01217.63
8.3.00.0200.00317.49
8.2.200.0060.00618.54
8.2.190.0220.01116.75
8.2.180.0250.00716.59
8.2.170.0140.00917.88
8.2.160.0210.00717.50
8.2.150.0330.00717.38
8.2.140.0140.01017.60
8.2.130.0190.00417.63
8.2.120.0180.00617.50
8.2.110.0220.00417.75
8.2.100.0270.00317.73
8.2.90.0370.00717.63
8.2.80.0320.00617.75
8.2.70.0300.00617.75
8.2.60.0280.00917.75
8.2.50.0300.00317.50
8.2.40.0260.01317.38
8.2.30.0420.00017.75
8.2.20.0360.00417.63
8.2.10.0340.00917.63
8.2.00.0380.00717.50
8.1.290.0100.00030.84
8.1.280.0360.00616.58
8.1.270.0310.00617.25
8.1.260.0290.00917.50
8.1.250.0170.01017.35
8.1.240.0190.00517.38
8.1.230.0240.00017.63
8.1.220.0180.00717.48
8.1.210.0280.00617.25
8.1.200.0340.00717.38
8.1.190.0260.00417.35
8.1.180.0240.00717.25
8.1.170.0220.00317.47
8.1.160.0380.00817.49
8.1.150.0280.01217.25
8.1.140.0320.00717.22
8.1.130.0320.00717.47
8.1.120.0360.00417.59
8.1.110.0290.01017.38
8.1.100.0270.00317.35
8.1.90.0250.00417.11
8.1.80.0220.00917.48
8.1.70.0260.01017.38
8.1.60.0340.00317.38
8.1.50.0230.00817.50
8.1.40.0260.00017.60
8.1.30.0260.00417.50
8.1.20.0300.01017.59
8.1.10.0270.01017.50
8.1.00.0290.00617.71

preferences:
37.02 ms | 401 KiB | 5 Q