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 42 43
| import hashlib import os
def generate_salt(length=16): """生成指定长度的盐值""" return os.urandom(length)
def md5_hash_with_salt(input_string, salt=None): """计算带盐值的MD5哈希""" if salt is None: salt = generate_salt() hasher = hashlib.md5() hasher.update(input_string.encode("utf-8") + salt) hash_value = hasher.hexdigest() return hash_value, salt
print( """ .----------------. .----------------. .-----------------. .----------------. .----------------. .----------------. | .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. | | | _____ _____ | || | __ | || | ____ _____ | || | ________ | || | ____ ____ | || | _______ | | | ||_ _||_ _|| || | / | | || ||_ \|_ _| | || | |_ ___ `. | || | |_ _||_ _| | || | / ___ | | | | | | | /\ | | | || | `| | | || | | \ | | | || | | | `. \ | || | \ \ / / | || | | (__ \_| | | | | | |/ \| | | || | | | | || | | |\ \| | | || | | | | | | || | \ \/ / | || | '.___`-. | | | | | /\ | | || | _| |_ | || | _| |_\ |_ | || | _| |___.' / | || | _| |_ | || | |`\____) | | | | | |__/ \__| | || | |_____| | || ||_____|\____| | || | |________.' | || | |______| | || | |_______.' | | | | | || | | || | | || | | || | | || | | | | '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' | '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' W1ndys的MD5加盐程序,首次发布:https://blog.w1ndys.top/ """ ) input_string = input("请输入要加密的字符串: ") hash_value, salt = md5_hash_with_salt(input_string) print(f"输入字符串: {input_string}") print(f"盐值: {salt.hex()}") print(f"加盐前MD5哈希值: {hashlib.md5(input_string.encode('utf-8')).hexdigest()}") print(f"加盐后MD5哈希值: {hash_value}")
|