>>> print(hashlib.new("md5",b"this is a test.").hexdigest()) 09cba091df696af91549de27b8e7d0f6 >>> print(hashlib.new("sha1",b"this is a test.").hexdigest()) 7728f8eb7bf75ec3cc49364861eec852fc814870 >>> print(hashlib.new("sha256",b"this is a test.").hexdigest()) aaae6f4e850e064ee0cbce6ac8fc6cab0a17f0ce112aaedba122fbc782d8251b >>> print(hashlib.new("sha512",b"this is a test.").hexdigest()) 640ada87d87eb01725b8b6293598c47193b891f13f9bc6b845fbd347f7f7b2e2df14f3698df00e6a8a6187a5d8373bdad824b8a10f0da80642ab32c338985e26
这样写也可以:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
>>> test_str="this is a test" >>> type(test_str) <class'str'> >>> bytes = test_str.encode("utf-8") >>> type(bytes) <class'bytes'> >>> bytes = "this is a test.".encode("utf-8") >>> print(hashlib.new("md5", bytes).hexdigest()) 09cba091df696af91549de27b8e7d0f6 >>> print(hashlib.new("sha1", bytes).hexdigest()) 7728f8eb7bf75ec3cc49364861eec852fc814870 >>> print(hashlib.new("sha256", bytes).hexdigest()) aaae6f4e850e064ee0cbce6ac8fc6cab0a17f0ce112aaedba122fbc782d8251b >>> print(hashlib.new("sha512", bytes).hexdigest()) 640ada87d87eb01725b8b6293598c47193b891f13f9bc6b845fbd347f7f7b2e2df14f3698df00e6a8a6187a5d8373bdad824b8a10f0da80642ab32c338985e26