pyflo/pybtc/hash.py
2018-05-28 13:59:35 +04:00

42 lines
921 B
Python

import hashlib
import hmac
from binascii import unhexlify
def sha256(h, hex = False):
if type(h) == str:
h = unhexlify(h)
if hex:
return hashlib.sha256(h).hexdigest()
return hashlib.sha256(h).digest()
def double_sha256(h, hex = False):
if type(h) == str:
h = unhexlify(h)
if hex:
return sha256(sha256(h), 1)
return sha256(sha256(h))
def hmac_sha512(key, data, hex = False):
if hex:
return hmac.new(key, data, hashlib.sha512).hexdigest()
return hmac.new(key, data, hashlib.sha512).digest()
def ripemd160(h, hex = False):
if type(h) == str:
h = unhexlify(h)
a = hashlib.new('ripemd160')
a.update(h)
if hex:
return a.hexdigest()
return a.digest()
def hash160(h, hex = False):
if type(h) == str:
h = unhexlify(h)
if hex:
return ripemd160(sha256(h), 1)
return ripemd160(sha256(h))
#