Reply to comment

Implementation of SHA 1, 256, 384, and 512

As a continuation of the previous entry about the implementation of SHA-1, I've now implemented the last three algorithms as well. These are SHA-256, SHA-384, and SHA-512. SHA-1 provided a 160-bit digest, SHA-256 provides a 256-bit digest, SHA-384 provides a 384-bit digest, and, believe it or not, SHA-512 provides a 512-bit digest. These algorithms are described in FIPS 180-2.

The usage is the same as for SHA-1, except for a little detail of specifying the exact algorithm to use:

>>> import sha
>>> data = sha.str2bin("All your base are belong to us!")
>>> sha.digest(data, "sha-512")
'01001101001000000100100101101010001111100001110011111010001101110
 01111000100101111111100011100001010101111010100111010011000100000
 11111011011001101010111101101000101100011111001000100010010001010
 11111111000110011101111100111011111011100001010111000101110101001
 10010100111100100001101000100001101000000010100001100111001110000
 10010000101110011111001000010111111110000101100111011111011110001
 11111100000001111101110110100011110110111110101111000101110100001
 011100110010001011001001001110001011101000111110011100111'
>>> sha.hex_digest(data, "sha-512")
'4d20496a3e1cfa373c4bfc70abd4e9883ed9abda2c7c88915fe33be77dc2b8ba9
 94f21a21a0286738485cf90bfc2cefbc7f01f768f6faf1742e6459271747ce7'

You can download it free from here.

Reply