Skip to content

Instantly share code, notes, and snippets.

@jcsalterego
Created July 18, 2009 05:42
Show Gist options
  • Select an option

  • Save jcsalterego/149433 to your computer and use it in GitHub Desktop.

Select an option

Save jcsalterego/149433 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import hashlib
import sys
def hamming(a, b):
diffs = 0
for c1, c2 in zip(a, b):
if c1 != c2:
diffmask = ord(c1) ^ ord(c2)
while diffmask != 0:
diffs += diffmask & 1
diffmask >>= 1
return diffs
def main(argv):
if len(argv) < 2:
return 1
m = hashlib.sha1("I am not a big believer in fortune telling")
target = m.digest()
magic = " ".join(argv[1:])
m = hashlib.sha1(magic)
h = hamming(target, m.digest())
print("%d\t%s\t%s" % (h, m.hexdigest(), magic))
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment