Skip to content

Commit

Permalink
fixed deepsource issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
ernstleierzopf committed Jul 2, 2020
1 parent bd6f676 commit fd5a9b7
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 134 deletions.
7 changes: 6 additions & 1 deletion .deepsource.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ name = "python"
enabled = true

[analyzers.meta]
runtime_version = "3.x.x"
runtime_version = "3.x.x"
max_line_length = 140

[[analyzers]]
name = "test-coverage"
enabled = true
4 changes: 2 additions & 2 deletions cipherImplementations/cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def generate_random_key(self, length):
if not isinstance(length, int):
raise ValueError('Length must be of type integer.')
key = b''
for i in range(length):
for _ in range(length):
char = bytes([self.alphabet[int(random.randrange(0, len(self.alphabet) - 1))]])
key = key + char
return key
Expand All @@ -28,4 +28,4 @@ def filter(self, plaintext, keep_unknown_symbols=False):
plaintext = plaintext.lower()
if not keep_unknown_symbols:
return textUtils.remove_unknown_symbols(plaintext, self.alphabet)
return plaintext
return plaintext
2 changes: 1 addition & 1 deletion cipherImplementations/columnarTransposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ def decrypt(self, ciphertext, key):
continue
plaintext[position] = c
position = position + len(key)
return np.array(plaintext)
return np.array(plaintext)
40 changes: 14 additions & 26 deletions cipherImplementations/hill.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,25 @@ def filter(self, plaintext, keep_unknown_symbols=False):
return plaintext

def determinant(self, matrix):
global alphabet
return int((
matrix[0, 3] * matrix[1, 2] * matrix[2, 1] * matrix[3, 0] - matrix[0, 2] * matrix[1, 3] *
matrix[2, 1] * matrix[3, 0] - \
matrix[0, 3] * matrix[1, 1] * matrix[2, 2] * matrix[3, 0] + matrix[0, 1] * matrix[1, 3] *
matrix[2, 2] * matrix[3, 0] + \
matrix[0, 2] * matrix[1, 1] * matrix[2, 3] * matrix[3, 0] - matrix[0, 1] * matrix[1, 2] *
matrix[2, 3] * matrix[3, 0] - \
matrix[0, 3] * matrix[1, 2] * matrix[2, 0] * matrix[3, 1] + matrix[0, 2] * matrix[1, 3] *
matrix[2, 0] * matrix[3, 1] + \
matrix[0, 3] * matrix[1, 0] * matrix[2, 2] * matrix[3, 1] - matrix[0, 0] * matrix[1, 3] *
matrix[2, 2] * matrix[3, 1] - \
matrix[0, 2] * matrix[1, 0] * matrix[2, 3] * matrix[3, 1] + matrix[0, 0] * matrix[1, 2] *
matrix[2, 3] * matrix[3, 1] + \
matrix[0, 3] * matrix[1, 1] * matrix[2, 0] * matrix[3, 2] - matrix[0, 1] * matrix[1, 3] *
matrix[2, 0] * matrix[3, 2] - \
matrix[0, 3] * matrix[1, 0] * matrix[2, 1] * matrix[3, 2] + matrix[0, 0] * matrix[1, 3] *
matrix[2, 1] * matrix[3, 2] + \
matrix[0, 1] * matrix[1, 0] * matrix[2, 3] * matrix[3, 2] - matrix[0, 0] * matrix[1, 1] *
matrix[2, 3] * matrix[3, 2] - \
matrix[0, 2] * matrix[1, 1] * matrix[2, 0] * matrix[3, 3] + matrix[0, 1] * matrix[1, 2] *
matrix[2, 0] * matrix[3, 3] + \
matrix[0, 2] * matrix[1, 0] * matrix[2, 1] * matrix[3, 3] - matrix[0, 0] * matrix[1, 2] *
matrix[2, 1] * matrix[3, 3] - \
matrix[0, 1] * matrix[1, 0] * matrix[2, 2] * matrix[3, 3] + matrix[0, 0] * matrix[1, 1] *
matrix[2, 2] * matrix[3, 3]) % len(self.alphabet))
matrix[0, 3] * matrix[1, 2] * matrix[2, 1] * matrix[3, 0] - matrix[0, 2] * matrix[1, 3] * matrix[2, 1] * matrix[3, 0] -
matrix[0, 3] * matrix[1, 1] * matrix[2, 2] * matrix[3, 0] + matrix[0, 1] * matrix[1, 3] * matrix[2, 2] * matrix[3, 0] +
matrix[0, 2] * matrix[1, 1] * matrix[2, 3] * matrix[3, 0] - matrix[0, 1] * matrix[1, 2] * matrix[2, 3] * matrix[3, 0] -
matrix[0, 3] * matrix[1, 2] * matrix[2, 0] * matrix[3, 1] + matrix[0, 2] * matrix[1, 3] * matrix[2, 0] * matrix[3, 1] +
matrix[0, 3] * matrix[1, 0] * matrix[2, 2] * matrix[3, 1] - matrix[0, 0] * matrix[1, 3] * matrix[2, 2] * matrix[3, 1] -
matrix[0, 2] * matrix[1, 0] * matrix[2, 3] * matrix[3, 1] + matrix[0, 0] * matrix[1, 2] * matrix[2, 3] * matrix[3, 1] +
matrix[0, 3] * matrix[1, 1] * matrix[2, 0] * matrix[3, 2] - matrix[0, 1] * matrix[1, 3] * matrix[2, 0] * matrix[3, 2] -
matrix[0, 3] * matrix[1, 0] * matrix[2, 1] * matrix[3, 2] + matrix[0, 0] * matrix[1, 3] * matrix[2, 1] * matrix[3, 2] +
matrix[0, 1] * matrix[1, 0] * matrix[2, 3] * matrix[3, 2] - matrix[0, 0] * matrix[1, 1] * matrix[2, 3] * matrix[3, 2] -
matrix[0, 2] * matrix[1, 1] * matrix[2, 0] * matrix[3, 3] + matrix[0, 1] * matrix[1, 2] * matrix[2, 0] * matrix[3, 3] +
matrix[0, 2] * matrix[1, 0] * matrix[2, 1] * matrix[3, 3] - matrix[0, 0] * matrix[1, 2] * matrix[2, 1] * matrix[3, 3] -
matrix[0, 1] * matrix[1, 0] * matrix[2, 2] * matrix[3, 3] + matrix[0, 0] * matrix[1, 1] * matrix[2, 2] * matrix[3, 3]) %
len(self.alphabet))

# compute greatest common divisor using euclid algorithm
def euclid_algo(self, x, y):
if x < y: # We want x >= y
return self.euclid_algo(y, x)
while y != 0:
(x, y) = (y, x % y)
return x
return x
34 changes: 17 additions & 17 deletions cipherImplementations/playfair.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
def get_right_neighbor(index):
if index % 5 < 4:
return index + 1
elif index % 5 == 4:
if index % 5 == 4:
return index - 4
else:
return -1
return -1


def get_lower_neighbour(index):
Expand All @@ -27,10 +26,9 @@ def get_substitute(row, col):
def get_left_neighbor(index):
if index % 5 > 0:
return index - 1
elif index % 5 == 0:
if index % 5 == 0:
return index + 4
else:
return -1
return -1


def get_upper_neighbour(index):
Expand All @@ -50,12 +48,12 @@ def generate_random_key(self, length=10):
raise ValueError('Length must be of type integer.')
original = bytearray(self.alphabet)
key = b''
for i in range(0, length):
for _ in range(0, length):
char = original[random.randrange(0, len(original))]
original = original.replace(bytes([char]), b'')
key = key + bytes([char])

for i in range(0, len(original)):
for _ in range(0, len(original)):
char = original[0]
original = original.replace(bytes([char]), b'')
key = key + bytes([char])
Expand All @@ -67,10 +65,10 @@ def encrypt(self, plaintext, key):
p0, p1 = plaintext[position-1], plaintext[position]
index0 = int(textUtils.num_index_of(key, p0))
index1 = int(textUtils.num_index_of(key, p1))
row_p0 = int(index0 / 5);
row_p1 = int(index1 / 5);
col_p0 = index0 % 5;
col_p1 = index1 % 5;
row_p0 = int(index0 / 5)
row_p1 = int(index1 / 5)
col_p0 = index0 % 5
col_p1 = index1 % 5
if row_p0 == row_p1:
index0 = get_right_neighbor(index0)
index1 = get_right_neighbor(index1)
Expand All @@ -90,10 +88,10 @@ def decrypt(self, ciphertext, key):
c0, c1 = ciphertext[position - 1], ciphertext[position]
index0 = int(textUtils.num_index_of(key, c0))
index1 = int(textUtils.num_index_of(key, c1))
row_p0 = int(index0 / 5);
row_p1 = int(index1 / 5);
col_p0 = index0 % 5;
col_p1 = index1 % 5;
row_p0 = int(index0 / 5)
row_p1 = int(index1 / 5)
col_p0 = index0 % 5
col_p1 = index1 % 5
if row_p0 == row_p1:
index0 = get_left_neighbor(index0)
index1 = get_left_neighbor(index1)
Expand All @@ -111,6 +109,8 @@ def filter(self, plaintext, keep_unknown_symbols=False):
plaintext = plaintext.lower().replace(b'j', b'i')
#plaintext = plaintext.replace(b'x', b'y')
plaintext = super().filter(bytes(plaintext), keep_unknown_symbols)
if len(plaintext) == 0:
return b''
#if len(plaintext) % 2 != 0:
# plaintext = bytes(plaintext) + bytes(b'x')
output = bytearray()
Expand All @@ -123,4 +123,4 @@ def filter(self, plaintext, keep_unknown_symbols=False):
#Unittests anpassen
output.append(plaintext[len(plaintext)-1])
plaintext = bytes(output)
return plaintext
return plaintext
4 changes: 2 additions & 2 deletions cipherImplementations/simpleSubstitution.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self, alphabet, unknown_symbol, unknown_symbol_number):
def generate_random_key(self, length=None):
alphabet2 = b'' + self.alphabet
key = b''
for i in range(len(self.alphabet)):
for _ in range(len(self.alphabet)):
position = int(random.randrange(0, len(alphabet2)))
char = bytes([alphabet2[position]])
key = key + char
Expand Down Expand Up @@ -40,4 +40,4 @@ def decrypt(self, ciphertext, key):
while p < 0:
p = p + len(self.alphabet)
plaintext.append(p)
return np.array(plaintext)
return np.array(plaintext)
2 changes: 1 addition & 1 deletion cipherImplementations/vigenere.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ def decrypt(self, ciphertext, key):
while p < 0:
p = p + len(self.alphabet)
plaintext.append(p)
return np.array(plaintext)
return np.array(plaintext)
6 changes: 3 additions & 3 deletions cipherTypeDetection/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ def predict_single_line(args, model):
parser = argparse.ArgumentParser(
description='CANN Ciphertype Detection Neuronal Network Evaluation Script', formatter_class=argparse.RawTextHelpFormatter)
sp = parser.add_subparsers()
bench_parser = sp.add_parser('benchmark', help='Use this argument to create ciphertexts on the fly, \nlike in '
'training mode, and evaluate them with the model. \nThis option is optimized for large '
'throughput to test the model.')
bench_parser = sp.add_parser('benchmark', help=
'Use this argument to create ciphertexts on the fly, \nlike in training mode, and evaluate them with the '
'model. \nThis option is optimized for large throughput to test the model.')
eval_parser = sp.add_parser('evaluate', help='Use this argument to evaluate cipher types for single files or directories.')
single_line_parser = sp.add_parser('single_line', help='Use this argument to predict a single line of ciphertext.')

Expand Down
24 changes: 10 additions & 14 deletions cipherTypeDetection/textLine2CipherStatisticsDataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import numpy as np


english_frequencies = [
0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015, 0.06094, 0.06966, 0.00153, 0.00772, 0.04025, 0.02406, 0.06749, 0.07507,
0.01929, 0.00095, 0.05987, 0.06327, 0.09056, 0.02758, 0.00978, 0.0236, 0.0015, 0.01974, 0.00074]


def calculate_frequencies(text, size, recursive=True):
before = []
if recursive is True and size > 1:
Expand Down Expand Up @@ -82,9 +87,6 @@ def has_doubles(text):
return 1
return 0

english_frequencies = [0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015, 0.06094, 0.06966, 0.00153,
0.00772, 0.04025, 0.02406, 0.06749, 0.07507, 0.01929, 0.00095, 0.05987, 0.06327, 0.09056, 0.02758, 0.00978,
0.0236, 0.0015, 0.01974, 0.00074]

def calculate_chi_square(frequencies):
chi_square = 0
Expand All @@ -107,12 +109,11 @@ def pattern_repetitions(text):
counter += 1
return counter


def calculate_entropy(text):
'''
calculates shannon's entropy index.
"""calculates shannon's entropy index.
:param text: input numbers-ciphertext
:return: calculated entropy
'''
:return: calculated entropy"""
# https://stackoverflow.com/questions/2979174/how-do-i-compute-the-approximate-entropy-of-a-bit-string
_unique, counts = np.unique(text, return_counts=True)
prob = []
Expand All @@ -124,11 +125,9 @@ def calculate_entropy(text):


def calculate_autocorrelation_average(text):
'''
calculates average of the normalized autocorrelation
"""calculates average of the normalized autocorrelation
:param text: input numbers-ciphertext
:return: autocorrelation average
'''
:return: autocorrelation average"""
# https://stackoverflow.com/questions/14297012/estimate-autocorrelation-using-python
n = len(text)
variance = text.var()
Expand Down Expand Up @@ -252,9 +251,6 @@ def __next__(self):
process = multiprocessing.Process(target=self._worker, args=(d, result_list))
process.start()
processes.append(process)
#self.iteration += self.batch_size
#for process in processes:
# process.join()
return processes, result_list

def _worker(self, data, result):
Expand Down
Loading

0 comments on commit fd5a9b7

Please sign in to comment.