上QQ阅读APP看书,第一时间看更新
Hashing example using an SHA-256 algorithm
The following example script uses the SHA-256 hashing algorithm to compute a digest of the message:
from Crypto.Hash import SHA256 hash_object = SHA256.new(data=b'First') print(hash_object.hexdigest()) hash_object.update(b'd') print(hash_object.hexdigest())
Let's consider the output of the preceding script and make a few observations:
a151ceb1711aad529a7704248f03333990022ebbfa07a7f04c004d70c167919f 18902d9ed3b47effdb6faf90ea69b2ef08ef3d25c60a13454ccaef7e60d1cfe1
As we can see, both the hash values in the output have 64 hexadecimal digits (256 bits) irrespective of the size of the message. The first hash value has a message "First," and the second one has "Firstd" (the update function appends the new message to the previous one). Although there is a difference of one character at the end, the entire SHA-256 hash value looks completely different. This property of SHA-256 makes sure that it is pre-image resistant, and thus very difficult to break.