diff --git a/ciphers/md5.py b/ciphers/md5.py new file mode 100644 index 000000000000..1bb24cf71b2b --- /dev/null +++ b/ciphers/md5.py @@ -0,0 +1,33 @@ +# 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}") diff --git a/ciphers/onepad_cipher.py b/ciphers/onepad_cipher.py index c4fb22e14a06..8de204e1a168 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)