md5.hexdigest() 字符串加密

hexdigest()函数用来给字符串加密,并返回一个新的加密后的字符串。

用处一般是为文件名加密。

from hashlib import md5

str_1 = "password or file_name or anything"
str_2 = str_1.encode()
cipher_str = md5(str_2).hexdigest()
print(cipher_str)

会得到如下结果:

e19540841be5868b02280418c41aa33a

实际应用场景大概如下:

from hashlib import md5

name_str = "config_1"
name_str = name_str.encode("utf-8")
file_name = md5(name_str).hexdigest()
file_path = "d:/"
file_path = file_path + file_name + ".conf"
with open(file_path,"w",encoding="UTF-8") as f:
    f.write("balbal")

会得到如下文件:

posted @ 2024-01-30 14:31  精神状态不常见  阅读(606)  评论(0编辑  收藏  举报