|
| 1 | +import os |
| 2 | +import hashlib |
| 3 | +import hmac |
| 4 | +import base64 |
| 5 | +from argon2 import PasswordHasher, exceptions as argon2_exceptions |
| 6 | +from Crypto.Cipher import AES |
| 7 | +from Crypto.Random import get_random_bytes |
| 8 | +from Crypto.Util.Padding import pad, unpad |
| 9 | +from Crypto.PublicKey import RSA |
| 10 | +from Crypto.Signature import pkcs1_15 |
| 11 | +from Crypto.Hash import SHA256 |
| 12 | + |
| 13 | + |
| 14 | +class Hashium: |
| 15 | + def __init__(self): |
| 16 | + self.ph = PasswordHasher() |
| 17 | + self.aes_key = get_random_bytes(32) # 256-bit AES key |
| 18 | + |
| 19 | + # Basic SHA-256 Hash |
| 20 | + def hashium_basic(self, data: str) -> str: |
| 21 | + return hashlib.sha256(data.encode()).hexdigest() |
| 22 | + |
| 23 | + # Salted SHA-256 Hash |
| 24 | + def hashium_salted(self, data: str) -> str: |
| 25 | + salt = os.urandom(16) |
| 26 | + hash_value = hashlib.sha256(salt + data.encode()).hexdigest() |
| 27 | + return f"{salt.hex()}:{hash_value}" |
| 28 | + |
| 29 | + # PBKDF2-HMAC-SHA256 |
| 30 | + def hashium_pbkdf2(self, password: str) -> str: |
| 31 | + salt = os.urandom(16) |
| 32 | + key = hashlib.pbkdf2_hmac('sha256', password.encode(), salt, 100000) |
| 33 | + return f"{salt.hex()}:{key.hex()}" |
| 34 | + |
| 35 | + def verify_pbkdf2(self, password: str, stored_hash: str) -> bool: |
| 36 | + salt_hex, key_hex = stored_hash.split(":") |
| 37 | + salt = bytes.fromhex(salt_hex) |
| 38 | + expected_key = bytes.fromhex(key_hex) |
| 39 | + test_key = hashlib.pbkdf2_hmac('sha256', password.encode(), salt, 100000) |
| 40 | + return hmac.compare_digest(expected_key, test_key) |
| 41 | + |
| 42 | + # HMAC-SHA256 |
| 43 | + def hashium_hmac(self, key: str, data: str) -> str: |
| 44 | + return hmac.new(key.encode(), data.encode(), hashlib.sha256).hexdigest() |
| 45 | + |
| 46 | + # Argon2 Hashing |
| 47 | + def hashium_argon2(self, password: str) -> str: |
| 48 | + return self.ph.hash(password) |
| 49 | + |
| 50 | + def verify_argon2(self, password: str, stored_hash: str) -> bool: |
| 51 | + try: |
| 52 | + return self.ph.verify(stored_hash, password) |
| 53 | + except argon2_exceptions.VerifyMismatchError: |
| 54 | + return False |
| 55 | + |
| 56 | + # AES-256 Encryption (EAX Mode) |
| 57 | + def encrypt_data(self, data: str) -> str: |
| 58 | + cipher = AES.new(self.aes_key, AES.MODE_EAX) |
| 59 | + ciphertext, tag = cipher.encrypt_and_digest(data.encode()) |
| 60 | + return f"{cipher.nonce.hex()}:{tag.hex()}:{ciphertext.hex()}" |
| 61 | + |
| 62 | + def decrypt_data(self, encrypted_str: str) -> str: |
| 63 | + nonce_hex, tag_hex, ciphertext_hex = encrypted_str.split(":") |
| 64 | + nonce = bytes.fromhex(nonce_hex) |
| 65 | + tag = bytes.fromhex(tag_hex) |
| 66 | + ciphertext = bytes.fromhex(ciphertext_hex) |
| 67 | + cipher = AES.new(self.aes_key, AES.MODE_EAX, nonce=nonce) |
| 68 | + decrypted = cipher.decrypt_and_verify(ciphertext, tag) |
| 69 | + return decrypted.decode() |
| 70 | + |
| 71 | + # File Hashing |
| 72 | + def hash_file(self, file_path: str) -> str: |
| 73 | + sha256 = hashlib.sha256() |
| 74 | + with open(file_path, 'rb') as f: |
| 75 | + while chunk := f.read(8192): |
| 76 | + sha256.update(chunk) |
| 77 | + return sha256.hexdigest() |
| 78 | + |
| 79 | + # RSA Key Generation |
| 80 | + def generate_rsa_keys(self, key_size: int = 2048) -> tuple: |
| 81 | + key = RSA.generate(key_size) |
| 82 | + private_key = key.export_key() |
| 83 | + public_key = key.publickey().export_key() |
| 84 | + return private_key.decode(), public_key.decode() |
| 85 | + |
| 86 | + # Digital Signature |
| 87 | + def sign_data(self, data: str, private_key_str: str) -> str: |
| 88 | + private_key = RSA.import_key(private_key_str.encode()) |
| 89 | + digest = SHA256.new(data.encode()) |
| 90 | + signature = pkcs1_15.new(private_key).sign(digest) |
| 91 | + return base64.b64encode(signature).decode() |
| 92 | + |
| 93 | + def verify_signature(self, data: str, signature_b64: str, public_key_str: str) -> bool: |
| 94 | + public_key = RSA.import_key(public_key_str.encode()) |
| 95 | + digest = SHA256.new(data.encode()) |
| 96 | + signature = base64.b64decode(signature_b64.encode()) |
| 97 | + try: |
| 98 | + pkcs1_15.new(public_key).verify(digest, signature) |
| 99 | + return True |
| 100 | + except (ValueError, TypeError): |
| 101 | + return False |
| 102 | + |
| 103 | + # Ultra-Secure 768-bit+ Custom Hash |
| 104 | + def hashium_ultra_secure(self, data: str, pepper: str = "Hashium2025Ultra") -> str: |
| 105 | + salt = os.urandom(32) |
| 106 | + mixed = salt + data.encode() + pepper.encode() |
| 107 | + pbkdf2_key = hashlib.pbkdf2_hmac('sha512', mixed, salt, 200000, dklen=64) |
| 108 | + hmac_key = hashlib.sha512(pbkdf2_key).digest() |
| 109 | + hmac_result = hmac.new(hmac_key, pbkdf2_key, hashlib.sha512).digest() |
| 110 | + intermediate_hex = hmac_result.hex() |
| 111 | + argon_hash = self.ph.hash(intermediate_hex) |
| 112 | + final_hash = hashlib.sha512((argon_hash + intermediate_hex).encode()).hexdigest() |
| 113 | + return f"{salt.hex()}:{final_hash}" |
| 114 | + |
| 115 | + |
| 116 | +# Example Usage |
| 117 | +if __name__ == "__main__": |
| 118 | + hashium = Hashium() |
| 119 | + |
| 120 | + print("1. Basic Hash:", hashium.hashium_basic("mypassword")) |
| 121 | + |
| 122 | + print("2. Salted Hash:", hashium.hashium_salted("mypassword")) |
| 123 | + |
| 124 | + pbkdf2_hash = hashium.hashium_pbkdf2("mypassword") |
| 125 | + print("3. PBKDF2 Hash:", pbkdf2_hash) |
| 126 | + print(" Verify PBKDF2:", hashium.verify_pbkdf2("mypassword", pbkdf2_hash)) |
| 127 | + |
| 128 | + print("4. HMAC Hash:", hashium.hashium_hmac("mysecretkey", "mypassword")) |
| 129 | + |
| 130 | + argon2_hash = hashium.hashium_argon2("mypassword") |
| 131 | + print("5. Argon2 Hash:", argon2_hash) |
| 132 | + print(" Verify Argon2:", hashium.verify_argon2("mypassword", argon2_hash)) |
| 133 | + |
| 134 | + encrypted = hashium.encrypt_data("mypassword") |
| 135 | + print("6. AES Encrypted:", encrypted) |
| 136 | + print(" AES Decrypted:", hashium.decrypt_data(encrypted)) |
| 137 | + |
| 138 | + print("7. File Hash (demo.txt):", hashium.hash_file("demo.txt") if os.path.exists("demo.txt") else "demo.txt not found") |
| 139 | + |
| 140 | + private_key, public_key = hashium.generate_rsa_keys() |
| 141 | + signature = hashium.sign_data("secure message", private_key) |
| 142 | + print("8. Digital Signature:", signature) |
| 143 | + print(" Verify Signature:", hashium.verify_signature("secure message", signature, public_key)) |
| 144 | + |
| 145 | + print("9. Ultra Secure Hash (768+ bits):", hashium.hashium_ultra_secure("mypassword")) |
0 commit comments