. * * @author Daniil Gentili * @copyright 2016-2020 Daniil Gentili * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 * * @link https://docs.madelineproto.xyz MadelineProto documentation */ namespace danog\MadelineProto; use danog\MadelineProto\TL\TL; /** * RSA class. */ class RSA { use \danog\Serializable; /** * Exponent. * * @var \tgseclib\Math\BigInteger */ public $e; /** * Modulus. * * @var \tgseclib\Math\BigInteger */ public $n; /** * Fingerprint. * * @var string */ public $fp; /** * Load RSA key. * * @param TL $TL TL serializer * @param string $rsa_key RSA key * * @return \Generator * * @psalm-return \Generator */ public function load(TL $TL, string $rsa_key): \Generator { $key = \tgseclib\Crypt\RSA::load($rsa_key); $this->n = Tools::getVar($key, 'modulus'); $this->e = Tools::getVar($key, 'exponent'); $this->fp = \substr(\sha1((yield from $TL->serializeObject(['type' => 'bytes'], $this->n->toBytes(), 'key')).(yield from $TL->serializeObject(['type' => 'bytes'], $this->e->toBytes(), 'key')), true), -8); return $this; } /** * Sleep function. * * @return array */ public function __sleep(): array { return ['e', 'n', 'fp']; } /** * Encrypt data. * * @param string $data Data to encrypt * * @return string */ public function encrypt($data): string { return (new \tgseclib\Math\BigInteger((string) $data, 256))->powMod($this->e, $this->n)->toBytes(); } }