Last active
November 27, 2015 07:46
-
-
Save BlacklightShining/571cb4b5b61e2efcc554 to your computer and use it in GitHub Desktop.
This file contains 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/env python3 | |
# eytlus in Freenode/#python wrote this. | |
# I decided to keep my own copies of it around. | |
# I've edited this. See the revision history for diffs and originals. | |
from __future__ import print_function, division | |
def eytlus_hash(text, a=0, b=0): | |
CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*($" | |
for char in text: | |
try: | |
char_index = CHARS.index(char) | |
except ValueError: | |
char_index = len(CHARS) | |
for __ in range(16): | |
a = a * -4 + b + 0x7494 - char_index | |
b = b // 5 + a + 0x85A8 - char_index | |
return a, b | |
if __name__ == '__main__': | |
import argparse | |
import sys | |
argument_parser = argparse.ArgumentParser() | |
argument_parser.add_argument('-s', '--string', action='append', dest='strings', help="Print the hash of the given string") | |
argument_parser.add_argument('--block-size', default=512, type=int, help="Read this many bytes from each file at a time") | |
argument_parser.add_argument('files', nargs='*', type=argparse.FileType(mode='rb')) | |
args = argument_parser.parse_args() | |
if (not args.files) and (not args.strings): | |
args.files = [sys.stdin] | |
for file in args.files: | |
a, b = 0, 0 | |
if file == sys.stdin: | |
file = sys.stdin.buffer | |
while True: | |
block = file.read(args.block_size) | |
if not block: | |
break | |
block = ''.join(chr(byte) for byte in block) | |
a, b = eytlus_hash(block, a, b) | |
print("Eytlus(file({filename!r})) == ({a:#08x}, {b:#08x})".format(filename=file.name, a=a, b=b)) | |
for string in args.strings: | |
a, b = eytlus_hash(string) | |
print("Eytlus({string!r}) == ({a:#08x}, {b:#08x})".format(string=string, a=a, b=b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment