Advertisement
Peaser

??

Oct 4th, 2015
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. δ=0xffffffff
  2. λ=ord
  3. μ=lambda x,k:(((x)<<(k))|((x)>>(32-(k))))
  4. def m_(a,b,c):
  5.     a&=δ;b&=δ;c&=δ
  6.     a-=c;a&=δ;a^=μ(c,4); a&=δ;c+=b;c&=δ
  7.     b-=a;b&=δ;b^=μ(a,6); b&=δ;a+=c;a&=δ
  8.     c-=b;c&=δ;c^=μ(b,8); c&=δ;b+=a;b&=δ
  9.     a-=c;a&=δ;a^=μ(c,16);a&=δ;c+=b;c&=δ
  10.     b-=a;b&=δ;b^=μ(a,19);b&=δ;a+=c;a&=δ
  11.     c-=b;c&=δ;c^=μ(b,4); c&=δ;b+=a;b&=δ
  12.     return a,b,c
  13. def f_(a,b,c):
  14.     a&=δ;b&=δ;c&=δ
  15.     c^=b;c&=δ;c-=μ(b,14);c&=δ
  16.     a^=c;a&=δ;a-=μ(c,11);a&=δ
  17.     b^=a;b&=δ;b-=μ(a,25);b&=δ
  18.     c^=b;c&=δ;c-=μ(b,16);c&=δ
  19.     a^=c;a&=δ;a-=μ(c,4);a&=δ
  20.     b^=a;b&=δ;b-=μ(a,14);b&=δ
  21.     c^=b;c&=δ;c-=μ(b,24);c&=δ
  22.     return a,b,c
  23. def h2_(ζ,iv=0,iv2=0):
  24.     _l=σ=len(ζ)
  25.     a=b=c=(0xdeadbeef+(_l)+iv)
  26.     c+=initval2;c&=δ
  27.     p=0
  28.     while σ > 12:
  29.         a+=(λ(ζ[p+0])+(λ(ζ[p+1])<<8)+(λ(ζ[p+2])<<16)+(λ(ζ[p+3])<<24));a&=δ
  30.         b+=(λ(ζ[p+4])+(λ(ζ[p+5])<<8)+(λ(ζ[p+6])<<16)+(λ(ζ[p+7])<<24));b&=δ
  31.         c+=(λ(ζ[p+8])+(λ(ζ[p+9])<<8)+(λ(ζ[p+10])<<16)+(λ(ζ[p+11])<<24));c&=δ
  32.         a,b,c=m_(a,b,c)
  33.         p+=12
  34.         σ-=12
  35.     if σ==12:c+=(λ(ζ[p+8])+(λ(ζ[p+9])<<8)+(λ(ζ[p+10])<<16)+(λ(ζ[p+11])<<24));b+=(λ(ζ[p+4])+(λ(ζ[p+5])<<8)+(λ(ζ[p+6])<<16)+(λ(ζ[p+7])<<24));a+=(λ(ζ[p+0])+(λ(ζ[p+1])<<8)+(λ(ζ[p+2])<<16)+(λ(ζ[p+3])<<24));
  36.     if σ==11:c+=(λ(ζ[p+8])+(λ(ζ[p+9])<<8)+(λ(ζ[p+10])<<16));b+=(λ(ζ[p+4])+(λ(ζ[p+5])<<8)+(λ(ζ[p+6])<<16)+(λ(ζ[p+7])<<24));a+=(λ(ζ[p+0])+(λ(ζ[p+1])<<8)+(λ(ζ[p+2])<<16)+(λ(ζ[p+3])<<24));
  37.     if σ==10:c+=(λ(ζ[p+8])+(λ(ζ[p+9])<<8));b+=(λ(ζ[p+4])+(λ(ζ[p+5])<<8)+(λ(ζ[p+6])<<16)+(λ(ζ[p+7])<<24));a+=(λ(ζ[p+0])+(λ(ζ[p+1])<<8)+(λ(ζ[p+2])<<16)+(λ(ζ[p+3])<<24));
  38.     if σ==9:c+=(λ(ζ[p+8]));b+=(λ(ζ[p+4])+(λ(ζ[p+5])<<8)+(λ(ζ[p+6])<<16)+(λ(ζ[p+7])<<24));a+=(λ(ζ[p+0])+(λ(ζ[p+1])<<8)+(λ(ζ[p+2])<<16)+(λ(ζ[p+3])<<24));
  39.     if σ==8:b+=(λ(ζ[p+4])+(λ(ζ[p+5])<<8)+(λ(ζ[p+6])<<16)+(λ(ζ[p+7])<<24));a+=(λ(ζ[p+0])+(λ(ζ[p+1])<<8)+(λ(ζ[p+2])<<16)+(λ(ζ[p+3])<<24));
  40.     if σ==7:b+=(λ(ζ[p+4])+(λ(ζ[p+5])<<8)+(λ(ζ[p+6])<<16));a+=(λ(ζ[p+0])+(λ(ζ[p+1])<<8)+(λ(ζ[p+2])<<16)+(λ(ζ[p+3])<<24));
  41.     if σ==6:b+=((λ(ζ[p+5])<<8)(ζ[p+4]));a+=(λ(ζ[p+0])+(λ(ζ[p+1])<<8)+(λ(ζ[p+2])<<16)+(λ(ζ[p+3])<<24))
  42.     if σ==5:b+=(λ(ζ[p+4]));a+=(λ(ζ[p+0])+(λ(ζ[p+1])<<8)+(λ(ζ[p+2])<<16)+(λ(ζ[p+3])<<24));
  43.     if σ==4:a+=(λ(ζ[p+0])+(λ(ζ[p+1])<<8)+(λ(ζ[p+2])<<16)+(λ(ζ[p+3])<<24))
  44.     if σ==3:a+=(λ(ζ[p+0])+(λ(ζ[p+1])<<8)+(λ(ζ[p+2])<<16))
  45.     if σ==2:a+=(λ(ζ[p+0])+(λ(ζ[p+1])<<8))
  46.     if σ==1:a+=λ(ζ[p+0])
  47.     a&=δ;b&=δ;c&=δ
  48.     if σ==0:return c,b
  49.     a,b,c=f_(a,b,c)
  50.     return c,b
  51. def h_(data,initval=0):
  52.     c,b=h2_(data,initval,0)
  53.     return c
  54.  
  55.  
  56.    
  57. if __name__=="__main__":
  58.     import sys
  59.     hashstr ='Four score and seven years ago'
  60.     hashstr2='Four score und seven years ago'
  61.  
  62.     hash,hash2=h2_(hashstr,0xdeadbeef,0xdeadbeef)
  63.     print('"%s": %x %x' % (hashstr,hash,hash2))
  64.  
  65.     hash=h_(hashstr,0)
  66.     print('"%s": %x' % (hashstr,hash))
  67.  
  68.     hash,hash2=h2_(hashstr2,0xdeadbeef,0xdeadbeef)
  69.     print('"%s": %x %x' % (hashstr2,hash,hash2))
  70.  
  71.     hash=h_(hashstr2,0)
  72.     print('"%s": %x' % (hashstr2,hash))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement