Skip to content

GLib.Checksum

record (struct)

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 Checksum.new. To free a GChecksum, use Checksum.free.

GLib supports incremental checksums using the GChecksum data structure, by calling Checksum.update as long as there’s data available and then using Checksum.get_string or 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 compute_checksum_for_data and compute_checksum_for_string, respectively.

Constructors

new

@classmethod
def new(cls, checksum_type: ChecksumType | int) -> Checksum | None

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

A Checksum works by feeding a binary blob through Checksum.update until there is data to be checked; the digest can then be extracted using 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 Checksum.get_string or g_checksum_get_digest() have been called on a Checksum, the checksum will be closed and it won't be possible to call Checksum.update on it anymore.

Parameters:

  • checksum_type — the desired type of checksum

Methods

copy

def copy(self) -> Checksum

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

free

def free(self) -> None

Frees the memory allocated for checksum.

get_string

def get_string(self) -> str

Gets the digest as a hexadecimal string.

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

The hexadecimal characters will be lower case.

reset

def reset(self) -> None

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

update

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

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

Parameters:

  • data — buffer used to compute the checksum

Static functions

type_get_length

@staticmethod
def type_get_length(checksum_type: ChecksumType | int) -> int

Gets the length in bytes of digests of type checksum_type

Parameters: