蟒蛇中的沙

SHA(安全哈希算法)是一组由语言定义的加密哈希函数,用于各种应用程序,如密码安全等。Python在 hashlib “图书馆。可以使用hashlib的“算法保证”函数找到这些。

null

# Python 3 code to check
# available algorithms
import hashlib
# prints all available algorithms
print ( "The available algorithms are : " , end = "")
print (hashlib.algorithms_guaranteed)


输出:

The available algorithms are : {'sha256', 'sha384', 'sha224', 'sha512', 'sha1', 'md5'}

接下来,让我们首先讨论本文将要使用的函数。

相关功能:

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

下面解释不同的SHA哈希函数。

  • SHA256: 此哈希函数属于哈希类SHA-2,其内部块大小为32位。
  • SHA384: 此哈希函数属于哈希类SHA-2,其内部块大小为32位。这是一个被截断的版本。
  • SHA224: 此哈希函数属于哈希类SHA-2,其内部块大小为32位。这是一个被截断的版本。
  • SHA512: 此哈希函数属于哈希类SHA-2,其内部块大小为64位。
  • SHA1: 160位的散列函数在工作中类似于MD5散列,由于存在安全漏洞而被停止使用。

下面的代码实现了这些散列函数。

# Python 3 code to demonstrate
# SHA hash algorithms.
import hashlib
# initializing string
str = "GeeksforGeeks"
# encoding GeeksforGeeks using encode()
# then sending to SHA256()
result = hashlib.sha256( str .encode())
# printing the equivalent hexadecimal value.
print ( "The hexadecimal equivalent of SHA256 is : " )
print (result.hexdigest())
print ( "
"
)
# initializing string
str = "GeeksforGeeks"
# encoding GeeksforGeeks using encode()
# then sending to SHA384()
result = hashlib.sha384( str .encode())
# printing the equivalent hexadecimal value.
print ( "The hexadecimal equivalent of SHA384 is : " )
print (result.hexdigest())
print ( "
"
)
# initializing string
str = "GeeksforGeeks"
# encoding GeeksforGeeks using encode()
# then sending to SHA224()
result = hashlib.sha224( str .encode())
# printing the equivalent hexadecimal value.
print ( "The hexadecimal equivalent of SHA224 is : " )
print (result.hexdigest())
print ( "
"
)
# initializing string
str = "GeeksforGeeks"
# encoding GeeksforGeeks using encode()
# then sending to SHA512()
result = hashlib.sha512( str .encode())
# printing the equivalent hexadecimal value.
print ( "The hexadecimal equivalent of SHA512 is : " )
print (result.hexdigest())
print ( "
"
)
# initializing string
str = "GeeksforGeeks"
# encoding GeeksforGeeks using encode()
# then sending to SHA1()
result = hashlib.sha1( str .encode())
# printing the equivalent hexadecimal value.
print ( "The hexadecimal equivalent of SHA1 is : " )
print (result.hexdigest())


输出:

The hexadecimal equivalent of SHA256 is : 
f6071725e7ddeb434fb6b32b8ec4a2b14dd7db0d785347b2fb48f9975126178f

The hexadecimal equivalent of SHA384 is : 
d1e67b8819b009ec7929933b6fc1928dd64b5df31bcde6381b9d3f90488d253240490460c0a5a1a873da8236c12ef9b3

The hexadecimal equivalent of SHA224 is : 
173994f309f727ca939bb185086cd7b36e66141c9e52ba0bdcfd145d

The hexadecimal equivalent of SHA512 is : 
0d8fb9370a5bf7b892be4865cdf8b658a82209624e33ed71cae353b0df254a75db63d1baa35ad99f26f1b399c31f3c666a7fc67ecef3bdcdb7d60e8ada90b722

The hexadecimal equivalent of SHA1 is : 
4175a37afd561152fb60c305d4fa6026b7e79856

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

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