Checksum


Object Hierarchy:

GLib.Checksum GLib.Checksum GLib.Checksum

Description:

[ Compact ]
[ Version ( since = "2.16" ) ]
[ CCode ( free_function = "g_checksum_free" , type_id = "G_TYPE_CHECKSUM" ) ]
public class 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.

Example: Compute the checksum of a file:

public static int main (string[] args) {
Checksum checksum = new Checksum (ChecksumType.MD5);

FileStream stream = FileStream.open (args[0], "rb");
uint8 fbuf[100];
size_t size;

while ((size = stream.read (fbuf)) > 0) {
checksum.update (fbuf, size);
}

unowned string digest = checksum.get_string ();
print ("%s: %s\n", args[0], digest);
return 0;
}

valac --pkg glib-2.0 GLib.Checksum.vala


Namespace: GLib
Package: glib-2.0

Content:

Static methods:

Creation methods:

Methods: