def shorten(self, full_url): """Create short relative URI from full URL. Thou shall have only one node as writer; see README. SIDE-EFFECTS: causes state to be written to durable storage. Returns: tuple containing state, pathname, shortened URI """ status = STATUS_UNKNOWN file_path = None shortened = '' if self.recursive_url_re.match(full_url): status = STATUS_REJECTED while status is STATUS_UNKNOWN: # Using same custom encoding here for compressing path name: #print("shorten: full-url={}".format(full_url)) hashed = base62ish.encode(cityhash.CityHash128(full_url)) dir_path, file_path = self.make_pathname(hashed) full_url_pathname = self.full_url_directory + '/' + file_path phishing_pathname = self.anti_phishing_dir + '/' + file_path if self.match_phishing_url(phishing_pathname, full_url): status = STATUS_PHISHING break elif os.path.exists(full_url_pathname): shortened = self.match_full_url(full_url_pathname, full_url) if shortened != '': status = STATUS_DUPLICATE break # Else, we found hash collision but is a new Full URL # Handle new Full URL: shortened = self.make_short_uri(file_path) self.persist(full_url, shortened, dir_path, file_path) status = STATUS_SHORTENED return (status, file_path, shortened)
def _hash_cityhash(buf): """ Produce a 16-bytes hash of *buf* using CityHash. """ h = cityhash.CityHash128(buf) if sys.version_info >= (3, ): return h.to_bytes(16, 'little') else: return binascii.a2b_hex('%032x' % h)
def _hash_cityhash(buf): """ Produce a 16-bytes hash of *buf* using CityHash. """ h = cityhash.CityHash128(buf) if not PY2: return h.to_bytes(16, "little") else: return binascii.a2b_hex("%032x" % h)
def update_anti_phishing_catalogue(self): """Update and repair artifacts related to known phishing URLs. SIDE-EFFECTS: destructively modifies Full URL files, removes Short URI files corresponding to URLs in known phishing list. Also, populates subdirectory with known phishing URL in list. """ if os.path.exists(self.anti_phishing_file): with open(self.anti_phishing_file, encoding='utf-8') as file: while True: phishing_url = file.readline()[1:-2] # "foo"\n => foo if phishing_url == '': break hashed = base62ish.encode( cityhash.CityHash128(phishing_url)) dir_path, file_path = self.make_pathname(hashed) full_url_pathname = self.full_url_directory + file_path if os.path.exists(full_url_pathname): # Remove pair of Full URL and Short URI from file short_uri, others = self.remove_entry( full_url_pathname, phishing_url) if short_uri: # Else was hash collision but new URL _d_path, f_path = self.make_pathname(short_uri) pathname = self.short_uri_directory + f_path if os.path.exists(pathname): print( "removing short-uri={}".format(short_uri), file=sys.stderr) os.remove(pathname) if len(others) == 0: os.remove(full_url_pathname) else: temp_pathname = full_url_pathname + '.TEMP' with open(temp_pathname, 'w', encoding='utf-8') as temp_file: temp_file.write(''.join(others)) os.rename(temp_pathname, full_url_pathname) pathname = self.anti_phishing_dir_updating + '/' + file_path if (not os.path.exists(pathname) or not self.match_phishing_url( pathname, phishing_url)): os.makedirs(self.anti_phishing_dir_updating + '/' + dir_path, exist_ok=True) with open(pathname, 'a', encoding='utf-8') as phishy_file: phishy_file.write(phishing_url) phishy_file.write("\n")
def __init__(self, ip, port): self.stream = Buffer() self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.s.connect((ip, port)) raw_pk = self.s.recv(33) self.pk = GraphenePublicKey(raw_pk.hex()) sk = PrivateKey() point = self.pk.point() * int.from_bytes(bytes(sk), "big") x: int = point.x() raw_data = x.to_bytes(32, "big") self.shared_secret = sha512(raw_data).digest() key = sha256(self.shared_secret).digest() crc = cityhash.CityHash128(self.shared_secret) data = crc.to_bytes(16, "little") iv = data[8:16] + data[:8] self.s.sendall(bytes.fromhex(repr(sk.pubkey))) self.encryptor = AES.new(key, AES.MODE_CBC, iv) self.test = AES.new(key, AES.MODE_CBC, iv) self.decryptor = AES.new(key, AES.MODE_CBC, iv) self.worker_thread = threading.Thread(target=self.worker) self.worker_thread.start() self.send( 5006, { "user_agent": "Haruka Mock Client", "core_protocol_version": 106, "inbound_address": "0.0.0.0", "inbound_port": 0, "outbound_port": 0, "node_public_key": sk.pubkey, "signed_shared_secret": ecdsa.sign_message(self.shared_secret, str(sk)), "chain_id": bytes.fromhex( "4018d7844c78f6a6c41c6a552b898022310fc5dec06da467ee7905a8dad512c8" ), "user_data": { "platform": String("unknown") } })
def _hash_cityhash(buf): """ Produce a 16-bytes hash of *buf* using CityHash. """ h = cityhash.CityHash128(buf) return h.to_bytes(16, "little")