예제 #1
0
 def sha1(data):
     if not isinstance(data, Bytes):
         data = Bytes(data)
     hasher = sha1()
     hasher.update(data)
     hash_digest = hasher.digest()
     return Bytes(hash_digest)
예제 #2
0
 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)
예제 #3
0
    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)
예제 #4
0
 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
예제 #5
0
 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)
예제 #6
0
 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
예제 #7
0
 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
예제 #8
0
 def serialize(self):
     buff = Bytes()
     for pname in self._params_ordered:
         buff += self._params_map[pname].serialize()
     return buff
예제 #9
0
파일: crypto.py 프로젝트: krow89/krgram
 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)
예제 #10
0
 def sha256(data):
     hasher = sha256()
     hasher.update(data)
     hash_digest = hasher.digest()
     return Bytes(hash_digest)
예제 #11
0
	def serialize(self):
		return Bytes.from_int(self.get(), 32, True, signed=self._signed)
예제 #12
0
	def deserialize_from(self, stream):
		self.set(Bytes(stream.read(32)).to_int(True))
		return self
예제 #13
0
 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)
예제 #14
0
파일: native.py 프로젝트: krow89/krgram
 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)
예제 #15
0
 def serialize_int32(i, signed=True):
     return Bytes.from_int(i, 4, False, signed)
예제 #16
0
 def clear(self):
     if len(self._data) != 0:
         del self._data
         self._data = Bytes()
예제 #17
0
 def serialize(self):
     ser_id = Bytes.from_int(self.ID, 4, False, False)
     ser_params = super(TLFunction, self).serialize()
     return ser_id + ser_params
예제 #18
0
 def serialize_long(l, signed=True):
     return Bytes.from_int(l, 8, False, signed)
예제 #19
0
	def serialize(self):
		itos = Bytes.from_int(self.get(), self._byte_len, True, self._signed)
		tls = TL_string(itos)
		return tls.serialize()
예제 #20
0
 def serialize(self):
     if self.is_flagged():
         return self._ref.serialize()
     return Bytes()
예제 #21
0
	def serialize(self):
		v = self.get()
		return Bytes.from_int(v, 16, True)
예제 #22
0
	def deserialize_from(self, stream):
		tls = TL_string()
		tls.deserialize_from(stream)
		self.set(Bytes(tls.get()).to_int(True, self._signed))
		return self