Skip to content

GLib.Hmac

record (struct)

HMACs should be used when producing a cookie or hash based on data and a key. Simple mechanisms for using SHA1 and other algorithms to digest a key and data together are vulnerable to various security issues. HMAC uses algorithms like SHA1 in a secure way to produce a digest of a key and data.

Both the key and data are arbitrary byte arrays of bytes or characters.

Support for HMAC Digests has been added in GLib 2.30, and support for SHA-512 in GLib 2.42. Support for SHA-384 was added in GLib 2.52.

To create a new GHmac, use Hmac.new. To free a GHmac, use Hmac.unref.

Constructors

new

@classmethod
def new(cls, digest_type: ChecksumType | int, key: list[int]) -> Hmac | None

Creates a new Hmac, using the digest algorithm digest_type. If the digest_type is not known, None is returned. A Hmac can be used to compute the HMAC of a key and an arbitrary binary blob, using different hashing algorithms.

A Hmac works by feeding a binary blob through Hmac.update until the data is complete; the digest can then be extracted using Hmac.get_string, which will return the checksum as a hexadecimal string; or Hmac.get_digest, which will return a array of raw bytes. Once either Hmac.get_string or Hmac.get_digest have been called on a Hmac, the HMAC will be closed and it won't be possible to call Hmac.update on it anymore.

Support for digests of type ChecksumType.SHA512 has been added in GLib 2.42. Support for ChecksumType.SHA384 was added in GLib 2.52.

Parameters:

  • digest_type — the desired type of digest
  • key — the key for the HMAC

Methods

copy

def copy(self) -> Hmac

Copies a Hmac. If hmac has been closed, by calling Hmac.get_string or Hmac.get_digest, the copied HMAC will be closed as well.

get_digest

def get_digest(self, buffer: list[int]) -> None

Gets the digest from checksum as a raw binary array and places it into buffer. The size of the digest depends on the type of checksum.

Once this function has been called, the Hmac is closed and can no longer be updated with Checksum.update.

Parameters:

  • buffer — output buffer

get_string

def get_string(self) -> str

Gets the HMAC as a hexadecimal string.

Once this function has been called the Hmac can no longer be updated with Hmac.update.

The hexadecimal characters will be lower case.

ref

def ref(self) -> Hmac

Atomically increments the reference count of hmac by one.

This function is MT-safe and may be called from any thread.

unref

def unref(self) -> None

Atomically decrements the reference count of hmac by one.

If the reference count drops to 0, all keys and values will be destroyed, and all memory allocated by the hash table is released. This function is MT-safe and may be called from any thread. Frees the memory allocated for hmac.

update

def update(self, data: list[int]) -> None

Feeds data into an existing Hmac.

The HMAC must still be open, that is Hmac.get_string or Hmac.get_digest must not have been called on hmac.

Parameters:

  • data — buffer used to compute the checksum