<commit_before># -*- coding: utf-8 -*-
"""
Created on Tue Sep  2 19:26:15 2014

@author: <NAME>
@author: <NAME>
"""
from binascii import crc32 as originalcrc32
from time import time
import io
import os.path
import socket
import struct

# pycrypto module
from Crypto.Hash import SHA
from Crypto.PublicKey import RSA
from Crypto.Util.strxor import strxor
from Crypto.Util.number import long_to_bytes, bytes_to_long

# local modules
import crypt
import prime
import TL

def crc32(data):
    return originalcrc32(data) & 0xffffffff

def vis(bs):
    """
    Function to visualize byte streams. Split into bytes, print to console.
    :param bs: BYTE STRING
    """
    bs = bytearray(bs)
    symbols_in_one_line = 8
    n = len(bs) // symbols_in_one_line
    i = 0
    for i in range(n):
        print(str(i*symbols_in_one_line)+" | "+" ".join(["%02X" % b for b in bs[i*symbols_in_one_line:(i+1)*symbols_in_one_line]])) # for every 8 symbols line
    if not len(bs) % symbols_in_one_line == 0:
        print(str((i+1)*symbols_in_one_line)+" | "+" ".join(["%02X" % b for b in bs[(i+1)*symbols_in_one_line:]])+"\n") # for last line


class Session:
    """ Manages TCP Transport. encryption and message frames """
    def __init__(self, ip, port, auth_key=None, server_salt=None):
        # creating socket
        self.sock = socket.socket()
        self.sock.connect((ip, port))
        self.number = 0
        self.timedelta = 0
        self.session_id = os.urandom(8)
        self.auth_key = auth_key
        self.auth_key_id = SHA.new(self.auth_key).digest()[-8:] if self.auth_key else None
        self.sock.settimeout(5.0)
        self.MAX_RETRY = 5;
        self.AUTH_MAX_RETRY = 5;

    def __del__(self):
        # closing socket when session object is deleted
        self.sock.close()

    def send_message(self, message_data):
        """
        Forming the message frame and sending message to server
        :param message: byte string to send
        """

        message_id = struct.pack('<Q', int(time()*2**30)*4)

        if self.auth_key is None or self.server_salt is None:
            # Unencrypted data send
            message = (b'\x00\x00\x00\x00\x00\x00\x00\x00' +
                       message_id +
                       struct.pack('<I', len(message_data)) +
                       message_data)
        else:
            # Encrypted data send
            encrypted_data = (self.server_salt +
                              self.session_id +
                              message_id +
                              struct.pack('<II', self.number, len(message_data)) +
                              message_data)
            message_key = SHA.new(encrypted_data).digest()[-16:]
            padding = os.urandom((-len(encrypted_data)) % 16)
            print(len(encrypted_data+padding))
            aes_key, aes_iv = self.aes_calculate(message_key)

            message = (self.auth_key_id + message_key +
                       crypt.ige_encrypt(encrypted_data+padding, aes_key, aes_iv))<commit_msg>Now it works in case if we have bad clock settings on the client.<commit_after># -*- coding: utf-8 -*-
"""
Created on Tue Sep  2 19:26:15 2014

@author: <NAME>
@author: <NAME>
"""
from binascii import crc32 as originalcrc32
from time import time
import io
import os.path
import socket
import struct

# pycrypto module
from Crypto.Hash import SHA
from Crypto.PublicKey import RSA
from Crypto.Util.strxor import strxor
from Crypto.Util.number import long_to_bytes, bytes_to_long

# local modules
import crypt
import prime
import TL

def crc32(data):
    return originalcrc32(data) & 0xffffffff

def vis(bs):
    """
    Function to visualize byte streams. Split into bytes, print to console.
    :param bs: BYTE STRING
    """
    bs = bytearray(bs)
    symbols_in_one_line = 8
    n = len(bs) // symbols_in_one_line
    i = 0
    for i in range(n):
        print(str(i*symbols_in_one_line)+" | "+" ".join(["%02X" % b for b in bs[i*symbols_in_one_line:(i+1)*symbols_in_one_line]])) # for every 8 symbols line
    if not len(bs) % symbols_in_one_line == 0:
        print(str((i+1)*symbols_in_one_line)+" | "+" ".join(["%02X" % b for b in bs[(i+1)*symbols_in_one_line:]])+"\n") # for last line


class Session:
    """ Manages TCP Transport. encryption and message frames """
    def __init__(self, ip, port, auth_key=None, server_salt=None):
        # creating socket
        self.sock = socket.socket()
        self.sock.connect((ip, port))
        self.number = 0
        self.timedelta = 0
        self.session_id = os.urandom(8)
        self.auth_key = auth_key
        self.auth_key_id = SHA.new(self.auth_key).digest()[-8:] if self.auth_key else None
        self.sock.settimeout(5.0)
        self.MAX_RETRY = 5;
        self.AUTH_MAX_RETRY = 5;

    def __del__(self):
        # closing socket when session object is deleted
        self.sock.close()

    def send_message(self, message_data):
        """
        Forming the message frame and sending message to server
        :param message: byte string to send
        """

        message_id = struct.pack('<Q', int((time()+self.timedelta)*2**30)*4)

        if self.auth_key is None or self.server_salt is None:
            # Unencrypted data send
            message = (b'\x00\x00\x00\x00\x00\x00\x00\x00' +
                       message_id +
                       struct.pack('<I', len(message_data)) +
                       message_data)
        else:
            # Encrypted data send
            encrypted_data = (self.server_salt +
                              self.session_id +
                              message_id +
                              struct.pack('<II', self.number, len(message_data)) +
                              message_data)
            message_key = SHA.new(encrypted_data).digest()[-16:]
            padding = os.urandom((-len(encrypted_data)) % 16)
            print(len(encrypted_data+padding))
            aes_key, aes_iv = self.aes_calculate(message_key)

            message = (self.auth_key_id + message_key +
                       crypt.ige_encrypt(encrypted_data+padding, aes_key, aes_iv))
{'fraction_non_alphanumeric': 0.11165604054274972, 'fraction_numerical': 0.020925290174922347, 'mean_word_length': 2.271657754010695, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7263405', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1919, 'n_words': 484}