SHARE
TWEET

Two's Complement Hash Generation Code

a guest Jun 21st, 2015 6 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def tohex(val, nbits):
  2.   return hex((val + (1 << nbits)) % (1 << nbits))
  3.  
  4. def get_hex(sha):
  5.     d = sha.digest()
  6.     d = [ord(x) for x in d]
  7.  
  8.     #carry flag
  9.     c = True
  10.  
  11.     #test for most significant bit
  12.     n = (d[0] & 0x80) == 0x80
  13.  
  14.     if n:
  15.         x = len(d)
  16.         while x > 0:
  17.             x -= 1
  18.             d[x] = ~d[x]
  19.             if c:
  20.                 c = d[x] == 0xFF
  21.                 d[x] = d[x]+1
  22.    
  23.     d = [tohex(x, 8)[tohex(x, 8).index('x')+1:] for x in d]
  24.  
  25.     d = ''.join(d)
  26.     while d[0] == '0':
  27.         d = d[1:]
  28.     if n:
  29.         d = '-'+d
  30.     return d.lower()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top