def hmac_hash(value, salt, digest_alg='md5'): if isinstance(digest_alg,str) and digest_alg.startswith('pbkdf2'): iterations, keylen, alg = digest_alg[7:-1].split(',') return pbkdf2_hex(value, salt, int(iterations), int(keylen),get_digest(alg)) digest_alg = get_digest(digest_alg) d = hmac.new(salt,value,digest_alg) return d.hexdigest()
def simple_hash(text, key="", salt="", digest_alg="md5"): """ Generates hash with the given text using the specified digest hashing algorithm """ if not digest_alg: raise RuntimeError("simple_hash with digest_alg=None") elif not isinstance(digest_alg, str): # manual approach h = digest_alg(text + key + salt) elif digest_alg.startswith("pbkdf2"): # latest and coolest! iterations, keylen, alg = digest_alg[7:-1].split(",") return pbkdf2_hex(text, salt, int(iterations), int(keylen), get_digest(alg)) elif key: # use hmac digest_alg = get_digest(digest_alg) h = hmac.new(key + salt, text, digest_alg) else: # compatible with third party systems h = hashlib.new(digest_alg) h.update(text + salt) return h.hexdigest()
def simple_hash(text, key='', salt='', digest_alg='md5'): """ Generates hash with the given text using the specified digest hashing algorithm """ if not digest_alg: raise RuntimeError("simple_hash with digest_alg=None") elif not isinstance(digest_alg, str): # manual approach h = digest_alg(text + key + salt) elif digest_alg.startswith('pbkdf2'): # latest and coolest! iterations, keylen, alg = digest_alg[7:-1].split(',') return pbkdf2_hex(text, salt, int(iterations), int(keylen), get_digest(alg)) elif key: # use hmac digest_alg = get_digest(digest_alg) h = hmac.new(key + salt, text, digest_alg) else: # compatible with third party systems h = hashlib.new(digest_alg) h.update(text + salt) return h.hexdigest()