GLib.Hmac¶
Fields¶
None
Methods¶
class |
|
|
|
|
|
|
|
|
|
|
|
|
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 aGHmac
, use [method`GLib`.Hmac.unref].New in version 2.30.
- classmethod new(digest_type, key)[source]¶
- Parameters:
digest_type (
GLib.ChecksumType
) – the desired type of digestkey (
bytes
) – the key for the HMAC
- Returns:
the newly created
GLib.Hmac
, orNone
. UseGLib.Hmac.unref
() to free the memory allocated by it.- Return type:
Creates a new
GLib.Hmac
, using the digest algorithm digest_type. If the digest_type is not known,None
is returned. AGLib.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 throughGLib.Hmac.update
() until the data is complete; the digest can then be extracted usingGLib.Hmac.get_string
(), which will return the checksum as a hexadecimal string; orGLib.Hmac.get_digest
(), which will return a array of raw bytes. Once eitherGLib.Hmac.get_string
() orGLib.Hmac.get_digest
() have been called on aGLib.Hmac
, the HMAC will be closed and it won’t be possible to callGLib.Hmac.update
() on it anymore.Support for digests of type
GLib.ChecksumType.SHA512
has been added in GLib 2.42. Support forGLib.ChecksumType.SHA384
was added in GLib 2.52.New in version 2.30.
- copy()[source]¶
- Returns:
the copy of the passed
GLib.Hmac
. UseGLib.Hmac.unref
() when finished using it.- Return type:
Copies a
GLib.Hmac
. If self has been closed, by callingGLib.Hmac.get_string
() orGLib.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 withGLib.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:
Gets the HMAC as a hexadecimal string.
Once this function has been called the
GLib.Hmac
can no longer be updated withGLib.Hmac.update
().The hexadecimal characters will be lower case.
New in version 2.30.
- ref()[source]¶
-
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
() orGLib.Hmac.get_digest
() must not have been called on self.New in version 2.30.