import random import ctypes import pathlib import hashlib # RBP import time # RBP def bin_to_hex(byte): byte_hex = "" total = 0 for i, bit in enumerate(byte): total += int(bit) * 2 ** i first_place = total // 16 second_place = total - first_place * 16 places = [first_place, second_place] for i, place in enumerate(places): if place < 10: byte_hex += str(place) else: byte_hex += chr(65 + place - 10) return byte_hex.lower() def den_to_bin(number): byte_string = "" result = 2 power = 0 # finds the greatest power of 2 that can fit in the number # this defines the length of the binary number while result > 0: result = number // 2**power if result == 0: break power += 1 for i in range(power-1, -1, -1): bit = number // 2**i number -= bit * 2**i byte_string += str(bit) return byte_string def set_bits(binary, num_bits): for i in range(num_bits - len(binary)): binary += "0" return binary #uuid START def generate(): byte_list = [] # generates 16 8 bit numbers as strings for i in range(16): number = random.randint(0, 255) bits = den_to_bin(number) byte = set_bits(bits , 8) byte_list.append(byte) # setting certain places as pre-defined, as stated by the UUID4 spec (see apendix) byte_list[6] = byte_list[6][:4] + "0010" byte_list[8] = byte_list[8][:6] + "01" # UUIDs are always shown in terms of hex hex_string = "" for byte_index, byte in enumerate(byte_list): byte_hex = bin_to_hex(byte) # adds the dashes in the indexes as required by the UUID4 spec if byte_index in [4, 6, 8, 10]: hex_string += "-" hex_string += byte_hex return hex_string #uuid END #string hash START def hash_string(string): string = string.replace("-", "0") string = string.replace("_", "0") libname = pathlib.Path().absolute() / "modules/algorithms/libcpphash.so" c_lib = ctypes.CDLL(libname) charptr = ctypes.POINTER(ctypes.c_char) c_lib.printc.argtypes = [charptr] c_lib.printc.restypes = int result = c_lib.hash(ctypes.c_char_p(string.encode('utf-8'))) return result def long_hash(string): result = hashlib.sha256(string.encode('utf-8')) result = result.hexdigest() return result # string hash END if __name__ == "__main__": result = hash_string("hello") print(result)