Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hashlib, listcomp, sys, os, pyperclip
- from string import printable as h
- from string import hexdigits as hd
- from string import letters as l
- warn = """Usage: """+os.path.split(sys.argv[0])[1]+""" <function> <content to hash> <algorithm> [copy to clipboard? (boolean, False by default)]
- functions:
- stencode
- stdecode
- stcondense
- stmean
- algorithms:
- md5
- sha1
- sha224
- sha256
- sha384
- sha512
- <content to hash> can be typed with quotes, apostrophes or standalone like this:
- ...<function> 'text' <algorithm>...
- ...<function> "text" <algorithm>...
- ...<function> text <algorithm>...
- that's no quotes or apostrophes.
- """
- try:
- __all1__ = ('stencode','stdecode','stcondense','stmean')
- __all2__ = ('md5','sha1','sha224','sha256','sha384','sha512')
- func = sys.argv[1]
- to_hash = sys.argv[2]
- use_alg = sys.argv[3]
- if func not in __all1__:raise Exception, "Invalid Function"
- if use_alg not in __all2__:raise Exception, "Invalid Algorithm"
- except:
- print warn
- sys.exit(0)
- try:
- copyyn = sys.argv[4]
- except:
- copyyn = False
- try:
- if (to_hash.endswith("'") and to_hash.startswith("'")) == False:
- to_hash = "'"+to_hash+"'"
- elif (to_hash.endswith('"') and to_hash.startswith('"')):
- to_hash = "'"+(to_hash[1:-1])+"'"
- else: pass
- except: pass
- md5 = hashlib.md5
- sha1 = hashlib.sha1
- sha224 = hashlib.sha224
- sha256 = hashlib.sha256
- sha384 = hashlib.sha384
- sha512 = hashlib.sha512
- modes = {
- md5: 32,
- sha1: 40,
- sha224: 56,
- sha256: 64,
- sha384: 96,
- sha512: 128
- }
- def stencode(stuff, alg):
- if alg in modes:
- return ''.join([alg(i).hexdigest() for i in stuff])
- else: raise Exception, "Not an Algorithm"
- def stcondense(stuff, alg):
- """For hashing only! cannot be docoded.. I think..."""
- if alg in modes:
- content = ''.join([alg(i).hexdigest() for i in stuff])
- lettermap = [[i for i in hd].index(o) for o in content]
- (p1, p2) = listcomp.halflist(lettermap)
- summap = [sum(i) for i in zip(p1, p2)]
- return ''.join([l[i] for i in summap])
- else: raise Exception, "Not an Algorithm"
- def stmean(stuff, alg):
- """hashes all characters in string, averages their index value, generates string based on values"""
- if alg in modes:
- parts = []
- content = [alg(i).hexdigest() for i in stuff]
- for i in content:
- parts.append(tuple([hd.index(ab) for ab in i]))
- result = [sum(y) / len(y) for y in zip(*parts)]
- return ''.join([hd[i] for i in result])
- else: raise Exception, "Not an Algorithm"
- def stdecode(stuff, alg):
- if alg in modes:
- stuff = listcomp.chop(stuff, modes[alg])
- completed = []
- for i in stuff:
- for a in h:
- if alg(a).hexdigest() == i:
- completed.append(a)
- return ''.join(completed)
- else: raise Exception, "Not an Algorithm"
- def do(a1, a2, a3):
- exec('print ('+a1+'('+a2+', '+a3+'))')
- if copyyn:
- exec('pyperclip.copy ('+a1+'('+a2+', '+a3+'))')
- try:
- do(func, to_hash, use_alg)
- except:
- print "Woops."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement