Hmac class

class cryptosyspki.Hmac

Compute keyed-hash based message authentication code (HMAC) values.

class Alg

HMAC algorithms.

MD5 = 1

HMAC-MD5

SHA1 = 0

HMAC-SHA-1 (default)

SHA224 = 6

HMAC-SHA-224

SHA256 = 3

HMAC-SHA-256

SHA384 = 4

HMAC-SHA-384

SHA3_224 = 10

HMAC-SHA-3-224

SHA3_256 = 11

HMAC-SHA-3-256

SHA3_384 = 12

HMAC-SHA-3-384

SHA3_512 = 13

HMAC-SHA-3-512

SHA512 = 5

HMAC-SHA-512

static data(data, key, alg=Alg.SHA1)

Compute a keyed-hash based message authentication code (HMAC) as a byte array from bytes data.

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

  • key (bytes) -- Key in byte array.

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

Returns:

HMAC in byte format

Return type:

bytes

static hex_from_data(data, key, alg=Alg.SHA1)

Compute a keyed-hash based message authentication code (HMAC) in hexadecimal format from bytes data.

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

  • key (bytes) -- Key in byte array.

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

Returns:

HMAC in hex-encoded format.

Return type:

str

Examples

>>> Hmac.hex_from_data(b"Hi There", Cnv.fromhex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"))
'b617318655057264e28bc0b6fb378c8ef146be00'
static hex_from_hex(datahex, keyhex, alg=Alg.SHA1)

Compute a keyed-hash based message authentication code (HMAC) in hex format from data in hex-encoded strings.

Parameters:
  • datahex (str) -- Message to be signed in hex-encoded format.

  • keyhex (str) -- Key in hex-encoded format.

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

Returns:

HMAC in hex-encoded format.

Return type:

str

Examples

>>> # HEX('Hi There') = 4869205468657265
>>> Hmac.hex_from_hex("4869205468657265", "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b")
'b617318655057264e28bc0b6fb378c8ef146be00'
static hex_from_string(s, key, alg=Alg.SHA1)

Compute a keyed-hash based message authentication code (HMAC) in hexadecimal format from string data.

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

  • key (bytes) -- Key in byte array.

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

Returns:

Message digest in hex-encoded format.

Return type:

str