Description
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.
Functions
g_hmac_ref ()
GHmac *
g_hmac_ref (GHmac *hmac);
Atomically increments the reference count of hmac
by one.
This function is MT-safe and may be called from any thread.
Returns
the passed in GHmac.
Since: 2.30
g_hmac_unref ()
void
g_hmac_unref (GHmac *hmac);
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
.
Since: 2.30
g_hmac_get_string ()
const gchar *
g_hmac_get_string (GHmac *hmac);
Gets the HMAC as a hexadecimal string.
Once this function has been called the GHmac can no longer be
updated with g_hmac_update().
The hexadecimal characters will be lower case.
Returns
the hexadecimal representation of the HMAC. The
returned string is owned by the HMAC and should not be modified
or freed.
Since: 2.30
g_hmac_get_digest ()
void
g_hmac_get_digest (GHmac *hmac,
guint8 *buffer,
gsize *digest_len);
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 GHmac is closed and can
no longer be updated with g_checksum_update().
Since: 2.30
g_compute_hmac_for_data ()
gchar *
g_compute_hmac_for_data (GChecksumType digest_type,
const guchar *key,
gsize key_len,
const guchar *data,
gsize length);
Computes the HMAC for a binary data
of length
. This is a
convenience wrapper for g_hmac_new(), g_hmac_get_string()
and g_hmac_unref().
The hexadecimal string returned will be in lower case.
Returns
the HMAC of the binary data as a string in hexadecimal.
The returned string should be freed with g_free() when done using it.
Since: 2.30
g_compute_hmac_for_string ()
gchar *
g_compute_hmac_for_string (GChecksumType digest_type,
const guchar *key,
gsize key_len,
const gchar *str,
gssize length);
Computes the HMAC for a string.
The hexadecimal string returned will be in lower case.
Returns
the HMAC as a hexadecimal string.
The returned string should be freed with g_free()
when done using it.
Since: 2.30
g_compute_hmac_for_bytes ()
gchar *
g_compute_hmac_for_bytes (GChecksumType digest_type,
GBytes *key,
GBytes *data);
Computes the HMAC for a binary data
. This is a
convenience wrapper for g_hmac_new(), g_hmac_get_string()
and g_hmac_unref().
The hexadecimal string returned will be in lower case.
Returns
the HMAC of the binary data as a string in hexadecimal.
The returned string should be freed with g_free() when done using it.
Since: 2.50