Advertisement
Peaser

FIXED

Jul 24th, 2014
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. import hashlib, listcomp, sys, os, pyperclip
  2. from string import printable as h
  3. from string import hexdigits as hd
  4. from string import letters as l
  5.  
  6. warn = """Usage: """+os.path.split(sys.argv[0])[1]+""" <function> <content to hash> <algorithm> [copy to clipboard? (boolean, False by default)]
  7.  
  8.    functions:
  9.        stencode
  10.        stdecode
  11.        stcondense
  12.        stmean
  13.  
  14.    algorithms:
  15.        md5
  16.        sha1
  17.        sha224
  18.        sha256
  19.        sha384
  20.        sha512
  21.  
  22.    <content to hash> can be typed with quotes, apostrophes or standalone like this:
  23.  
  24.        ...<function> 'text' <algorithm>...
  25.        ...<function> "text" <algorithm>...
  26.        ...<function> text <algorithm>...
  27.  
  28.    that's no quotes or apostrophes.
  29.  
  30.  
  31.    """
  32.  
  33. try:
  34.     __all1__ = ('stencode','stdecode','stcondense','stmean')
  35.     __all2__ = ('md5','sha1','sha224','sha256','sha384','sha512')
  36.     func = sys.argv[1]
  37.     to_hash = sys.argv[2]
  38.     use_alg = sys.argv[3]
  39.  
  40.     if func not in __all1__:raise Exception, "Invalid Function"
  41.     if use_alg not in __all2__:raise Exception, "Invalid Algorithm"
  42.  
  43. except:
  44.     print warn
  45.     sys.exit(0)
  46. try:
  47.     copyyn = sys.argv[4]
  48. except:
  49.     copyyn = False
  50.  
  51. try:
  52.     if (to_hash.endswith("'") and to_hash.startswith("'")) == False:
  53.         to_hash = "'"+to_hash+"'"
  54.     elif (to_hash.endswith('"') and to_hash.startswith('"')):
  55.         to_hash = "'"+(to_hash[1:-1])+"'"
  56.     else: pass
  57. except: pass
  58.  
  59. md5 = hashlib.md5
  60. sha1 = hashlib.sha1
  61. sha224 = hashlib.sha224
  62. sha256 = hashlib.sha256
  63. sha384 = hashlib.sha384
  64. sha512 = hashlib.sha512
  65.  
  66. modes = {
  67. md5: 32,
  68. sha1: 40,
  69. sha224: 56,
  70. sha256: 64,
  71. sha384: 96,
  72. sha512: 128
  73. }
  74.  
  75. def stencode(stuff, alg):
  76.     if alg in modes:
  77.         return ''.join([alg(i).hexdigest() for i in stuff])
  78.     else: raise Exception, "Not an Algorithm"
  79.  
  80. def stcondense(stuff, alg):
  81.     """For hashing only! cannot be docoded.. I think..."""
  82.     if alg in modes:
  83.         content = ''.join([alg(i).hexdigest() for i in stuff])
  84.         lettermap = [[i for i in hd].index(o) for o in content]
  85.         (p1, p2) = listcomp.halflist(lettermap)
  86.         summap = [sum(i) for i in zip(p1, p2)]
  87.         return ''.join([l[i] for i in summap])
  88.     else: raise Exception, "Not an Algorithm"
  89.  
  90. def stmean(stuff, alg):
  91.     """hashes all characters in string, averages their index value, generates string based on values"""
  92.     if alg in modes:
  93.         parts = []
  94.         content = [alg(i).hexdigest() for i in stuff]
  95.         for i in content:
  96.             parts.append(tuple([hd.index(ab) for ab in i]))
  97.         result = [sum(y) / len(y) for y in zip(*parts)]
  98.         return ''.join([hd[i] for i in result])
  99.     else: raise Exception, "Not an Algorithm"
  100.  
  101. def stdecode(stuff, alg):
  102.     if alg in modes:
  103.         stuff = listcomp.chop(stuff, modes[alg])
  104.         completed = []
  105.         for i in stuff:
  106.             for a in h:
  107.                 if alg(a).hexdigest() == i:
  108.                     completed.append(a)
  109.         return ''.join(completed)
  110.     else: raise Exception, "Not an Algorithm"
  111.  
  112. def do(a1, a2, a3):
  113.     exec('print ('+a1+'('+a2+', '+a3+'))')
  114.     if copyyn:
  115.         exec('pyperclip.copy ('+a1+'('+a2+', '+a3+'))')
  116. try:
  117.     do(func, to_hash, use_alg)
  118. except:
  119.     print "Woops."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement