Skip to content

Commit

Permalink
add lint
Browse files Browse the repository at this point in the history
  • Loading branch information
programmingAthlete committed Jan 5, 2024
1 parent 8af8a4a commit efa65bf
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 30 deletions.
40 changes: 26 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,45 @@ PROJECT_NAME = crypto_pkg

SHELL_DOT = $(shell printf "\033[34;1m▶\033[0m")


.DEFAULT_GOAL := help

.PHONY: help
help:
@echo "Available targets:"
@echo "$(SHELL_DOT) help - Display this help message"
@echo "$(SHELL_DOT) setup - Install package"
@echo "$(SHELL_DOT) deps - Install dependencies"
@echo "$(SHELL_DOT) tests - Run tests"
@echo "$(SHELL_DOT) coverage - Run coverage tests"
@echo "$(SHELL_DOT) lint - Run flake8 for linting"

.PHONY: tests
tests: $(info $(SHELL_DOT) testing package...)
tests:
@$(info $(SHELL_DOT) testing package...)
@pip install -e . > /dev/null && pip install pytest > /dev/null
@python -m pytest tests

.PHONY: coverage
coverage: $(info $(SHELL_DOT) coverage testing package...)
coverage:
@$(info $(SHELL_DOT) coverage testing package...)
pip install -e . > /dev/null && pip install pytest pytest-cov > /dev/null
python -m pytest tests --cov=$(PROJECT_NAME) --cov-fail-under=0

.PHONY: deps
deps: $(info $(SHELL_DOT) install required packages...)
deps:
@$(info $(SHELL_DOT) install required packages...)
pip install -r requirements.txt

.PHONY: setup
setup: $(info $(SHELL_DOT) Install packge)
setup:
@$(info $(SHELL_DOT) Install packge)
pip install -e .

.PHONY: build
build: $(info $(SHELL_DOT) build package)
rm -rf dist
python -m build

.PHONY: publishtest
publishtest: $(info $(SHELL_DOT) Publish package to test PyPI)
python -m twine upload --repository testpypi dist/*
.PHONY: lint
lint:
@$(info $(M) coverage testing package...)
pip install -e . > /dev/null && pip install flake8 > /dev/null
flake8 src/$(PROJECT_NAME)

.PHONY: publish
publish: $(info $(SHELL_DOT) Publish package to PyPI)
python -m twine upload dist/*
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[metadata]
name = crypto_pkg

[flake8]
max-line-length = 120

[options]
include_package_data = True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

__version__ = "1.5.1"
__version__ = "1.6.0"

with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
Expand Down
2 changes: 1 addition & 1 deletion src/crypto_pkg/attacks/block_ciphers/double_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def attack(cls, plain_text: str, cipher_text: str, max_key: int = 24, _verbose:
look_up_table = cls.lookup_table_computation(plain_text=plain_text, max_key=max_key)
log.info("Search encryption match in lookup table")
keys = cls.search_match(cipher_text=cipher_text, lookup_table=look_up_table)
log.debug(f"Key Found")
log.debug("Key Found")
return keys


Expand Down
3 changes: 0 additions & 3 deletions src/crypto_pkg/attacks/block_ciphers/modified_aes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import logging
from enum import Enum
from logging import getLogger
from multiprocessing import Pool
import random

Expand Down
2 changes: 1 addition & 1 deletion src/crypto_pkg/attacks/block_ciphers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def prepare_key(key: int, max_key=24) -> Text:

except Exception as exc:
raise exc
return Text(text=k_n)
return Text(text=k_n)
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def compute_c(self, predicted_currents: np.ndarray, byte_position: int, save: bo
np.save(f"matrices/matrix_{byte_position}.npy", np.array(c))
return c


def attack_byte(self, byte_position: int = 0, plot: bool = False,
store: bool = True, re_calculate: bool = False, _verbose: bool = False) -> Tuple[int, np.ndarray]:
"""
Expand Down Expand Up @@ -202,7 +201,7 @@ def attack_full_key(self, show_plot_correlations: bool = False, store_correlatio
log.info(
f"All processes finished. Final output: {results}. Execution time: {tf - ti} seconds -"
f" {(tf - ti) / 60} minutes")
log.debug(f"Constructing the final key from the output")
log.debug("Constructing the final key from the output")
out = [(pos, hex(item)[2:]) for (pos, item) in results]
sorted_list = sorted(out, key=lambda x: x[0])
key_list = [item[1] for item in sorted_list][::-1]
Expand Down
3 changes: 2 additions & 1 deletion src/crypto_pkg/attacks/stream_ciphers/geffe_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ def attack(self, thresholds, _verbose: bool = False):
''' Example '''

# Choose Geffe output
stream = '01001110000011101100011101010111011100000011010001111001101101100000000111110110111011011001010111101100111001111100001111100101110000000010110101001111110110010001111101010110011010010110101011000101'
stream = '01001110000011101100011101010111011100000011010001111001101101100000000111110110111011011001010111101' \
'100111001111100001111100101110000000010110101001111110110010001111101010110011010010110101011000101'
# Geffe tabs
taps = [[0, 1, 4, 7], [0, 1, 7, 11], [0, 2, 3, 5]]
stream_l = [int(item) for item in stream]
Expand Down
7 changes: 4 additions & 3 deletions src/crypto_pkg/clis/attacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def attack_geffe(
settings
"""
# Choose Geffe output
stream = '01001110000011101100011101010111011100000011010001111001101101100000000111110110111011011001010111101100111001111100001111100101110000000010110101001111110110010001111101010110011010010110101011000101'
stream = '0100111000001110110001110101011101110000001101000111100110110110000000011111011011101101100101011110' \
'1100111001111100001111100101110000000010110101001111110110010001111101010110011010010110101011000101'
# Geffe tabs
taps = [[0, 1, 4, 7], [0, 1, 7, 11], [0, 2, 3, 5]]
attack = GeffeAttack(all_taps=taps, stream_ref=stream, f=[1, 1, 0, 1, 0, 0, 0, 1], max_clock=200, n=16)
Expand Down Expand Up @@ -178,8 +179,8 @@ def attack_correlation_power_analysis(
):
"""
Example on how to use the power correlation attack.\n
The filename of the measurement file is required. This file mush be a valid pickle file with at leas 'max_datapoints'
datapoints\n
The filename of the measurement file is required. This file mush be a valid pickle file with at leas
'max_datapoints' datapoints
If a byte position is provided, only the provided key byte will be attacked, otherwise the whole key will be.
"""
with resources.open_binary('crypto_pkg.attacks.power_analysis', 'test_file.pickle') as file:
Expand Down
2 changes: 0 additions & 2 deletions src/crypto_pkg/contracts/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ class PrimeNotGeneratedException(Exception):

class KValueException(Exception):
""" Raised when k is not an even number"""


2 changes: 1 addition & 1 deletion src/crypto_pkg/number_theory/number_theory.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def chinese_reminder_list(cls, a: [int], n: [int]) -> int:
"""
Chinese reminder from a list of integers as and list of modulus ns.
we want to have z such that z = a_i (mod n_i), we hence compute it as
z = \sum_i^{len(a)} ( \prod_{j \neq i} (n_j * m_j * a_i) )
z = sum_i^{len(a)} ( prod_{j neq i} (n_j * m_j * a_i) )
:param a: list of integers as
:param n: list of modulus ns
Expand Down
2 changes: 1 addition & 1 deletion src/crypto_pkg/number_theory/prime_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ def k_bit_prim_number(cls, k, t=100, max_iter=10000):
if response:
return KBitPrimeResponse(status=True, base_10=base_10_n, base_2=k_bit_n)
i += 1
return KBitPrimeResponse(status=False, base_10=None, base_2=[])
return KBitPrimeResponse(status=False, base_10=None, base_2=[])

0 comments on commit efa65bf

Please sign in to comment.