Created
July 18, 2009 05:42
-
-
Save jcsalterego/149433 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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