Hash class¶
- class crsysapi.Hash¶
Message digest hash functions.
- class Alg¶
Hash algorithms.
- ASCON_HASH = 175¶
ASCON-HASH
- ASCON_HASHA = 191¶
ASCON-HASHA
- MD5 = 1¶
MD5
- 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 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 (ASCON is not supported in file mode)
- Returns:
Message digest in byte array.
- Return type:
bytes
- static hex_from_bits(data, databitlen, alg=Alg.SHA1)¶
Compute message digest in hexadecimal format from bit-oriented data.
- Parameters:
data (bytes) -- Message data in byte array.
databitlen (int) -- Length of message data in bits.
alg (Hash.Alg) -- Hash algorithm to be used (only the SHA family is supported).
- Returns:
Message digest in hex-encoded format.
- Return type:
string
Note
Pass a bitstring as an array of bytes in data in big-endian order with the most-significant bit first. The bitstring will be truncated to the number of bits specified in databitlen. The number of bytes in data must be at least ceil(databitlen / 8).
Examples
>>> Hash.hex_from_bits(Cnv.fromhex("5180"), 9, Hash.Alg.SHA1) # 0101 0001 1 '0f582fa68b71ecdf1dcfc4946019cf5a18225bd2' >>> Hash.hex_from_bits(Cnv.fromhex("2590A0"), 22, Hash.Alg.SHA3_256) # 1001 0110 0100 0010 1000 00 'd5863d4b1ff41551c92a9e08c52177e32376c9bd100c611c607db840096eb22f'
- 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 (ASCON is not supported in file mode)
- 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' >>> Hash.hex_from_string("", Hash.Alg.ASCON_HASHA) 'aecd027026d0675f9de7a8ad8ccf512db64b1edcf0b20c388a0c7cc617aaa2c4'