Skip to content

Add Error Handling for Onepad Encryption/Decryption #11691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions ciphers/md5.py
Original file line number Diff line number Diff line change
@@ -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.

Check failure on line 3 in ciphers/md5.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

ciphers/md5.py:3:89: E501 Line too long (111 > 88)

Check failure on line 3 in ciphers/md5.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

ciphers/md5.py:3:89: E501 Line too long (111 > 88)
# While MD5 is considered weak and not suitable for cryptographic purposes, it can still be used for non-security-related purposes

Check failure on line 4 in ciphers/md5.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

ciphers/md5.py:4:89: E501 Line too long (130 > 88)

Check failure on line 4 in ciphers/md5.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

ciphers/md5.py:4:89: E501 Line too long (130 > 88)
# like checksums or simple data integrity checks.


# By importing the hashlib library, which provides various hashing algorithms, including MD5.

Check failure on line 8 in ciphers/md5.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

ciphers/md5.py:8:89: E501 Line too long (93 > 88)

Check failure on line 8 in ciphers/md5.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

ciphers/md5.py:8:89: E501 Line too long (93 > 88)
import hashlib


# Function to calculate the MD5 hash of a given input string.
def calculate_md5_hash(input_string):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: calculate_md5_hash. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file ciphers/md5.py, please provide doctest for the function calculate_md5_hash

Please provide type hint for the parameter: input_string

# Create an MD5 hash object using hashlib.
md5_hash = hashlib.md5()

Check failure on line 15 in ciphers/md5.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (S324)

ciphers/md5.py:15:16: S324 Probable use of insecure hash functions in `hashlib`: `md5`

Check failure on line 15 in ciphers/md5.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (S324)

ciphers/md5.py:15:16: S324 Probable use of insecure hash functions in `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}")
37 changes: 34 additions & 3 deletions ciphers/onepad_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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 = []
Expand All @@ -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)

Expand Down
Loading