Python中的MD5哈希

加密哈希在日常生活中使用,比如数字签名、消息身份验证码、操作检测、指纹、校验和(消息完整性检查)、哈希表、密码存储等等。为了安全起见,它们还用于通过网络发送消息或将消息存储在数据库中。 “中定义了许多哈希函数” hashlib “python中的库。 本文介绍MD5哈希的解释和工作。

null
MD5散列

此哈希函数接受字节序列并返回 128位散列值 ,通常用于检查数据完整性,但存在安全问题。

相关功能:

  • encode(): 将字符串转换为哈希函数可接受的字节。
  • 摘要(): 返回字节格式的编码数据。
  • hexdigest(): 返回十六进制格式的编码数据。

下面的代码演示了MD5哈希接受字节并作为字节输出的工作原理。

# Python 3 code to demonstrate the
# working of MD5 (byte - byte)
import hashlib
# encoding GeeksforGeeks using md5 hash
# function
result = hashlib.md5(b 'GeeksforGeeks' )
# printing the equivalent byte value.
print ( "The byte equivalent of hash is : " , end = "")
print (result.digest())


输出:

The byte equivalent of hash is : b'xf1xe0ix~xcetSx1dx11%Yx94\hq'

说明: 上面的代码以字节为单位,可以被哈希函数接受。md5哈希函数对其进行编码,然后使用digest()打印字节等效的编码字符串。

下面的代码演示了如何将字符串作为输入,并输出编码值的十六进制等效值。

# Python 3 code to demonstrate the
# working of MD5 (string - hexadecimal)
import hashlib
# initializing string
str2hash = "GeeksforGeeks"
# encoding GeeksforGeeks using encode()
# then sending to md5()
result = hashlib.md5(str2hash.encode())
# printing the equivalent hexadecimal value.
print ( "The hexadecimal equivalent of hash is : " , end = "")
print (result.hexdigest())


输出:

The hexadecimal equivalent of hash is : f1e069787ece74531d112559945c6871

说明: 上面的代码获取字符串,并使用encode()将其转换为等效字节,以便哈希函数可以接受它。md5散列函数对其进行编码,然后使用hexdigest()打印十六进制等效编码字符串。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享