Mac class

class crsysapi.Mac

Message authentication code (MAC) functions.

class Alg

MAC algorithms.

CMAC_AES128 = 257

CMAC-AES128

CMAC_AES192 = 258

CMAC-AES192

CMAC_AES256 = 259

CMAC-AES256

CMAC_TDEA = 256

CMAC-TDEA (CMAC-DESEDE)

HMAC_MD5 = 1

HMAC-MD5

HMAC_RMD160 = 7

HMAC-RMD160

HMAC_SHA1 = 0

HMAC-SHA-1 (default)

HMAC_SHA224 = 6

HMAC-SHA-224

HMAC_SHA256 = 3

HMAC-SHA-256

HMAC_SHA384 = 4

HMAC-SHA-384

HMAC_SHA3_224 = 10

HMAC-SHA-3-224

HMAC_SHA3_256 = 11

HMAC-SHA-3-256

HMAC_SHA3_384 = 12

HMAC-SHA-3-384

HMAC_SHA3_512 = 13

HMAC-SHA-3-512

HMAC_SHA512 = 5

HMAC-SHA-512

KMAC_128 = 513

KMAC128 with a fixed-length output of 256 bits (32 bytes)

KMAC_256 = 514

KMAC256 with a fixed-length output of 512 bits (64 bytes)

MAC_POLY1305 = 512

Poly1305

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

Compute a message authentication code (MAC) as a byte array from bytes data.

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

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

  • alg (Mac.Alg) -- MAC algorithm to be used.

Returns:

MAC in byte format

Return type:

bytes

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

Compute a message authentication code (MAC) in hexadecimal format from bytes data.

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

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

  • alg (Mac.Alg) -- MAC algorithm to be used.

Returns:

MAC in hex-encoded format.

Return type:

str

Examples

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

Compute a message authentication code (MAC) 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 (Mac.Alg) -- MAC algorithm to be used.

Returns:

HMAC in hex-encoded format.

Return type:

str

Examples

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

Compute a message authentication code (MAC) in hexadecimal format from string data.

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

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

  • alg (Mac.Alg) -- MAC algorithm to be used.

Returns:

Message digest in hex-encoded format.

Return type:

str