<commit_before>from present.miniPresent import MiniPresent

__author__ = '<NAME>'


class ResultHolder(object):
    def __init__(self, plain_text, key, encrypted_text):
        self.plain_text = plain_text
        self.key = key
        self.encrypted_text = encrypted_text

    def __unicode__(self):
        return u'plain_text = %s | key = %s | encrypted_text = %s' % (
            str(self.plain_text), str(self.key), str(self.encrypted_text))

    def __str__(self):
        return self.__unicode__()


class CoincidenceHolder(object):
    def __init__(self, results):
        self.results = results
        self.coincidence_map = {}

    def check_coincidences(self):
        for result in self.results:
            if self.coincidence_map.has_key(result.encrypted_text):
                coincidence_amount = self.coincidence_map.get(result.encrypted_text)
                coincidence_amount += 1
                self.coincidence_map[result.encrypted_text] = coincidence_amount
            else:
                self.coincidence_map[result.encrypted_text] = 1


def run_for_lab():
    results = []
    plain_text = 0
    for key in range(0, 2 << 16 - 1):
        cipher = MiniPresent(key)
        encrypted_text = cipher.encrypt(plain_text)
        decrypted_text = cipher.decrypt(encrypted_text)
        result = ResultHolder(decrypted_text, key, encrypted_text)
        results.append(result)
        print result
    coincidence_holder = CoincidenceHolder(results)
    coincidence_holder.check_coincidences()
    result_map = {}
    for i in coincidence_holder.coincidence_map.values():
        if result_map.has_key(i):
            result_amount = result_map.get(i)
            result_amount += 1
            result_map[i] = result_amount
        else:
            result_map[i] = 1
    all_values_amount = 0
    line_chart = pygal.Line()
    line_chart.title = 'Coincidence chart'
    line_chart.x_title = 'Number of identical numbers'
    line_chart.y_title = 'Number of identical numbers sets'
    for key, value in result_map.items():
        if value == 1:
            print("%d equal values were met %d time") % (key, value)
        else:
            print("%d equal values were met %d times") % (key, value)
        all_values_amount += key * value
    line_chart.x_labels = map(str, result_map.keys())
    line_chart.add('coincidence', result_map.values())
    print("all values amount = %d") % all_values_amount
    line_chart.render_to_file('coincidence_chart.svg')
    return results, coincidence_holder, result_map


def test_all():
    key = 0x0
    plain_1 = 0
    plain_2 = "1"
    plain_3 = "2"
    print plain_1
    print plain_2
    print plain_3
    cipher = MiniPresent(key)
    encrypted_1 = cipher.encrypt(plain_1)
    encrypted_2 = cipher.encrypt(plain_2)
    encrypted_3 = cipher.encrypt(plain_3)
    enc_1 = encrypted_1
    enc_2 = encrypted_2.encode('hex')
    enc_3 = encrypted_3.encode('hex')
    print enc_1
    print enc_2
    print enc_3

    decrypted_1 = cipher.decrypt(encrypted_1)
    decrypted_2 = cipher.decrypt(encrypted_2)
    decrypted_3 = cipher.decrypt(encrypted_3)
    decr_1 = decrypted_1
    decr_2 = decrypted_2.encode('hex')
    decr_3 = decrypted_3.encode('hex')
    print decr_1
    print decr_2.decode('hex')
    print decr_3.decode('hex')

    results = run_for_lab()<commit_msg>change all prints to file.write()<commit_after># coding=utf-8
import pygal
from present.miniPresent import MiniPresent

__author__ = '<NAME>'

with open('./results.txt', 'w+') as result_file:
    class ResultHolder(object):
        def __init__(self, plain_text, key, encrypted_text):
            self.plain_text = plain_text
            self.key = key
            self.encrypted_text = encrypted_text

        def __unicode__(self):
            return u'plain_text = %s | key = %s | encrypted_text = %s' % (
                str(self.plain_text), str(self.key), str(self.encrypted_text))

        def __str__(self):
            return self.__unicode__()


    class CoincidenceHolder(object):
        def __init__(self, results):
            self.results = results
            self.coincidence_map = {}

        def check_coincidences(self):
            for result in self.results:
                if self.coincidence_map.has_key(result.encrypted_text):
                    coincidence_amount = self.coincidence_map.get(result.encrypted_text)
                    coincidence_amount += 1
                    self.coincidence_map[result.encrypted_text] = coincidence_amount
                else:
                    self.coincidence_map[result.encrypted_text] = 1


    def run_for_lab():
        results = []
        plain_text = 0
        for key in range(0, 2 << 16 - 1):
            cipher = MiniPresent(key)
            encrypted_text = cipher.encrypt(plain_text)
            decrypted_text = cipher.decrypt(encrypted_text)
            result = ResultHolder(decrypted_text, key, encrypted_text)
            results.append(result)
            result_file.write(str(result))
        coincidence_holder = CoincidenceHolder(results)
        coincidence_holder.check_coincidences()
        result_map = {}
        for i in coincidence_holder.coincidence_map.values():
            if result_map.has_key(i):
                result_amount = result_map.get(i)
                result_amount += 1
                result_map[i] = result_amount
            else:
                result_map[i] = 1
        all_values_amount = 0
        line_chart = pygal.Line()
        line_chart.title = 'Coincidence chart'
        line_chart.x_title = 'Number of identical numbers'
        line_chart.y_title = 'Number of identical numbers sets'
        for key, value in result_map.items():
            if value == 1:
                result_file.write(("%d equal values were met %d time") % (key, value))
            else:
                result_file.write(("%d equal values were met %d times") % (key, value))
            all_values_amount += key * value
        line_chart.x_labels = map(str, result_map.keys())
        line_chart.add('coincidence', result_map.values())
        result_file.write(("all values amount = %d") % all_values_amount)
        line_chart.render_to_file('coincidence_chart.svg')
        return results, coincidence_holder, result_map


    def test_all():
        key = 0x0
        plain_1 = 0
        plain_2 = "1"
        plain_3 = "2"
        result_file.write(str(plain_1))
        result_file.write(plain_2)
        result_file.write(plain_3)
        cipher = MiniPresent(key)
        encrypted_1 = cipher.encrypt(plain_1)
        encrypted_2 = cipher.encrypt(plain_2)
        encrypted_3 = cipher.encrypt(plain_3)
        enc_1 = encrypted_1
        enc_2 = encrypted_2.encode('hex')
        enc_3 = encrypted_3.encode('hex')
        result_file.write(str(enc_1))
        result_file.write(enc_2)
        result_file.write(enc_3)

        decrypted_1 = cipher.decrypt(encrypted_1)
        decrypted_2 = cipher.decrypt(encrypted_2)
        decrypted_3 = cipher.decrypt(encrypted_3)
        decr_1 = decrypted_1
        decr_2 = decrypted_2.encode('hex')
        decr_3 = decrypted_3.encode('hex')
        result_file.write(decr_1)
        result_file.write(decr_2.decode('hex'))
        result_file.write(decr_3.decode('hex'))

        results = run_for_lab()
{'fraction_non_alphanumeric': 0.09675653482961544, 'fraction_numerical': 0.01464349254139866, 'mean_word_length': 2.4455445544554455, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '2817374', 'n_tokens_mistral': 2354, 'n_tokens_neox': 2248, 'n_words': 448}