python 实现Hash函数

1. 根据uid和日期生成统一id。

# 12345678_20211112
def hash(uid_date):
    seed = 131
    hash = 0
    for i in uid_date:
        hash = hash * seed + ord(i)
    hash = hash & 0x7FFFFFFF
    return (hash % 5000)

2. 使用sha256进行hash

from datetime import datetime
from hashlib import sha256, sha512
import hmac

def hash(data, key="1547836219980"):
    # data = "testappSecret"
    cts = datetime.now().__str__()
    data = data + cts
    data = data.encode('utf-8')
    key = key.encode('utf-8')
    result = hmac.new(key, data, digestmod=sha256).hexdigest().upper()
    return result

你可能感兴趣的:(python,哈希算法,python,散列表)