Skip to content

Commit 6f6e368

Browse files
authored
Update main.py
1 parent 7fdba6a commit 6f6e368

1 file changed

Lines changed: 60 additions & 63 deletions

File tree

main.py

Lines changed: 60 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,60 @@
1-
import struct
2-
import threading
3-
4-
class HashX:
5-
def __init__(self, seed=0xABCDEF):
6-
"""Initialize hash state with a seed."""
7-
self.seed = seed
8-
self.state = [seed, seed ^ 0x12345678, seed ^ 0x87654321, seed ^ 0xFEDCBA98]
9-
10-
def _mix(self, v):
11-
"""Improved mixing function for better diffusion and security."""
12-
v ^= v >> 13
13-
v *= 0x85EBCA6B
14-
v ^= v >> 16
15-
v *= 0xC2B2AE35
16-
v ^= v >> 16
17-
return v & 0xFFFFFFFFFFFFFFFF # 64-bit mask
18-
19-
def _rotate_left(self, x, r):
20-
"""Bitwise left rotation for fast scrambling."""
21-
return ((x << r) | (x >> (64 - r))) & 0xFFFFFFFFFFFFFFFF
22-
23-
def _process_chunk(self, chunk, index):
24-
"""Multi-threaded processing of input chunks."""
25-
v = struct.unpack('<Q', chunk)[0] # Convert to 64-bit integer
26-
v = self._mix(v)
27-
self.state[index % 4] ^= v
28-
self.state[index % 4] = self._rotate_left(self.state[index % 4], (index * 7) % 64)
29-
30-
def update(self, data):
31-
"""Process input data in 64-byte chunks with multi-threading."""
32-
if isinstance(data, str):
33-
data = data.encode('utf-8') # Convert string to bytes
34-
35-
length = len(data)
36-
padded_data = data + b'\x00' * (64 - (length % 64)) # Padding
37-
blocks = [padded_data[i:i+8] for i in range(0, len(padded_data), 8)]
38-
39-
threads = []
40-
for i, block in enumerate(blocks):
41-
thread = threading.Thread(target=self._process_chunk, args=(block, i))
42-
threads.append(thread)
43-
thread.start()
44-
45-
for thread in threads:
46-
thread.join()
47-
48-
def digest(self):
49-
"""Generate final 256-bit hash."""
50-
final_hash = b''
51-
for i in range(4):
52-
self.state[i] = self._mix(self.state[i] ^ (self.seed + i))
53-
final_hash += struct.pack('<Q', self.state[i]) # Convert back to bytes
54-
return final_hash.hex()
55-
56-
def hash(self, data):
57-
"""Convenience function to compute hash in one call."""
58-
self.update(data)
59-
return self.digest()
60-
61-
# Example usage:
62-
hasher = HashX()
63-
print(hasher.hash("Hello, World!"))
1+
import struct import threading import secrets import multiprocessing import hmac import numpy as np
2+
3+
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
4+
5+
def _mix(self, v):
6+
"""Cryptographic mixing function with avalanche effect."""
7+
v ^= v >> 33
8+
v *= 0xFF51AFD7ED558CCD
9+
v ^= v >> 33
10+
v *= 0xC4CEB9FE1A85EC53
11+
v ^= v >> 33
12+
return v & 0xFFFFFFFFFFFFFFFF
13+
14+
def _rotate_left(self, x, r):
15+
"""Bitwise left rotation for fast scrambling."""
16+
return ((x << r) | (x >> (64 - r))) & 0xFFFFFFFFFFFFFFFF
17+
18+
def _process_chunk(self, chunk, index):
19+
"""Parallel processing of 64-bit chunks."""
20+
v = struct.unpack('<Q', chunk)[0]
21+
v = self._mix(v)
22+
self.state[index % 8] ^= v
23+
self.state[index % 8] = self._rotate_left(self.state[index % 8], (index * 13) % 64)
24+
25+
def update(self, data):
26+
"""Process input data securely in chunks with multiprocessing."""
27+
if isinstance(data, str):
28+
data = data.encode('utf-8')
29+
30+
length = len(data)
31+
padded_data = data + secrets.token_bytes(64 - (length % 64)) # Secure padding
32+
blocks = [padded_data[i:i+8] for i in range(0, len(padded_data), 8)]
33+
34+
pool = multiprocessing.Pool()
35+
pool.starmap(self._process_chunk, [(block, i) for i, block in enumerate(blocks)])
36+
pool.close()
37+
pool.join()
38+
39+
def digest(self):
40+
"""Generate final 512-bit cryptographic hash."""
41+
final_hash = b''
42+
for i in range(8):
43+
self.state[i] = self._mix(self.state[i] ^ (self.seed + i))
44+
final_hash += struct.pack('<Q', self.state[i])
45+
return final_hash.hex()
46+
47+
def hmac_digest(self, data):
48+
"""Compute HMAC-like hash for extra security."""
49+
mac = hmac.new(self.key, data.encode('utf-8') if isinstance(data, str) else data, digestmod=self.digest)
50+
return mac.hexdigest()
51+
52+
def hash(self, data):
53+
"""Convenience function to compute hash securely."""
54+
self.update(data)
55+
return self.digest()
56+
57+
Example usage:
58+
59+
hasher = HashX() print(hasher.hash("Hello, World!")) print(hasher.hmac_digest("Hello, World!"))
60+

0 commit comments

Comments
 (0)