Cleaning up..:

This commit is contained in:
danogentili 2016-09-10 19:53:08 +02:00
parent 3a635d8bb0
commit c51e4cb9d8
4 changed files with 29 additions and 125 deletions

View File

@ -0,0 +1,20 @@
<?php
/*
Copyright 2016 Daniil Gentili
(https://daniil.it)
This file is part of MadelineProto.
MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU General Public License along with the MadelineProto.
If not, see <http://www.gnu.org/licenses/>.
*/
namespace danog\MadelineProto;
/**
* Manages encryption and message frames.
*/
class MTProto extends Tools {
}

View File

@ -13,7 +13,7 @@ If not, see <http://www.gnu.org/licenses/>.
namespace danog\MadelineProto;
/**
* Manages encryption and message frames.
* Manages session and the creation of authorization keys.
*/
class Session extends Tools
{
@ -176,8 +176,6 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
$in = $content_related ? 1 : 0;
$value = $this->seq_no;
$this->seq_no += $in;
var_dump((($value * 2) + $in));
return ($value * 2) + $in;
}
@ -274,10 +272,11 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
public function method_call($method, $args)
{
$opts = $this->tl->get_opts($method);
foreach (range(1, $this->settings['max_tries']['query']) as $i) {
try {
$this->send_message($this->tl->serialize_method($method, $args), $this->tl->content_related($method));
$server_answer = $this->recv_message();
if ($opts["requires_answer"]) $server_answer = $this->recv_message();
} catch (Exception $e) {
$this->log->log('An error occurred while calling method '.$method.': '.$e->getMessage().' in '.$e->getFile().':'.$e->getLine().'. Recreating connection and retrying to call method...');
unset($this->sock);

View File

@ -263,4 +263,9 @@ class TL
'msg_resend_ans_req',
]);
}
public function get_opts($method) {
$opts = ["requires_answer" => !in_array($method, [
'msgs_ack',
]]
}
}

View File

@ -1,120 +0,0 @@
<?php
namespace danog\MadelineProto;
// by https://github.com/mgp25
class TelegramEncryption
{
public $key;
public $iv;
public $debug;
public $rijndael;
public function __construct($key, $iv, $debug = false)
{
$this->key = $key;
$this->iv = $iv;
$this->debug = $debug;
$this->rijndael = new \phpseclib\Crypt\Rijndael(\phpseclib\Crypt\Rijndael::MODE_ECB);
$this->rijndael->setKeyLength(128);
$this->rijndael->setKey($this->key);
}
public function decrypt($message = null)
{
$key = $this->key;
$blockSize = $this->rijndael->block_size;
$xPrev = substr($this->iv, 0, $blockSize);
$yPrev = substr($this->iv, $blockSize, strlen($this->iv));
$decrypted = '';
for ($i = 0; $i < strlen($message); $i += $blockSize) {
$x = substr($message, $i, $blockSize);
$this->debugLog('x: '.$this->_c($x)."\n");
$yXOR = $this->exor($x, $yPrev);
$this->debugLog('yPrev: '.$this->_c($yPrev)."\n");
$this->debugLog('yXOR: '.$this->_c($yXOR)."\n");
$yFinal = $this->rijndael->encrypt($yXOR);
$yFinal = str_pad($yFinal, strlen($xPrev), "\x00");
$this->debugLog('yFinal: '.$this->_c($yFinal)."\n");
$y = $this->exor($yFinal, $xPrev);
$this->debugLog('xPrev: '.$this->_c($xPrev)."\n");
$this->debugLog('y: '.$this->_c($y)."\n");
$xPrev = $x;
$yPrev = $y;
$decrypted .= $y;
$this->debugLog('Currently Decrypted: '.$this->_c($decrypted)."\n\n");
}
return $decrypted;
}
public function encrypt()
{
$key = $this->key;
$message = $this->plainText;
$blockSize = $this->rijndael->block_size;
$xPrev = substr($this->iv, $blockSize, strlen($this->iv));
$yPrev = substr($this->iv, 0, $blockSize);
$encrypted = '';
for ($i = 0; $i < strlen($message); $i += $blockSize) {
$x = substr($message, $i, $blockSize);
$this->debugLog('x: '.$this->_c($x)."\n");
$yXOR = $this->exor($x, $yPrev);
$this->debugLog('yPrev: '.$this->_c($yPrev)."\n");
$this->debugLog('yXOR: '.$this->_c($yXOR)."\n");
$yFinal = $this->rijndael->encrypt($yXOR);
$yFinal = str_pad($yFinal, strlen($xPrev), "\x00");
$this->debugLog('yFinal: '.$this->_c($yFinal)."\n");
$y = $this->exor($yFinal, $xPrev);
$this->debugLog('xPrev: '.$this->_c($xPrev)."\n");
$this->debugLog('y: '.$this->_c($y)."\n");
$xPrev = $x;
$yPrev = $y;
$encrypted .= $y;
$this->debugLog('Currently encrypted: '.$this->_c($encrypted)."\n\n");
}
return $encrypted;
}
public function debugLog($message)
{
if ($this->debug) {
echo $message;
}
}
public function exor($array1, $array2)
{
$len = (strlen($array1) <= strlen($array2)) ? strlen($array2) : strlen($array1);
$array1 = str_pad($array1, $len, "\x00");
$array2 = str_pad($array2, $len, "\x00");
$res = '';
for ($i = 0; $i < $len; $i++) {
$res .= $array1[$i] ^ $array2[$i];
}
return $res;
}
public function _c($binary)
{
return sprintf('[%s]', chunk_split(bin2hex($binary), 4, ' '));
}
}