Advertisement
Peaser

phash

Jul 23rd, 2014
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. import hashlib, listcomp
  2.  
  3. from string import printable as h
  4. from string import hexdigits as hd
  5. from string import letters as l
  6.  
  7. md5 = hashlib.md5
  8. sha1 = hashlib.sha1
  9. sha224 = hashlib.sha224
  10. sha256 = hashlib.sha256
  11. sha384 = hashlib.sha384
  12. sha512 = hashlib.sha512
  13.  
  14. modes = {
  15. md5: 32,
  16. sha1: 40,
  17. sha224: 56,
  18. sha256: 64,
  19. sha384: 96,
  20. sha512: 128
  21. }
  22.  
  23. def stencode(stuff, alg):
  24.     if alg in modes:
  25.         return ''.join([alg(i).hexdigest() for i in stuff])
  26.     else: raise Exception, "Not an Algorithm"
  27.  
  28. def stcondense(stuff, alg):
  29.     """For hashing only! cannot be docoded.. I think..."""
  30.     if alg in modes:
  31.         content = ''.join([alg(i).hexdigest() for i in stuff])
  32.         lettermap = [[i for i in hd].index(o) for o in content]
  33.         (p1, p2) = listcomp.halflist(lettermap)
  34.         summap = [sum(i) for i in zip(p1, p2)]
  35.         return ''.join([l[i] for i in summap])
  36.     else: raise Exception, "Not an Algorithm"
  37.  
  38. def stmean(stuff, alg):
  39.     """hashes all characters in string, averages their index value, generates string based on values"""
  40.     if alg in modes:
  41.         parts = []
  42.         content = [alg(i).hexdigest() for i in stuff]
  43.         for i in content:
  44.             parts.append(tuple([hd.index(ab) for ab in i]))
  45.         result = [sum(y) / len(y) for y in zip(*parts)]
  46.         return ''.join([hd[i] for i in result])
  47.     else: raise Exception, "Not an Algorithm"
  48.  
  49. def stdecode(stuff, alg):
  50.     if alg in modes:
  51.         stuff = listcomp.chop(stuff, modes[alg])
  52.         completed = []
  53.         for i in stuff:
  54.             for a in h:
  55.                 if alg(a).hexdigest() == i:
  56.                     completed.append(a)
  57.         return ''.join(completed)
  58.     else: raise Exception, "Not an Algorithm"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement