HEX
Server: LiteSpeed
System: Linux sv4.hami.host 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64
User: armgroup (1008)
PHP: 8.2.32
Disabled: show_source, system, shell_exec, passthru, exec, popen, proc_open, mail, socket_create, socket_create_listen, socket_create_pair, link, dl, openlog, syslog, stream_socket_server, curl_multi_init
Upload Files
File: //home/armgroup/libs/vendor/shetabit/multipay/src/Drivers/Pasargad/Utils/RSAProcessor.php
<?php

namespace Shetabit\Multipay\Drivers\Pasargad\Utils;

use Shetabit\Multipay\Drivers\Pasargad\Utils\RSA;

class RSAProcessor
{
    public const KEY_TYPE_XML_FILE = 'xml_file';
    public const KEY_TYPE_XML_STRING = 'xml_string';

    private $publicKey = null;
    private $privateKey = null;
    private $modulus = null;
    private $keyLength = "1024";

    public function __construct($key, $keyType = null)
    {
        $xmlObject = null;
        $keyType = is_null($keyType) ? null : strtolower($keyType);

        if ($keyType == null || $keyType == self::KEY_TYPE_XML_STRING) {
            $xmlObject = simplexml_load_string($key);
        } elseif ($keyType == self::KEY_TYPE_XML_FILE) {
            $xmlObject = simplexml_load_file($key);
        }

        $this->modulus = RSA::binaryToNumber(base64_decode($xmlObject->Modulus));
        $this->publicKey = RSA::binaryToNumber(base64_decode($xmlObject->Exponent));
        $this->privateKey = RSA::binaryToNumber(base64_decode($xmlObject->D));
        $this->keyLength = strlen(base64_decode($xmlObject->Modulus)) * 8;
    }

    /**
     * Retrieve public key
     *
     * @return string|null
     */
    public function getPublicKey()
    {
        return $this->publicKey;
    }

    /**
     * Retrieve private key
     *
     * @return string|null
     */
    public function getPrivateKey()
    {
        return $this->privateKey;
    }

    /**
     * Retrieve key length
     *
     * @return integer
     */
    public function getKeyLength()
    {
        return $this->keyLength;
    }

    /**
     * Retrieve modulus
     *
     * @return string|null
     */
    public function getModulus()
    {
        return $this->modulus;
    }

    /**
     * Encrypt given data
     *
     * @param string $data
     *
     * @return string
     */
    public function encrypt($data)
    {
        return base64_encode(RSA::rsaEncrypt($data, $this->publicKey, $this->modulus, $this->keyLength));
    }

    /**
     * Decrypt given data
     *
     * @param $data
     *
     * @return string
     */
    public function decrypt($data)
    {
        return RSA::rsaDecrypt($data, $this->privateKey, $this->modulus, $this->keyLength);
    }

    /**
     * Sign given data
     *
     * @param string $data
     *
     * @return string
     */
    public function sign($data)
    {
        return RSA::rsaSign($data, $this->privateKey, $this->modulus, $this->keyLength);
    }

    /**
     * Verify RSA data
     *
     * @param string $data
     *
     * @return boolean
     */
    public function verify($data)
    {
        return RSA::rsaVerify($data, $this->publicKey, $this->modulus, $this->keyLength);
    }
}