def hash_doc_id_python(doc_id): if isinstance(doc_id, unicode): doc_id = doc_id.encode('utf-8') digest = csiphash.siphash24(ShardAccessor.hash_key, doc_id) hash_long = struct.unpack("<Q", digest)[0] # convert byte string to long # convert 64 bit hash to 32 bit to match Postgres return hash_long & 0xffffffff
def get_vpn_id(vpn: Dict[str, Any]) -> str: """ Calculate RFC2685-compatible VPN ID :param vpn: Dict containing following keys * type - with VPN type ("VRF", "VPLS", "VLL", "EVPN") * vpn_id (optional) - given vpn id * rd (optional) - VRF RD * name (optional) - Local VPN name * rt_export (optional) - List of exported route targets (["xx:yy", ..., "xx:yy"] :return: """ vpn_id = vpn.get("vpn_id") if vpn_id: # Already calculated return vpn_id.lower() # Generate VPN identity fingerprint rt_export = vpn.get("rt_export", []) if rt_export: identity = ":".join(sorted(rt_export)) elif vpn.get("rd"): if vpn["rd"] == "0:0": return "0:0" identity = vpn["rd"] elif vpn.get("name"): identity = vpn["name"] else: raise ValueError("Cannot calculate VPN id") identity = "%s:%s" % (T_MAP.get(vpn["type"], vpn["type"]), identity) # RFC2685 declares VPN ID as <IEEE OUI (3 octets)>:<VPN number (4 octets) # Use reserved OUI range 00 00 00 - 00 00 FF to generate # So we have 5 octets to fill vpn id # Use last 5 octets of siphash 2-4 i_hash = siphash24(SIPHASH_SEED, smart_bytes(identity)) return "%x:%x" % struct.unpack("!BI", i_hash[3:])
def _hash_key(self, key): # NOTE(kgriffs): Be extremely cautious about changing this implementation, # as it will result in invalidating any items in caches, etc. since # they will now have a different key. if not isinstance(key, bytes): key = key.encode('utf-8') time_slot = int(time.time() / self._max_ttl) # NOTE(kgriffs): Be explicit about the endianness in case we ever # deploy to mixed architectures. ts_bytes = struct.pack('<I', time_slot) # NOTE(kgriffs): Hash it to normalize the length; should be more # optimal in terms of memory usage and traversal. Also, Redis # seems to perform slightly better with pre-hashed keys. hash_input = self._namespace + b'\n' + key + b'\n' + ts_bytes # NOTE(kgriffs): This is about 2x as fast as sha256 and results in a # smaller digest. Should be collision-resistant enough for # use in cache keys (TBD). a = xxh64(hash_input).digest() b = siphash24(b'\x00' * 16, hash_input) digest = (a + b) return digest
def __init__(self, size, hash_key=None, store_values=True): if hash_key is None: hash_key = os.urandom(16) self.hash = lambda val: struct.unpack('q', siphash24(hash_key, val))[0] self.k_records = SkipDict() self.size = size self.modifications = 0 self.store_values = store_values
def hash_str(value): """ Calculate integer hash of value :param value: String :return: Hashed string """ return siphash24(SIPHASH_SEED, str(value))
def hash_str(value: Any) -> str: """ Calculate integer hash of value :param value: String :return: Hashed string """ return siphash24(SIPHASH_SEED, smart_bytes(smart_text(value)))
def bi_hash(v): """ Calculate BI hash from given value :param v: :return: """ bh = siphash24(SIPHASH_SEED, str(v)) return int(struct.unpack("!Q", bh)[0] & 0x7fffffffffffffff)
def _siphash(data): if _CSIPHASH_AVAILABLE: hash_bytes = csiphash.siphash24(_SECRET, data) # Equivalent to `int.from_bytes(hash_bytes, sys.byteorder)` in py3, # but py2 compatible: return struct.unpack('<Q', hash_bytes)[0] else: return siphash.SipHash24(_SECRET, data).hash()
def enc_init(ver=0b00, is_mod=False): key = b'\x00' * 16 # fix the key for debugging # key = os.urandom(16) try: # using os.urandom(16) or other methods k1 = bytearray(siphash24(key, str(0))) k2 = bytearray(siphash24(key, str(1))) k3 = bytearray(siphash24(key, str(2))) key_list1 = [key1_table.make_key([gc.KeyTuple('ig_md.cur_ver', ver)])] data_list1 = [ key1_table.make_data([ gc.DataTuple('k1', k1[4:8]), gc.DataTuple('k2', k1[0:4]), gc.DataTuple('otp1', k2), gc.DataTuple('otp2', k3), ], "SwitchIngress.get_key_1") ] key_list2 = [key2_table.make_key([gc.KeyTuple('ig_md.cur_ver', ver)])] data_list2 = [ key2_table.make_data([ gc.DataTuple('k1', k3[4:8]), gc.DataTuple('k2', k3[0:4]), gc.DataTuple('otp1', k2), gc.DataTuple('otp2', k1), ], "SwitchIngress.get_key_2") ] if is_mod: key1_table.entry_mod(target, key_list1, data_list1) key2_table.entry_mod(target, key_list2, data_list2) else: key1_table.entry_add(target, key_list1, data_list1) key2_table.entry_add(target, key_list2, data_list2) except Exception as e: try: flush_table(key1_table, target) flush_table(key2_table, target) except Exception as e: pass
def bi_hash(v): """ Calculate BI hash from given value :param v: :return: """ if not isinstance(v, six.string_types): v = str(v) bh = siphash24(SIPHASH_SEED, smart_bytes(v)) return int(struct.unpack("!Q", bh)[0] & 0x7FFFFFFFFFFFFFFF)
def port_enc_init(ver=0b00, is_mod=False): tables = [otp_table1, otp_table2] key = os.urandom(16) try: for idx in xrange(2): key_list = [] data_list = [] for i in xrange(256): _k = tables[idx].make_key([ gc.KeyTuple('ig_md.r1', i), gc.KeyTuple('ig_md.cur_ver', ver) ]) tmp = siphash24(key, str(i)) bt = bytearray(tmp) if idx == 0: _d = tables[idx].make_data([gc.DataTuple('tmpk', bt[0:2])], "SwitchIngress.port_enc_act") if idx == 1: _d = tables[idx].make_data([gc.DataTuple('tmpk', bt[0:2])], "SwitchIngress.port_dec_act") key_list.append(_k) data_list.append(_d) if is_mod: tables[idx].entry_mod(target, key_list, data_list) else: tables[idx].entry_add(target, key_list, data_list) except Exception as e: try: flush_table(otp_table1, target) flush_table(otp_table2, target) except Exception as e: # print "error", str(e) pass
def compute_short_id(key: bytes, tx_hash_binary: Union[bytearray, memoryview]) -> bytes: return siphash24(key, bytes(tx_hash_binary))[0:6]
def to_user_id(slack_user_id): return siphash24(SIPHASH_KEY, slack_user_id.encode('utf-8')).hex()
def siphash(key, value): '''A really fast hash function which outputs 64 bits. This is returned as an integer''' if len(key) != 16: raise ValueError('Key should be 16 bytes, not {}'.format(len(key))) return little_endian_to_int(siphash24(key, value))
def _siphash(key, value): if len(key) != 16: raise ValueError("Key should be 16 bytes") return little_endian_to_int(siphash24(key, value))
def check_hash(key, message, expected): hex_hash = binascii.hexlify(csiphash.siphash24(binascii.unhexlify(key), binascii.unhexlify(message))) assert_equal(hex_hash, expected)
def hash_doc_id_python(doc_id): digest = csiphash.siphash24(ShardAccessor.hash_key, doc_id) hash_long = struct.unpack("<Q", digest)[0] # convert byte string to long # convert 64 bit hash to 32 bit to match Postgres return hash_long & 0xffffffff