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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| import os import numpy as np import galois from crc import Calculator, Crc64 from Crypto.Util.number import long_to_bytes, bytes_to_long
def generate_file_for_hash(target_hash: str, output_file_path: str): def calculate_crc64(data: bytes) -> int: calculator = Calculator(Crc64.CRC64, optimized=True) return calculator.checksum(data)
def construct_linear_system(m_bits, n_bits): assert m_bits % 8 == 0, "Message size must be a multiple of 8 bits." GF2 = galois.GF(2)
crc_of_zeros = calculate_crc64(b"\x00" * (m_bits // 8)) c_list = [int(bit) for bit in bin(crc_of_zeros)[2:].zfill(n_bits)] C = GF2(c_list)
matrix_rows = [] for i in range(m_bits): input_int = 1 << (m_bits - 1 - i) input_bytes = long_to_bytes(input_int, m_bits // 8) crc_val = calculate_crc64(input_bytes) v_list = [int(bit) for bit in bin(crc_val)[2:].zfill(n_bits)] row = GF2(v_list) + C matrix_rows.append(row) A = GF2(np.array(matrix_rows)) return A, C
m_bits = 64 n_bits = 64
print("Constructing the linear system. This may take a moment...") A, C = construct_linear_system(m_bits, n_bits) print("System constructed.")
GF2 = galois.GF(2) target_hash_int = int(target_hash, 16) hash_list = [int(bit) for bit in bin(target_hash_int)[2:].zfill(n_bits)] hash_bin_vector = GF2(hash_list)
print("Solving the system of equations...") b = hash_bin_vector + C A_T = A.T b_T = b.T.reshape(-1, 1)
try: solution_v_T = np.linalg.solve(A_T, b_T) solution_v = solution_v_T.T.flatten() print("Solution found.")
solution_int = int("".join(map(str, solution_v)), 2) with open(output_file_path, "wb") as f: f.write(long_to_bytes(solution_int, m_bits // 8)) print(f"\nSuccessfully generated file '{output_file_path}'.") with open(output_file_path, "rb") as f: content = f.read() final_crc = calculate_crc64(content) print(f"Verification CRC: {final_crc:016x}") print(f"Target CRC: {target_hash}") assert f"{final_crc:016x}" == target_hash.lower() print("Verification successful!")
except np.linalg.LinAlgError: print("Could not find a unique solution for the given hash.")
TARGET_HASH = "ed807cd407bbadc4" OUTPUT_FILE = "fake.png" generate_file_for_hash(TARGET_HASH, OUTPUT_FILE)
|