Commit 4aaeaae9 authored by BO ZHANG's avatar BO ZHANG 🏀
Browse files

add hash module

parent aaac8bca
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)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment