Hash class

class cryptosyspki.Hash

Compute message digest hash values.

class Alg

Hash algorithms.

BTC160 = 8

RIPEMD-160 hash of a SHA-256 hash (RIPEMD160(SHA256(m)))

MD5(as per RFC 1321) = 1

MD5 (as per RFC 1321)

RMD160 = 7

RIPEMD-160

SHA1 = 0

SHA-1 (default)

SHA224 = 6

SHA-224

SHA256 = 3

SHA-256

SHA384 = 4

SHA-384

SHA3_224 = 10

SHA-3-224

SHA3_256 = 11

SHA-3-256

SHA3_384 = 12

SHA-3-384

SHA3_512 = 13

SHA-3-512

SHA512 = 5

SHA-512

static data(data, alg=Alg.SHA1)

Compute message digest as a byte array from bytes data.

Parameters:
  • data (bytes) -- Message data

  • alg (Hash.Alg) -- Hash algorithm to be used

Returns:

Message digest in byte array.

Return type:

bytes

static double(data, alg=Alg.SHA1)

Create a double hash - hash of hash - as a byte array from bytes data.

Parameters:
  • data (bytes) -- Message data in byte array

  • alg (Hash.Alg) -- Hash algorithm to be used

Returns:

Message digest HASH(HASH(m)) in byte format

Return type:

bytes

static file(filename, alg=Alg.SHA1)

Compute message digest as a byte array from data in a file.

Parameters:
  • filename (str) -- Name of file containing message data

  • alg (Hash.Alg) -- Hash algorithm to be used

Returns:

Message digest in byte array.

Return type:

bytes

static hex_from_data(data, alg=Alg.SHA1)

Compute message digest in hexadecimal format from bytes data.

Parameters:
  • data (bytes) -- Message data in byte array.

  • alg (Hash.Alg) -- Hash algorithm to be used.

Returns:

Message digest in hex-encoded format.

Return type:

string

Examples

>>> Hash.hex_from_data(b'abc')
'a9993e364706816aba3e25717850c26c9cd0d89d'
>>> Hash.hex_from_data(b'abc', Hash.Alg.SHA256)
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
static hex_from_file(filename, alg=Alg.SHA1)

Compute message digest in hexadecimal format from data in a file.

Parameters:
  • filename (str) -- Name of file containing message data

  • alg (Hash.Alg) -- Hash algorithm to be used

Returns:

Message digest in hex-encoded format

Return type:

str

static hex_from_hex(datahex, alg=Alg.SHA1)

Compute message digest in hexadecimal format from data in a hexadecimal-encoded string.

Parameters:
  • datahex (str) -- Message data in hex-encoded format

  • alg (Hash.Alg) -- Hash algorithm to be used

Returns:

Message digest in hex-encoded format.

Return type:

str

Examples

>>> Hash.hex_from_hex('616263')  # HEX('abc')
'a9993e364706816aba3e25717850c26c9cd0d89d'
static hex_from_string(s, alg=Alg.SHA1)

Compute message digest in hexadecimal format from a string.

Parameters:
  • s (str) -- Message data in UTF-8 string.

  • alg (Hash.Alg) -- Hash algorithm to be used.

Returns:

Message digest in hex-encoded format.

Return type:

str

Examples

>>> Hash.hex_from_string('abc', Hash.Alg.SHA256)
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
>>> Hash.hex_from_string('Olá mundo')  # UTF-8
'f6c2fc0dd7f1131d8cb5ac7420d77a4c28ac1aa0'
static length(alg)

Return length of message digest output in bytes.

Parameters:

alg (Hash.Alg) -- Hash algorithm.

Returns:

Length of the hash function output in bytes.

Return type:

int

Examples

>>> Hash.length(Hash.Alg.SHA256)
32
>>> Hash.length(Hash.Alg.SHA512)
64