From 037472fc0729f941894880b46697576f3a2c8459 Mon Sep 17 00:00:00 2001 From: Joshua Flores Date: Sat, 26 Apr 2025 00:00:45 -0400 Subject: [PATCH] first two challenges solved --- data/02.txt | 2 ++ data/03.txt | 1 + setup.cfg | 24 ++++++++++++++++++++++++ src/cryptopals/cryptlib.py | 14 ++++++++++++++ src/cryptopals/cryptopals.py | 9 --------- tests/01.py | 12 +++++++++--- tests/02.py | 17 +++++++++++++++++ tests/template.py | 10 ++++++++++ 8 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 data/02.txt create mode 100644 data/03.txt create mode 100644 setup.cfg create mode 100644 src/cryptopals/cryptlib.py delete mode 100644 src/cryptopals/cryptopals.py create mode 100644 tests/02.py diff --git a/data/02.txt b/data/02.txt new file mode 100644 index 0000000..3e2d695 --- /dev/null +++ b/data/02.txt @@ -0,0 +1,2 @@ +1c0111001f010100061a024b53535009181c +686974207468652062756c6c277320657965 diff --git a/data/03.txt b/data/03.txt new file mode 100644 index 0000000..38b54db --- /dev/null +++ b/data/03.txt @@ -0,0 +1 @@ +1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..d802289 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,24 @@ +[metadata] +name = cryptopals +version = 0.1 +author = Joshua Flores +author_email = josh@heavyguys.net +description = Cryptopals Solutions +long_description = file: README.md, LICENSE.txt +long_description_content_type = text/markdown +url = https://git.heavyguys.net/Josh/cryptopals +project_urls = + repository = https://git.heavyguys.net/Josh/cryptopals.git +classifiers = + Programming Language :: Python :: 3 + License :: OSI Approved :: MIT License + Operating System :: OS Independent + +[options] +package_dir = + = src +packages = find: +python_requires = >=3.9 + +[options.packages.find] +where = src diff --git a/src/cryptopals/cryptlib.py b/src/cryptopals/cryptlib.py new file mode 100644 index 0000000..318821f --- /dev/null +++ b/src/cryptopals/cryptlib.py @@ -0,0 +1,14 @@ +import base64 + + +# this function takes a string representing hex, converts to bytes, and re-encodes to base64 +def hex_to_base64(hex_string): + byte_array = bytearray.fromhex(hex_string) + base64_value = base64.b64encode(byte_array) + return base64_value + +# this function takes two hex strings, converts them to bytes, and xor's them +def byte_xor(bs1, bs2): + byte_array1 = int(bs1,16) + byte_array2 = int(bs2,16) + return byte_array1 ^ byte_array2 diff --git a/src/cryptopals/cryptopals.py b/src/cryptopals/cryptopals.py deleted file mode 100644 index 6c0dd6c..0000000 --- a/src/cryptopals/cryptopals.py +++ /dev/null @@ -1,9 +0,0 @@ -import base64 - - -# this function takes a string representing hex, converts to bytes, and re-encodes to base64 -def hex_to_base64(hex_string) - byte_array = bytearray.fromhex(hex_string) - base64_value = base64.b64encode(byte_array) - return base64_value - diff --git a/tests/01.py b/tests/01.py index 5e5a74b..cd15344 100644 --- a/tests/01.py +++ b/tests/01.py @@ -1,10 +1,16 @@ #!/usr/bin/env python -from cryptopals import helper -from cryptopals import cryptopals +import os, sys + +module_dir = os.path.relpath('../src/cryptopals') +sys.path.insert(0, module_dir) + +import helper, cryptlib def main(): - pass + hex_string = helper.read_file("../data/01.txt") + solution = cryptlib.hex_to_base64(hex_string) + print(solution.decode()) if __name__ == '__main__': diff --git a/tests/02.py b/tests/02.py new file mode 100644 index 0000000..e34c999 --- /dev/null +++ b/tests/02.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python + +import os, sys + +module_dir = os.path.relpath('../src/cryptopals') +sys.path.insert(0, module_dir) + +import helper, cryptlib + +def main(): + content = helper.read_file("../data/02.txt") + hexstring_list = content.splitlines() + solution = cryptlib.byte_xor(hexstring_list[0], hexstring_list[1]) + print(hex(solution)[2:]) + +if __name__ == '__main__': + main() diff --git a/tests/template.py b/tests/template.py index 9369e2e..1b9b8ae 100644 --- a/tests/template.py +++ b/tests/template.py @@ -1,5 +1,15 @@ +#!/usr/bin/env python + +import os, sys + +module_dir = os.path.relpath('../src/cryptopals') +sys.path.insert(0, module_dir) + +import helper, cryptlib + def main(): pass + if __name__ == '__main__': main()