hash.py 1.13 KB
Newer Older
BO ZHANG's avatar
BO ZHANG committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import hashlib
import random
import string
from astropy.time import Time


def generate_sha1_from_time(verbose=False):
    """
    根据当前时间生成 SHA-1 哈希值,并添加随机字符串确保唯一性

    Returns
    -------
    Tuple:
        (时间戳, 随机字符串, SHA-1哈希值) 元组
    """
    # 获取当前毫秒级时间戳(ISO格式)
    timestamp = Time.now().isot
    # 生成40个随机字母和数字
    random_str = "".join(random.choices(string.ascii_letters + string.digits, k=40))
    # 将时间戳和随机字符串组合
    combined_str = f"{timestamp}_{random_str}"

    # 生成 SHA-1 哈希
    sha1 = hashlib.sha1()
    sha1.update(combined_str.encode("utf-8"))
    sha_value = sha1.hexdigest()

    if verbose:
        return timestamp, random_str, sha_value
    else:
        return sha_value


if __name__ == "__main__":
    # 生成并输出结果
    for i in range(3):
        timestamp, random_str, sha = generate_sha1_from_time(verbose=True)
        print("当前时间: ", timestamp)
        print("随机字符串: ", random_str)
        print("SHA-1 哈希: ", sha)
        print("-" * 50)