Skip to content

Commit

Permalink
Bump dependencies and perform minor linting
Browse files Browse the repository at this point in the history
  • Loading branch information
fionn committed Jun 5, 2024
1 parent 6d98a66 commit d7eedcd
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 34 deletions.
3 changes: 1 addition & 2 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[flake8]
ignore = E302, E305, E741, E116, E114, W503, E265, E124, E126 E127
format = pylint
ignore = E114, E116, E124, E126, E127, E265, E302, E305, E741, W503
max-line-length = 90
statistics = true
9 changes: 4 additions & 5 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ runs:
steps:

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.11"
python-version: "3.12"
cache: pip
cache-dependency-path: requirements*.txt

- name: Install Python dependencies
run: |
pip install --progress-bar off -r requirements.txt
pip install --progress-bar off -r requirements_dev.txt
run: pip install -r requirements.txt -r requirements_dev.txt
shell: bash
env:
PIP_DISABLE_PIP_VERSION_CHECK: 1
PIP_PROGRESS_BAR: "off"
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
steps:

- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install dependendencies
uses: ./.github/actions/setup
Expand All @@ -29,7 +29,7 @@ jobs:
steps:

- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install dependencies
uses: ./.github/actions/setup
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ venv: requirements.txt requirements_dev.txt
packages.svg: $(SRC)
@pyreverse $(SRC) -o svg
@rm classes.svg
@sed -i "s/Times,serif/Inconsolata, DejaVu Sans Mono, monospace/g" $@
@sed -i "s/Times,serif/Inconsolata,monospace/g" $@

tags: $(SRC) $(TEST)
@ctags --languages=python --python-kinds=-i $^
@ctags --languages=python $^

.PHONY: test
test:
Expand Down
1 change: 1 addition & 0 deletions m06.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def hamming_distance(s1: bytes, s2: bytes) -> int:

def find_keysize(cyphertext: bytes) -> int:
bound = float("inf")
keysize = 0
for k in range(2, 41):
normal_distance = 0.0
n_max = int(len(cyphertext) / k) - 2
Expand Down
2 changes: 1 addition & 1 deletion m24.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def main() -> None:
plaintext = prefix + b"A" * 14

if not verify_mt19937_crypt(message=bytes(10), seed=0xffff):
raise RuntimeError("Failed to verify mt19937_crypt")
raise ValueError("Failed to verify mt19937_crypt")

cyphertext = mt19937_crypt(plaintext, seed)
found_seed = crack_mt19937(cyphertext)
Expand Down
1 change: 0 additions & 1 deletion m28.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class SHA1(HashBase):
name = "sha1"

def __init__(self, data: bytes = b"") -> None:
super().__init__(data)
self.h: Register = (0x67452301, 0xefcdab89,
0x98badcfe, 0x10325476, 0xc3d2e1f0)
self._current_register: Register = self.h
Expand Down
1 change: 0 additions & 1 deletion m30.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class MD4(HashBase):
register = (0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476)

def __init__(self, data: bytes = b"") -> None:
super().__init__(data)
self._register: Register = MD4.register
self.data = b""
self._vandercorput = [self._binaryreverse(x) for x in range(16)]
Expand Down
10 changes: 5 additions & 5 deletions m35.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ def _a(self) -> int:
@property
def pubkey(self) -> int:
if self.g is None or self.p is None:
raise Exception
raise RuntimeError
return pow(self.g, self._a, self.p)

def _session_key(self) -> int:
if self._s is None:
if self._peer is None:
raise Exception
raise RuntimeError
self._s = pow(self._peer.pubkey, self._a, self.p)
return self._s

Expand Down Expand Up @@ -113,7 +113,7 @@ def _send(self, data: bytes, peer: Peer) -> None:
def _send_dict(self, data: Mapping[str, object], peer: Peer = None) -> None:
peer = peer or self._peer
if not peer:
raise Exception
raise RuntimeError
data_encoded = json.dumps(data).encode()
self._send(data_encoded, peer)

Expand Down Expand Up @@ -160,7 +160,7 @@ def send_parameters(self, peer: Peer = None, g: int = None) -> None:
g = g or self.g
peer = peer or self._peer
if not peer:
raise Exception
raise RuntimeError
parameters = {"p": self.p, "g": g}
self._send_dict(parameters, peer)
logging.info("%s parameters --> %s@%s:%s",
Expand Down Expand Up @@ -272,7 +272,7 @@ def break_dh(self) -> bytes:
self._s = 0
return self.decrypt()
if self.p is None:
raise Exception
raise RuntimeError
if self.bad_g == self.p - 1:
cyphertext = self._message_buffer.pop()
longest_message = b""
Expand Down
2 changes: 1 addition & 1 deletion m47.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def intervals(n: int, s: int, M: set[range], B: int) -> set[range]:
M_next.add(range(a_prime, b_prime))

if not M_next:
raise Exception("Interval is empty")
raise ValueError("Interval is empty")
return M_next

def attack(oracle: RSAPaddingOracle, c_0: int) -> int:
Expand Down
9 changes: 5 additions & 4 deletions m49.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""CBC-MAC Message Forgery"""

import json
from functools import reduce
from hmac import compare_digest
from typing import TypedDict
Expand Down Expand Up @@ -49,7 +50,7 @@ def _transact(message: bytes) -> dict[str, str]:
@staticmethod
def process(payload: bytes) -> dict[str, str]:
if not ServerV1.validate(payload):
raise Exception("Invalid payload")
raise ValueError("Invalid payload")
message, _, _ = parse_payload_v1(payload)
return ServerV1._transact(message)

Expand Down Expand Up @@ -90,7 +91,7 @@ def validate(payload: bytes) -> bool:
@staticmethod
def process(payload: bytes) -> TransactionV2:
if not ServerV2.validate(payload):
raise Exception("Invalid payload")
raise ValueError("Invalid payload")
message, _ = parse_payload_v2(payload)
return ServerV2._transact(message)

Expand Down Expand Up @@ -198,15 +199,15 @@ def main() -> None:
assert tx["from"] == victim_id
assert tx["to"] == attacker_id
assert tx["amount"] == str(1000000)
print(tx)
print(json.dumps(tx, indent=2))

# v2
forgery = forge_via_length_extension(attacker_id, victim_id)
print(forgery)
txs = ServerV2.process(forgery)
assert {"to": attacker_id, "amount": 1000000} in txs["tx_list"]
assert txs["from"] == victim_id
print(txs)
print(json.dumps(txs, indent=2))

if __name__ == "__main__":
main()
1 change: 0 additions & 1 deletion m52.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class MDHash(HashBase, ABC):
register = bytes(digest_size)

def __init__(self, data: bytes = b"") -> None:
super().__init__(data)
self._h = self.register
self.update(data)

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pycryptodome == 3.16.0
pycryptodome == 3.20.0
7 changes: 4 additions & 3 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pylint == 2.15.8
mypy == 0.991
coverage == 6.5.0
pylint == 3.2.2
mypy == 1.4.0
coverage == 7.5.3
flake8 == 7.0.0
10 changes: 5 additions & 5 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import base64
import hashlib
import unittest
import functools
from unittest import mock
from functools import cache

from Crypto.Hash import MD4
from Crypto.Cipher import AES
Expand Down Expand Up @@ -107,7 +107,7 @@ class Test03(unittest.TestCase):
"""Single-byte XOR cipher"""

def test_xor_everything_sanity(self) -> None:
"""xor_everything on \\x00 produces [\\x00, ..., \\xff]"""
"""xor_everything on 0x00 produces [0x00, ..., 0xff]"""
x = m03.xor_everything(b"\x00")
self.assertEqual(len(x), 256)
for i in range(256):
Expand Down Expand Up @@ -1157,7 +1157,7 @@ class Test43(unittest.TestCase):
"""DSA key recovery from nonce"""

@staticmethod
@functools.cache
@cache
def data() -> dict[str, str]:
"""Load data from file"""
with open("data/43.txt") as data_fd:
Expand Down Expand Up @@ -1701,7 +1701,7 @@ class Test54(unittest.TestCase):
"""Kelsey and Kohno's Nostradamus Attack"""

@staticmethod
@functools.cache
@cache
def build_diamond_structure(k: int) -> m54.Tree:
return m54.build_diamond_structure(k)

Expand Down Expand Up @@ -1735,7 +1735,7 @@ def test_diamond_structure_leaves(self) -> None:
k = 2
tree = self.build_diamond_structure(k)
level_traversed = tree.level_traverse([tree.root])
lowest_level = level_traversed[2 ** k - 1: 2 ** (k + 1) - 1]
lowest_level = level_traversed[2 ** k - 1: 2 ** (k + 1) - 1]
self.assertEqual(len(tree.leaves), 2 ** k)
self.assertEqual(tree.leaves, lowest_level)

Expand Down

0 comments on commit d7eedcd

Please sign in to comment.