# here is a vibe coded a hash function with each step broken down and printed in binary

# this is the basis of cryptographic hashing functions

# note how each step is deterministic operation, no single step can make the process secure:

# --> we cannot identify a single thing that makes cryptographic hashing secure

# security is based on assumption that random looking probabilities in output (avalanche effect) makes the hash secure

####

import sys

def print_hash(prefix, hash_val):

text = bin(hash_val)[2:].zfill(16)

print(prefix, ' '.join([text[i:i+8] for i in range(0, len(text), 8)]) + "\n")

def minimal_crypto_hash(data, key=int("1001100110111", 2)):

if isinstance(data, str):

data = data.encode('utf-8')

# Initialize with key

hash_val = key & int("1111111111111111", 2)

print_hash("hash_val k", hash_val)

# Process each byte with nonlinear operations

for byte in data:

print("byte ", "0" * 8, bin(byte)[2:].zfill(8))

print_hash("hash_val s", hash_val)

print("# XOR with input byte")

hash_val ^= byte

print_hash("hash_val a", hash_val)

print("# Nonlinear transformation - multiplication with odd constant 1100101100011")

hash_val = hash_val * int("1100101100011", 2)

print_hash("hash_val b", hash_val)

print("bitwise and 1111111111111111")

hash_val = hash_val & int("1111111111111111", 2)

print_hash("hash_val g", hash_val)

print("# Bit rotation for better mixing, bitwise OR")

hash_val = (hash_val >> 3) | (hash_val << 13)

print_hash("hash_val c", hash_val)

print("# bitwise AND")

hash_val = hash_val & int("1111111111111111", 2)

print_hash("hash_val c", hash_val)

print("# Another nonlinear step, shift hash_val >> 7")

hash_val ^= (hash_val >> 7)

print_hash("hash_val d", hash_val)

print("# multiply by 1010101111001101")

hash_val = hash_val * int("1010101111001101", 2)

print_hash("hash_val e", hash_val)

print("# bitwise and 1111111111111111")

hash_val = hash_val & int("1111111111111111", 2)

print_hash("hash_val h", hash_val)

return hash_val & int("1111111111111111", 2)

final_hash = minimal_crypto_hash(sys.argv[1])

print_hash("hash_val f", final_hash)

print("final_hash", bin(final_hash)[2:], hex(final_hash))

Reply to this note

Please Login to reply.

Discussion

No replies yet.