Python_Hash

Python - 哈希

Python的hashlib包是一个内置库,提供了多种安全的单向数据转换的哈希函数。哈希函数在密码学、数据完整性和其他安全相关应用中起着至关重要的作用。它们接受一个输入(或“消息”)并返回一个固定大小的字节串,通常是一个“摘要”,对于每个唯一的输入都是唯一的。即使输入有很小的变化,输出也会完全不同,这使得从哈希中反向工程原始数据变得困难。

以下是Python hashlib包的概述,没有示例:

导入hashlib:要在Python脚本中使用hashlib包,你需要导入它
支持的算法:hashlib包支持各种哈希算法,如MD5、SHA-1、SHA-224、SHA-256、SHA-384、SHA-512、SHA-3和BLAKE2。这些算法的可用性可能会根据Python构建和OpenSSL库版本而有所不同。
创建哈希对象:要计算哈希值,需要为所需的算法创建一个哈希对象。可以通过调用hashlib模块中的适当函数来实现,如hashlib.sha256()或hashlib.md5()。
使用数据更新哈希对象:哈希对象有一个update()方法,该方法接受你想要哈希的数据作为类似字节的对象(例如,字节、字节数组)。可以多次调用此方法来分块哈希较大的数据。
获取哈希摘要:在用数据更新哈希对象后,可以使用两种方法获得哈希摘要:
hexdigest():将哈希摘要作为十六进制字符串返回。这是哈希值的人类可读表示。
digest():将哈希摘要作为字节对象返回。这是哈希值的更紧凑、二进制表示。
hashlib实用函数:
hashlib.new():允许你通过指定算法名称的字符串(例如,"sha256"或"md5")创建一个新的哈希对象。这在运行时确定算法时很有用。
hashlib.pbkdf2_hmac():实现了PBKDF2(基于密码的密钥派生函数2)算法,该算法通常用于从密码中派生出加密密钥。

注意1:本页面中的所有示例都是用Python 3.x编写的。如果你使用的是Python 2.x,可能无法正常工作。

注意2:本页面中的所有示例都假定是在Windows 7上编写/运行的,除非另有说明。你可能需要在运行其他操作系统时稍微修改一下语法。

关于哈希的定义(什么是哈希?),请参阅IP安全:哈希页面。

MD5算法 - 示例1
SHA512算法 - 示例2

Examples










 

 

< Example 1 >

 

# Import the hashlib library

import hashlib

 

# Create an MD5 hash object with the encoded string 'Hello World'

hashOut = hashlib.md5('Hello World'.encode())

 

# Print the binary representation of the hash digest for 'Hello World'

print("Hash Out for 'Hello World' [Binary] = ", hashOut.digest())

# Print the hexadecimal string representation of the hash digest for 'Hello World'

print("Hash Out for 'Hello World' [String] = ", hashOut.hexdigest())

 

# Create an MD5 hash object with the encoded string 'Hello World!'

hashOut = hashlib.md5('Hello World!'.encode())

 

# Print the binary representation of the hash digest for 'Hello World!'

print("Hash Out for 'Hello World!' [Binary] = ", hashOut.digest())

# Print the hexadecimal string representation of the hash digest for 'Hello World!'

print("Hash Out for 'Hello World!' [String] = ", hashOut.hexdigest())

 

# Create an MD5 hash object with the encoded string 'Hello World Hello World Hello World Hello World Hello World'

hashOut = hashlib.md5('Hello World Hello World Hello World Hello World Hello World'.encode())

 

# Print the binary representation of the hash digest for the long 'Hello World' string

print("Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [Binary] = ",

       hashOut.digest())

# Print the hexadecimal string representation of the hash digest for the long 'Hello World' string

print("Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [String] = ",

       hashOut.hexdigest())

Hash Out for 'Hello World' [Binary] =  b'\xb1\n\x8d\xb1d\xe0uA\x05\xb7\xa9\x9b\xe7.?\xe5'

Hash Out for 'Hello World' [String] =  b10a8db164e0754105b7a99be72e3fe5

 

Hash Out for 'Hello World!' [Binary] =  b'\xed\x07b\x87S.\x866^\x84\x1e\x92\xbf\xc5\r\x8c'

Hash Out for 'Hello World!' [String] =  ed076287532e86365e841e92bfc50d8c

 

Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [Binary]

     =  b'\x99\xa5\xae\xfc\xa17\xbfX\x8eq?BO\x8d\xfb\xfa'

Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [String]

     =  99a5aefca137bf588e713f424f8dfbfa

 

 

< Example 2 >

 

 

# Import the hashlib library

import hashlib

 

# Create an SHA-512 hash object with the encoded string 'Hello World'

hashOut = hashlib.sha512('Hello World'.encode())

 

# Print the binary representation of the hash digest for 'Hello World'

print("Hash Out for 'Hello World' [Binary] = ", hashOut.digest())

# Print the hexadecimal string representation of the hash digest for 'Hello World'

print("Hash Out for 'Hello World' [String] = ", hashOut.hexdigest())

 

# Create an SHA-512 hash object with the encoded string 'Hello World!'

hashOut = hashlib.sha512('Hello World!'.encode())

 

# Print the binary representation of the hash digest for 'Hello World!'

print("Hash Out for 'Hello World!' [Binary] = ", hashOut.digest())

# Print the hexadecimal string representation of the hash digest for 'Hello World!'

print("Hash Out for 'Hello World!' [String] = ", hashOut.hexdigest())

 

# Create an SHA-512 hash object with the encoded string 'Hello World Hello World Hello World Hello World Hello World'

hashOut = hashlib.sha512('Hello World Hello World Hello World Hello World Hello World'.encode())

 

# Print the binary representation of the hash digest for the long 'Hello World' string

print("Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [Binary] = ",

       hashOut.digest())

# Print the hexadecimal string representation of the hash digest for the long 'Hello World' string

print("Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [String] = ",

       hashOut.hexdigest())

Hash Out for 'Hello World' [Binary] =  

b',t\xfd\x17\xed\xaf\xd8\x0e\x84G\xb0\xd4gA\xee$;~\xb7M\xd2\x14\x9a\n\xb1\xb9$o\xb3\x03\x82\xf2~\

   x85=\x85\x85q\x9e\x0eg\xcb\xda\r\xaa\x8fQg\x10da]dZ\xe2z\xcb\x15\xbf\xb1D\x7fE\x9b'

Hash Out for 'Hello World' [String] =  

   2c74fd17edafd80e8447b0d46741ee243b7eb74dd2149a0ab1b9246fb30382f27e853d8585719e0e67cbda0da

  a8f51671064615d645ae27acb15bfb1447f459b

 

Hash Out for 'Hello World!' [Binary] =  

  b'\x86\x18D\xd6pN\x85s\xfe\xc3M\x96~ \xbc\xfe\xf3\xd4$\xcfH\xbe\x04\xe6\xdc\x08\xf2\xbdX\xc7)t3q\x01

  ^\xad\x89\x1c\xc3\xcf\x1c\x9d4\xb4\x92d\xb5\x10u\x1b\x1f\xf9\xe57\x93{\xc4k]o\xf4\xec\xc8'

Hash Out for 'Hello World!' [String] =

 861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b4926

 4b510751b1ff9e537937bc46b5d6ff4ecc8

 

Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [Binary] =  

 b'\xda&\xc0\xad\x86O\xcf|\xa2;Hp\xc2\x92\xba\x97\x95VO\xf9\xd8\xf5b\xaeS\x00\x15[\xb5Wd\xe3\xc0\xbb9

 \x84\x13\x9a\x84}3R\xaeu>\x8d\x0f\xcdlF\xba\xef\x9dw2+\xb2\x15\x812\x83\xf0\x90\xd3'

Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [String] =

 da26c0ad864fcf7ca23b4870c292ba9795564ff9d8f562ae5300155bb55764e3c0bb3984139a847d3352ae753e8d0

  fcd6c46baef9d77322bb215813283f090d3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值