def sha1(data): if not isinstance(data, Bytes): data = Bytes(data) hasher = sha1() hasher.update(data) hash_digest = hasher.digest() return Bytes(hash_digest)
def send_raw(self, data): msg_len = len(data) >> 2 tcp_stream = self._get_tcp_stream() if self._send_abridged_flag: tcp_stream.write(chr(0xef)) self._send_abridged_flag = False if msg_len < 127: tcp_stream.write(Bytes.from_int(msg_len, 1, False)) else: tcp_stream.write(chr(0x7f)) tcp_stream.write(Bytes.from_int(msg_len, 3, False)) tcp_stream.write(data)
def aes_ige_encrypt(data, aes_key_iv): cipher = AES(aes_key_iv.key) iv_1 = aes_key_iv.iv[:Crypto._IGE_BLOCK_SIZE] iv_2 = aes_key_iv.iv[Crypto._IGE_BLOCK_SIZE:] data = [ data[i:i + Crypto._IGE_BLOCK_SIZE] for i in range(0, len(data), Crypto._IGE_BLOCK_SIZE) ] for i, chunk in enumerate(data): iv_1 = data[i] = Bytes(cipher.encrypt(chunk ^ iv_1)) ^ iv_2 iv_2 = chunk return Bytes.flattern(data)
def deserialize_from(self, stream): # check ids of subclasses oid = Bytes(stream.read(4)).to_int(False, False) ctor = self._ref_ctor assert ctor.ID == oid ctor.deserialize_from(stream) return self
def __init__(self, src=None): self._data = Bytes() if src is not None: if isinstance(src, QueueByteStream): self._copy(src) elif isinstance(src, Bytes): self.clear() self.write(src)
def read_raw(self): tcp_stream = self._get_tcp_stream() packet_len = ord(tcp_stream.read(1)) if packet_len == 0x7f: packet_len = Bytes(self._tcp.read(3)).to_int(False, False) packet_len *= 4 packet = self._tcp.read(packet_len) if packet == b"\x6c\xfe\xff\xff": raise MTProtoFatalError("Error -404 while reading message") stream = TLBytesStream(packet) return stream
def serialize_string(s): slen = len(s) if slen < 254: pad_count = 4 - ((slen + 1) % 4) if pad_count == 4: pad_count = 0 b = Bytes(slen + 1) b[0] = slen b[1:] = s if pad_count != 0: # TODO: fill with random data b += Bytes('\x00' * pad_count) else: pad_count = 4 - (slen % 4) if pad_count == 4: pad_count = 0 b = Bytes(slen + 1) b[0] = 254 b[1:4] = [slen % 256, (slen >> 8) % 256, (slen >> 16) % 256] b[4:] = s if pad_count != 0: b += Bytes('\x00' * pad_count) return b
def serialize(self): buff = Bytes() for pname in self._params_ordered: buff += self._params_map[pname].serialize() return buff
def encrypt_client_dh(data, server_dh_aes_key_iv): data_with_hash = Hash.sha1(data) + data if len(data_with_hash) % 16 != 0: pad = 16 - len(data_with_hash) % 16 data_with_hash += Bytes(os.urandom(pad)) return Crypto.aes_ige_encrypt(data_with_hash, server_dh_aes_key_iv)
def sha256(data): hasher = sha256() hasher.update(data) hash_digest = hasher.digest() return Bytes(hash_digest)
def serialize(self): return Bytes.from_int(self.get(), 32, True, signed=self._signed)
def deserialize_from(self, stream): self.set(Bytes(stream.read(32)).to_int(True)) return self
def rsa_encrypt(plain, key): if isinstance(plain, Bytes): plain = plain.to_int(True) return Bytes.from_int(pow(plain, key.e, key.n), 256, True)
def set(self, v): if v is None: v = 0 elif not isinstance(v, (int, long)): v = Bytes(v).to_int(False, self._signed) super(TL_int, self).set(v)
def serialize_int32(i, signed=True): return Bytes.from_int(i, 4, False, signed)
def clear(self): if len(self._data) != 0: del self._data self._data = Bytes()
def serialize(self): ser_id = Bytes.from_int(self.ID, 4, False, False) ser_params = super(TLFunction, self).serialize() return ser_id + ser_params
def serialize_long(l, signed=True): return Bytes.from_int(l, 8, False, signed)
def serialize(self): itos = Bytes.from_int(self.get(), self._byte_len, True, self._signed) tls = TL_string(itos) return tls.serialize()
def serialize(self): if self.is_flagged(): return self._ref.serialize() return Bytes()
def serialize(self): v = self.get() return Bytes.from_int(v, 16, True)
def deserialize_from(self, stream): tls = TL_string() tls.deserialize_from(stream) self.set(Bytes(tls.get()).to_int(True, self._signed)) return self