|
|
|
@ -79,25 +79,39 @@ def _get_hash(secret): |
|
|
|
|
return base64.b64encode(hasher.digest()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _find_secret_of_length(target, maximum_distance, length, stop_event): |
|
|
|
|
def _find_secret_of_length(target, ideal_distance, length, stop_event, interesting_hamming_distance=None): |
|
|
|
|
digits = [0] * length |
|
|
|
|
while True: |
|
|
|
|
if stop_event.is_set(): |
|
|
|
|
return hash_name_pb2.HashNameResponse() |
|
|
|
|
# Yield a sentinel and stop the generator if the RPC has been |
|
|
|
|
# cancelled. |
|
|
|
|
yield None |
|
|
|
|
raise StopIteration() |
|
|
|
|
secret = b''.join(struct.pack('B', i) for i in digits) |
|
|
|
|
hash = _get_hash(secret) |
|
|
|
|
distance = _get_substring_hamming_distance(hash, target) |
|
|
|
|
if distance <= maximum_distance: |
|
|
|
|
return hash_name_pb2.HashNameResponse(secret=base64.b64encode(secret), |
|
|
|
|
if interesting_hamming_distance is not None and distance <= interesting_hamming_distance: |
|
|
|
|
# Surface interesting candidates, but don't stop. |
|
|
|
|
yield hash_name_pb2.HashNameResponse(secret=base64.b64encode(secret), |
|
|
|
|
hashed_name=hash, |
|
|
|
|
hamming_distance=distance) |
|
|
|
|
elif distance <= ideal_distance: |
|
|
|
|
# Yield the ideal candidate followed by a sentinel to signal the end |
|
|
|
|
# of the stream. |
|
|
|
|
yield hash_name_pb2.HashNameResponse(secret=base64.b64encode(secret), |
|
|
|
|
hashed_name=hash, |
|
|
|
|
hamming_distance=distance) |
|
|
|
|
yield None |
|
|
|
|
raise StopIteration() |
|
|
|
|
digits[-1] += 1 |
|
|
|
|
i = length - 1 |
|
|
|
|
while digits[i] == _BYTE_MAX + 1: |
|
|
|
|
digits[i] = 0 |
|
|
|
|
i -= 1 |
|
|
|
|
if i == -1: |
|
|
|
|
return None |
|
|
|
|
# Terminate the generator since we've run out of strings of |
|
|
|
|
# `length` bytes. |
|
|
|
|
raise StopIteration() |
|
|
|
|
else: |
|
|
|
|
digits[i] += 1 |
|
|
|
|
|
|
|
|
@ -106,11 +120,15 @@ def _find_secret(target, maximum_distance, stop_event): |
|
|
|
|
length = 1 |
|
|
|
|
while True: |
|
|
|
|
print("Checking strings of length {}.".format(length)) |
|
|
|
|
match = _find_secret_of_length(target, maximum_distance, length, stop_event) |
|
|
|
|
if match is not None: |
|
|
|
|
return match |
|
|
|
|
if stop_event.is_set(): |
|
|
|
|
return hash_name_pb2.HashNameResponse() |
|
|
|
|
for candidate in _find_secret_of_length(target, maximum_distance, length, stop_event): |
|
|
|
|
if candidate is not None: |
|
|
|
|
yield candidate |
|
|
|
|
else: |
|
|
|
|
raise StopIteration() |
|
|
|
|
if stop_event.is_set(): |
|
|
|
|
# Terminate the generator if the RPC has been cancelled. |
|
|
|
|
raise StopIteration() |
|
|
|
|
print("Incrementing length") |
|
|
|
|
length += 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -121,12 +139,28 @@ class HashFinder(hash_name_pb2_grpc.HashFinderServicer): |
|
|
|
|
def on_rpc_done(): |
|
|
|
|
stop_event.set() |
|
|
|
|
context.add_callback(on_rpc_done) |
|
|
|
|
result = _find_secret(request.desired_name, request.maximum_hamming_distance, stop_event) |
|
|
|
|
return result |
|
|
|
|
candidates = list(_find_secret(request.desired_name, request.ideal_hamming_distance, stop_event)) |
|
|
|
|
if not candidates: |
|
|
|
|
return hash_name_pb2.HashNameResponse() |
|
|
|
|
return candidates[-1] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def FindRange(self, request, context): |
|
|
|
|
stop_event = threading.Event() |
|
|
|
|
def on_rpc_done(): |
|
|
|
|
stop_event.set() |
|
|
|
|
context.add_callback(on_rpc_done) |
|
|
|
|
secret_generator = _find_secret(request.desired_name, |
|
|
|
|
request.ideal_hamming_distance, |
|
|
|
|
stop_event, |
|
|
|
|
interesting_hamming_distance=request.interesting_hamming_distance) |
|
|
|
|
for candidate in secret_generator: |
|
|
|
|
yield candidate |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _run_server(port): |
|
|
|
|
server = grpc.server(futures.ThreadPoolExecutor(max_workers=1)) |
|
|
|
|
server = grpc.server(futures.ThreadPoolExecutor(max_workers=1), |
|
|
|
|
maximum_concurrent_rpcs=1) |
|
|
|
|
hash_name_pb2_grpc.add_HashFinderServicer_to_server( |
|
|
|
|
HashFinder(), server) |
|
|
|
|
address = '{}:{}'.format(_SERVER_HOST, port) |
|
|
|
@ -151,6 +185,7 @@ def main(): |
|
|
|
|
args = parser.parse_args() |
|
|
|
|
_run_server(args.port) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
logging.basicConfig() |
|
|
|
|
main() |
|
|
|
|