-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
60 lines (47 loc) · 2.44 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import struct import threading import secrets import multiprocessing import hmac import numpy as np
class HashX: def init(self, seed=None, key=None): """Initialize hash state with a secure seed and optional key for HMAC-like security.""" if seed is None: seed = secrets.randbits(64) # Strong random seed for better security self.seed = seed self.state = [ seed, seed ^ 0xA3B1C6D8E5F7A9C1, seed ^ 0x1B3A5C7D9E0F2416, seed ^ 0xF123456789ABCDEF, seed ^ 0x1234567890ABCDEF, seed ^ 0xFEDCBA9876543210, seed ^ 0x0F1E2D3C4B5A6978, seed ^ 0x9A8B7C6D5E4F3210 ] self.key = key or secrets.token_bytes(16) # Optional secret key for keyed hashing
def _mix(self, v):
"""Cryptographic mixing function with avalanche effect."""
v ^= v >> 33
v *= 0xFF51AFD7ED558CCD
v ^= v >> 33
v *= 0xC4CEB9FE1A85EC53
v ^= v >> 33
return v & 0xFFFFFFFFFFFFFFFF
def _rotate_left(self, x, r):
"""Bitwise left rotation for fast scrambling."""
return ((x << r) | (x >> (64 - r))) & 0xFFFFFFFFFFFFFFFF
def _process_chunk(self, chunk, index):
"""Parallel processing of 64-bit chunks."""
v = struct.unpack('<Q', chunk)[0]
v = self._mix(v)
self.state[index % 8] ^= v
self.state[index % 8] = self._rotate_left(self.state[index % 8], (index * 13) % 64)
def update(self, data):
"""Process input data securely in chunks with multiprocessing."""
if isinstance(data, str):
data = data.encode('utf-8')
length = len(data)
padded_data = data + secrets.token_bytes(64 - (length % 64)) # Secure padding
blocks = [padded_data[i:i+8] for i in range(0, len(padded_data), 8)]
pool = multiprocessing.Pool()
pool.starmap(self._process_chunk, [(block, i) for i, block in enumerate(blocks)])
pool.close()
pool.join()
def digest(self):
"""Generate final 512-bit cryptographic hash."""
final_hash = b''
for i in range(8):
self.state[i] = self._mix(self.state[i] ^ (self.seed + i))
final_hash += struct.pack('<Q', self.state[i])
return final_hash.hex()
def hmac_digest(self, data):
"""Compute HMAC-like hash for extra security."""
mac = hmac.new(self.key, data.encode('utf-8') if isinstance(data, str) else data, digestmod=self.digest)
return mac.hexdigest()
def hash(self, data):
"""Convenience function to compute hash securely."""
self.update(data)
return self.digest()
Example usage:
hasher = HashX() print(hasher.hash("Hello, World!")) print(hasher.hmac_digest("Hello, World!"))