SHA-256

SHA-256 belongs to the SHA-2_ family of cryptographic hashes. It produces the 256 bit digest of a message.

>>> from Crypto.Hash import SHA256
>>>
>>> h = SHA256.new()
>>> h.update(b'Hello')
>>> print h.hexdigest()

SHA stands for Secure Hash Algorithm.

Warning

SHA-256 is vulnerable to length-extension attacks, which are relevant if you are computing the hash of a secret message.

For instance, let’s say you were planning to build a cheap MAC by concatenating a secret key to a public message m (bad idea!):

\[h = \text{SHA-256}(m || k)\]

By only knowing the digest h and the length of m and k, the attacker can easily compute a second digest h’:

\[h' = \text{SHA-256}(m || p || z)\]

where p is a well-known bit string and the attacker can pick a bit string z at will.

SHA-256 cryptographic hash algorithm.

SHA-256 belongs to the SHA-2_ family of cryptographic hashes. It produces the 256 bit digest of a message.

>>> from Crypto.Hash import SHA256
>>>
>>> h = SHA256.new()
>>> h.update(b'Hello')
>>> print h.hexdigest()

SHA stands for Secure Hash Algorithm.

class Crypto.Hash.SHA256.SHA256Hash(data=None)

Class that implements a SHA-256 hash

Undocumented:

block_size

new(data=None)

Return a fresh instance of the hash object.

Unlike the copy method, the internal state of the object is empty.

Parameters:
databyte string

The next chunk of the message being hashed.

Return:

A hash object of the same type

oid = b'\x06\t`\x86H\x01e\x03\x04\x02\x01'

ASN.1 Object identifier (OID):

id-sha256    OBJECT IDENTIFIER ::= {
           joint-iso-itu-t(2) country(16) us(840) organization(1)
            gov(101) csor(3) nistalgorithm(4) hashalgs(2) 1
}

This value uniquely identifies the SHA-256 algorithm.

Crypto.Hash.SHA256.digest_size = 32

The size of the resulting hash in bytes.

Crypto.Hash.SHA256.new(data=None)

Return a fresh instance of the hash object.

Parameters:
databyte string

The very first chunk of the message to hash. It is equivalent to an early call to SHA256Hash.update(). Optional.

Return:

A SHA256Hash object