Secure Hash Algorithms

SHA algorithms (e.g., SHA-1, SHA-256) produce a fixed-size digest from arbitrary input data. They are designed to be collision-resistant, meaning it is computationally infeasible to find two inputs with the same hash. SHA is used for integrity verification, digital signatures, and building cryptographic protocols.

class pycrypt.hash.SHA1(data=None)[source]

Bases: SHACore

SHA-1 cryptographic hash function.

Implements the SHA-1 algorithm (160-bit digest) using the core SHA framework defined in SHACore.

Example

>>> sha = SHA1(b"hello world")
>>> sha.hexdigest()
'2aae6c35c94fcfb415dbe95f408b9ce91ee846ed'
>>> sha.update(b"!")
>>> sha.hexdigest()
'7c211433f02071597741e6ff5a8ea34789abbf43'
Parameters:

data (bytes | bytearray | None)

BLOCK_SIZE: int = 64

Input block size in bytes.

DIGEST_SIZE: int = 20

Output digest size in bytes.

WORD_SIZE: int = 32

Word size in bits.

digest()

Return the hash digest of all processed data.

This finalizes the current message with padding as per the SHA specification, processes any remaining blocks, and returns the resulting digest bytes.

Returns:

The binary hash digest.

Return type:

bytes

hexdigest()

Return the digest as a hexadecimal string.

Returns:

Hexadecimal representation of the digest.

Return type:

str

reset()

Reset the hashing state to its initial values.

Return type:

None

update(data)

Update the hash with new data.

Parameters:

data (bytes) – Input data to hash. May be called multiple times to process streaming input.

class pycrypt.hash.SHA256(data=None)[source]

Bases: SHACore

SHA-256 cryptographic hash function.

Implements the SHA-256 algorithm (256-bit digest) using the core SHA framework defined in SHACore.

Example

>>> sha = SHA256(b"hello world")
>>> sha.hexdigest()
'b94d27b9934d3e08a52e52d7da7dabfade4b988'  # truncated example
>>> sha.update(b"!")
>>> sha.hexdigest()
'7f83b1657ff1fc53b92dc18148a1d65dfc2d4a'  # truncated example
Parameters:

data (bytes | bytearray | None)

BLOCK_SIZE: int = 64

Input block size in bytes.

DIGEST_SIZE: int = 32

Output digest size in bytes.

WORD_SIZE: int = 32

Word size in bits.

digest()

Return the hash digest of all processed data.

This finalizes the current message with padding as per the SHA specification, processes any remaining blocks, and returns the resulting digest bytes.

Returns:

The binary hash digest.

Return type:

bytes

hexdigest()

Return the digest as a hexadecimal string.

Returns:

Hexadecimal representation of the digest.

Return type:

str

reset()

Reset the hashing state to its initial values.

Return type:

None

update(data)

Update the hash with new data.

Parameters:

data (bytes) – Input data to hash. May be called multiple times to process streaming input.