GLib.Checksum

Fields

None

Methods

class

new (checksum_type)

class

type_get_length (checksum_type)

copy ()

free ()

get_string ()

reset ()

update (data)

Details

class GLib.Checksum

GLib provides a generic API for computing checksums (or ‘digests’) for a sequence of arbitrary bytes, using various hashing algorithms like MD5, SHA-1 and SHA-256. Checksums are commonly used in various environments and specifications.

To create a new GChecksum, use [ctor`GLib`.Checksum.new]. To free a GChecksum, use [method`GLib`.Checksum.free].

GLib supports incremental checksums using the GChecksum data structure, by calling [method`GLib`.Checksum.update] as long as there’s data available and then using [method`GLib`.Checksum.get_string] or [method`GLib`.Checksum.get_digest] to compute the checksum and return it either as a string in hexadecimal form, or as a raw sequence of bytes. To compute the checksum for binary blobs and nul-terminated strings in one go, use the convenience functions [func`GLib`.compute_checksum_for_data] and [func`GLib`.compute_checksum_for_string], respectively.

New in version 2.16.

classmethod new(checksum_type)[source]
Parameters:

checksum_type (GLib.ChecksumType) – the desired type of checksum

Returns:

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

Return type:

GLib.Checksum or None

Creates a new GLib.Checksum, using the checksum algorithm checksum_type. If the checksum_type is not known, None is returned. A GLib.Checksum can be used to compute the checksum, or digest, of an arbitrary binary blob, using different hashing algorithms.

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

New in version 2.16.

classmethod type_get_length(checksum_type)[source]
Parameters:

checksum_type (GLib.ChecksumType) – a GLib.ChecksumType

Returns:

the checksum length, or -1 if checksum_type is not supported.

Return type:

int

Gets the length in bytes of digests of type checksum_type

New in version 2.16.

copy()[source]
Returns:

the copy of the passed GLib.Checksum. Use GLib.Checksum.free() when finished using it.

Return type:

GLib.Checksum

Copies a GLib.Checksum. If self has been closed, by calling GLib.Checksum.get_string() or g_checksum_get_digest(), the copied checksum will be closed as well.

New in version 2.16.

free()[source]

Frees the memory allocated for self.

New in version 2.16.

get_string()[source]
Returns:

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

Return type:

str

Gets the digest as a hexadecimal string.

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

The hexadecimal characters will be lower case.

New in version 2.16.

reset()[source]

Resets the state of the self back to its initial state.

New in version 2.18.

update(data)[source]
Parameters:

data (bytes) – buffer used to compute the checksum

Feeds data into an existing GLib.Checksum. The checksum must still be open, that is GLib.Checksum.get_string() or g_checksum_get_digest() must not have been called on self.

New in version 2.16.