GLib.Hmac

Fields

None

Methods

class

new (digest_type, key)

copy ()

get_digest (buffer)

get_string ()

ref ()

unref ()

update (data)

Details

class GLib.Hmac

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 [ctor`GLib`.Hmac.new]. To free a GHmac, use [method`GLib`.Hmac.unref].

New in version 2.30.

classmethod new(digest_type, key)[source]
Parameters:
Returns:

the newly created GLib.Hmac, or None. Use GLib.Hmac.unref() to free the memory allocated by it.

Return type:

GLib.Hmac or None

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

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

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

New in version 2.30.

copy()[source]
Returns:

the copy of the passed GLib.Hmac. Use GLib.Hmac.unref() when finished using it.

Return type:

GLib.Hmac

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

New in version 2.30.

get_digest(buffer)[source]
Parameters:

buffer (bytes) – output buffer

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 GLib.Hmac is closed and can no longer be updated with GLib.Checksum.update().

New in version 2.30.

get_string()[source]
Returns:

the hexadecimal representation of the HMAC. The returned string is owned by the HMAC and should not be modified or freed.

Return type:

str

Gets the HMAC as a hexadecimal string.

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

The hexadecimal characters will be lower case.

New in version 2.30.

ref()[source]
Returns:

the passed in GLib.Hmac.

Return type:

GLib.Hmac

Atomically increments the reference count of self by one.

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

New in version 2.30.

unref()[source]

Atomically decrements the reference count of self 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 self.

New in version 2.30.

update(data)[source]
Parameters:

data (bytes) – buffer used to compute the checksum

Feeds data into an existing GLib.Hmac.

The HMAC must still be open, that is GLib.Hmac.get_string() or GLib.Hmac.get_digest() must not have been called on self.

New in version 2.30.