From 13183a0902be2eb185e32d76d355208b8607bf4b Mon Sep 17 00:00:00 2001 From: Farazul Hoda Date: Sat, 21 Oct 2023 23:37:52 -0500 Subject: [PATCH 1/5] message-digesting5 hashing algorithm --- ciphers/md5.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 ciphers/md5.py diff --git a/ciphers/md5.py b/ciphers/md5.py new file mode 100644 index 000000000000..76878e0e074a --- /dev/null +++ b/ciphers/md5.py @@ -0,0 +1,31 @@ +# Author: farazulhoda + +# A Python program that calculates the MD5 (Message Digest 5) hash of a given string using the hashlib library. +# While MD5 is considered weak and not suitable for cryptographic purposes, it can still be used for non-security-related purposes +# like checksums or simple data integrity checks. + + +#By importing the hashlib library, which provides various hashing algorithms, including MD5. +import hashlib + +# Function to calculate the MD5 hash of a given input string. +def calculate_md5_hash(input_string): + # Create an MD5 hash object using hashlib. + md5_hash = hashlib.md5() + + # Update the hash object with the input string, encoded as bytes. + md5_hash.update(input_string.encode('utf-8')) + + # Get the hexadecimal representation of the MD5 hash. + md5_hash_hex = md5_hash.hexdigest() + + # Return the MD5 hash as a hexadecimal string. + return md5_hash_hex + +# Input string provided by the user. +input_string = input("Enter a string: ") + +# Calculate and print the MD5 hash. +md5_hash = calculate_md5_hash(input_string) +print(f"Input String: {input_string}") +print(f"MD5 Hash: {md5_hash}") \ No newline at end of file From beb2d5c0db35fb75dfb45c1f7b8f08fe2ce6a56c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 22 Oct 2023 04:43:34 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ciphers/md5.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ciphers/md5.py b/ciphers/md5.py index 76878e0e074a..1bb24cf71b2b 100644 --- a/ciphers/md5.py +++ b/ciphers/md5.py @@ -1,20 +1,21 @@ # Author: farazulhoda # A Python program that calculates the MD5 (Message Digest 5) hash of a given string using the hashlib library. -# While MD5 is considered weak and not suitable for cryptographic purposes, it can still be used for non-security-related purposes +# While MD5 is considered weak and not suitable for cryptographic purposes, it can still be used for non-security-related purposes # like checksums or simple data integrity checks. -#By importing the hashlib library, which provides various hashing algorithms, including MD5. +# By importing the hashlib library, which provides various hashing algorithms, including MD5. import hashlib + # Function to calculate the MD5 hash of a given input string. def calculate_md5_hash(input_string): # Create an MD5 hash object using hashlib. md5_hash = hashlib.md5() # Update the hash object with the input string, encoded as bytes. - md5_hash.update(input_string.encode('utf-8')) + md5_hash.update(input_string.encode("utf-8")) # Get the hexadecimal representation of the MD5 hash. md5_hash_hex = md5_hash.hexdigest() @@ -22,10 +23,11 @@ def calculate_md5_hash(input_string): # Return the MD5 hash as a hexadecimal string. return md5_hash_hex + # Input string provided by the user. input_string = input("Enter a string: ") # Calculate and print the MD5 hash. md5_hash = calculate_md5_hash(input_string) print(f"Input String: {input_string}") -print(f"MD5 Hash: {md5_hash}") \ No newline at end of file +print(f"MD5 Hash: {md5_hash}") From 2e05db8ae81e1bad907cc54c52bede893801d2da Mon Sep 17 00:00:00 2001 From: Farazul Hoda Date: Wed, 2 Oct 2024 23:31:36 -0400 Subject: [PATCH 3/5] Enhance Onepad encryption/decryption with input type verification and Unicode range checks --- ciphers/caesar_cipher.py | 4 +--- ciphers/onepad_cipher.py | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/ciphers/caesar_cipher.py b/ciphers/caesar_cipher.py index d19b9a337221..ba31dca7f1d9 100644 --- a/ciphers/caesar_cipher.py +++ b/ciphers/caesar_cipher.py @@ -188,9 +188,7 @@ def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str >>> brute_force("jFyuMy xIH'N vLONy zILwy Gy!")[20] "Please don't brute force me!" - >>> brute_force(1) - Traceback (most recent call last): - TypeError: 'int' object is not iterable + >>> brute_force("sample text") # the function expects a string as the first argument """ # Set default alphabet to lower and upper case english chars alpha = alphabet or ascii_letters diff --git a/ciphers/onepad_cipher.py b/ciphers/onepad_cipher.py index c4fb22e14a06..a798a313dec3 100644 --- a/ciphers/onepad_cipher.py +++ b/ciphers/onepad_cipher.py @@ -9,7 +9,9 @@ def encrypt(text: str) -> tuple[list[int], list[int]]: >>> Onepad().encrypt("") ([], []) >>> Onepad().encrypt([]) - ([], []) + Traceback (most recent call last): + ... + TypeError: Input must be a string >>> random.seed(1) >>> Onepad().encrypt(" ") ([6969], [69]) @@ -19,12 +21,27 @@ def encrypt(text: str) -> tuple[list[int], list[int]]: >>> Onepad().encrypt(1) Traceback (most recent call last): ... - TypeError: 'int' object is not iterable + TypeError: Input must be a string >>> Onepad().encrypt(1.1) Traceback (most recent call last): ... - TypeError: 'float' object is not iterable + TypeError: Input must be a string """ + # Original code for encrypting the text + # plain = [ord(i) for i in text] + # key = [] + # cipher = [] + # for i in plain: + # k = random.randint(1, 300) + # c = (i + k) * k + # cipher.append(c) + # key.append(k) + # return cipher, key + + # New code: Ensure input is a string + if not isinstance(text, str): # Ensure input is a string + raise TypeError("Input must be a string") + plain = [ord(i) for i in text] key = [] cipher = [] @@ -51,9 +68,23 @@ def decrypt(cipher: list[int], key: list[int]) -> str: >>> Onepad().decrypt([9729, 114756, 4653, 31309, 10492], [69, 292, 33, 131, 61]) 'Hello' """ + # Original code for decrypting the text + # plain = [] + # for i in range(len(key)): + # p = int((cipher[i] - (key[i]) ** 2) / key[i]) + # plain.append(chr(p)) + # return "".join(plain) + + # New code: Ensure lengths of cipher and key match + if len(cipher) != len(key): # Check if lengths match + raise ValueError("Cipher and key must have the same length") + plain = [] for i in range(len(key)): p = int((cipher[i] - (key[i]) ** 2) / key[i]) + # Check for valid Unicode range + if p < 0 or p > 1114111: # Check for valid Unicode range + raise ValueError("Decrypted value out of range for valid characters") plain.append(chr(p)) return "".join(plain) @@ -61,4 +92,4 @@ def decrypt(cipher: list[int], key: list[int]) -> str: if __name__ == "__main__": c, k = Onepad().encrypt("Hello") print(c, k) - print(Onepad().decrypt(c, k)) + print(Onepad().decrypt(c, k)) \ No newline at end of file From 1f6ef3319950457891fd1e866018b8e6aebe190f Mon Sep 17 00:00:00 2001 From: Farazul Hoda Date: Wed, 2 Oct 2024 23:37:44 -0400 Subject: [PATCH 4/5] REVERSED the changes for Python/ciphers/caesar_cipher.py --- ciphers/caesar_cipher.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ciphers/caesar_cipher.py b/ciphers/caesar_cipher.py index ba31dca7f1d9..d19b9a337221 100644 --- a/ciphers/caesar_cipher.py +++ b/ciphers/caesar_cipher.py @@ -188,7 +188,9 @@ def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str >>> brute_force("jFyuMy xIH'N vLONy zILwy Gy!")[20] "Please don't brute force me!" - >>> brute_force("sample text") # the function expects a string as the first argument + >>> brute_force(1) + Traceback (most recent call last): + TypeError: 'int' object is not iterable """ # Set default alphabet to lower and upper case english chars alpha = alphabet or ascii_letters From ebc5792a90d1616695f3ca4f6937b7da12dd00b7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 03:46:51 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ciphers/onepad_cipher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ciphers/onepad_cipher.py b/ciphers/onepad_cipher.py index a798a313dec3..8de204e1a168 100644 --- a/ciphers/onepad_cipher.py +++ b/ciphers/onepad_cipher.py @@ -92,4 +92,4 @@ def decrypt(cipher: list[int], key: list[int]) -> str: if __name__ == "__main__": c, k = Onepad().encrypt("Hello") print(c, k) - print(Onepad().decrypt(c, k)) \ No newline at end of file + print(Onepad().decrypt(c, k))