Determine if a 32 bit word hashes to itself
This commit is contained in:
parent
cd096b8551
commit
c461e3a390
1 changed files with 41 additions and 0 deletions
41
python/hash-quine.py
Normal file
41
python/hash-quine.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
import hashlib
|
||||
import signal
|
||||
|
||||
def rotate(seq):
|
||||
last_seq = True
|
||||
mod_next = True
|
||||
idx = 0
|
||||
|
||||
while mod_next and idx < len(seq):
|
||||
mod_next = False
|
||||
|
||||
if last_seq:
|
||||
last_seq = seq[idx] == ord("f")
|
||||
|
||||
if seq[idx] == ord("9"):
|
||||
seq[idx] = ord("a")
|
||||
elif seq[idx] == ord("f"):
|
||||
seq[idx] = ord("0")
|
||||
mod_next = True
|
||||
else:
|
||||
seq[idx] += 1
|
||||
|
||||
idx += 1
|
||||
|
||||
return not last_seq
|
||||
|
||||
hash_str = ""
|
||||
show = lambda sig, frame: print("\rcurrently on:", hash_str)
|
||||
hash_bytes = [ord("0") for _ in range(64)]
|
||||
should_rotate = True
|
||||
|
||||
signal.signal(signal.SIGTSTP, show)
|
||||
|
||||
while should_rotate:
|
||||
hash_str = "".join([chr(byte) for byte in hash_bytes[::-1]])
|
||||
sha_hash = hashlib.sha256(hash_str.encode("utf-8")).hexdigest()
|
||||
|
||||
if hash_str == sha_hash:
|
||||
print(hash_str)
|
||||
|
||||
should_rotate = rotate(hash_bytes)
|
Loading…
Add table
Reference in a new issue