<refentry id="glib-Data-Checksums">
<refmeta>
<refentrytitle role="top_of_page" id="glib-Data-Checksums.top_of_page">Data Checksums</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>GLIB Library</refmiscinfo>
</refmeta>

<refnamediv>
<refname>Data Checksums</refname>
<refpurpose>Computes the checksum for data</refpurpose>
</refnamediv>

<refsynopsisdiv id="glib-Data-Checksums.synopsis" role="synopsis">
<title role="synopsis.title">Synopsis</title>

<synopsis>

#include &lt;glib.h&gt;

enum                <link linkend="GChecksumType">GChecksumType</link>;
<link linkend="gssize">gssize</link>              <link linkend="g-checksum-type-get-length">g_checksum_type_get_length</link>          (<link linkend="GChecksumType">GChecksumType</link> checksum_type);
                    <link linkend="GChecksum">GChecksum</link>;
<link linkend="GChecksum">GChecksum</link> *         <link linkend="g-checksum-new">g_checksum_new</link>                      (<link linkend="GChecksumType">GChecksumType</link> checksum_type);
<link linkend="GChecksum">GChecksum</link> *         <link linkend="g-checksum-copy">g_checksum_copy</link>                     (const <link linkend="GChecksum">GChecksum</link> *checksum);
<link linkend="void">void</link>                <link linkend="g-checksum-free">g_checksum_free</link>                     (<link linkend="GChecksum">GChecksum</link> *checksum);
<link linkend="void">void</link>                <link linkend="g-checksum-reset">g_checksum_reset</link>                    (<link linkend="GChecksum">GChecksum</link> *checksum);
<link linkend="void">void</link>                <link linkend="g-checksum-update">g_checksum_update</link>                   (<link linkend="GChecksum">GChecksum</link> *checksum,
                                                         const <link linkend="guchar">guchar</link> *data,
                                                         <link linkend="gssize">gssize</link> length);
const <link linkend="gchar">gchar</link> *       <link linkend="g-checksum-get-string">g_checksum_get_string</link>               (<link linkend="GChecksum">GChecksum</link> *checksum);
<link linkend="void">void</link>                <link linkend="g-checksum-get-digest">g_checksum_get_digest</link>               (<link linkend="GChecksum">GChecksum</link> *checksum,
                                                         <link linkend="guint8">guint8</link> *buffer,
                                                         <link linkend="gsize">gsize</link> *digest_len);

<link linkend="gchar">gchar</link> *             <link linkend="g-compute-checksum-for-data">g_compute_checksum_for_data</link>         (<link linkend="GChecksumType">GChecksumType</link> checksum_type,
                                                         const <link linkend="guchar">guchar</link> *data,
                                                         <link linkend="gsize">gsize</link> length);
<link linkend="gchar">gchar</link> *             <link linkend="g-compute-checksum-for-string">g_compute_checksum_for_string</link>       (<link linkend="GChecksumType">GChecksumType</link> checksum_type,
                                                         const <link linkend="gchar">gchar</link> *str,
                                                         <link linkend="gssize">gssize</link> length);
</synopsis>
</refsynopsisdiv>









<refsect1 id="glib-Data-Checksums.description" role="desc">
<title role="desc.title">Description</title>
<para>
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.
</para>
<para>
GLib supports incremental checksums using the GChecksum data structure, by
calling <link linkend="g-checksum-update"><function>g_checksum_update()</function></link> as long as there's data available and then using
<link linkend="g-checksum-get-string"><function>g_checksum_get_string()</function></link> or <link linkend="g-checksum-get-digest"><function>g_checksum_get_digest()</function></link> 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 <link linkend="g-compute-checksum-for-data"><function>g_compute_checksum_for_data()</function></link> and
<link linkend="g-compute-checksum-for-string"><function>g_compute_checksum_for_string()</function></link>, respectively.
</para>
<para>
Support for checksums has been added in GLib 2.16
</para>
</refsect1>

<refsect1 id="glib-Data-Checksums.details" role="details">
<title role="details.title">Details</title>
<refsect2 id="GChecksumType" role="enum" condition="since:2.16">
<title>enum GChecksumType</title>
<indexterm zone="GChecksumType" role="2.16"><primary sortas="ChecksumType">GChecksumType</primary></indexterm><programlisting>typedef enum {
  G_CHECKSUM_MD5,
  G_CHECKSUM_SHA1,
  G_CHECKSUM_SHA256
} GChecksumType;
</programlisting>
<para>
The hashing algorithm to be used by <link linkend="GChecksum"><type>GChecksum</type></link> when performing the
digest of some data.
</para>
<para>
Note that the <link linkend="GChecksumType"><type>GChecksumType</type></link> enumeration may be extended at a later 
date to include new hashing algorithm types.</para>
<para>
</para><variablelist role="enum">
<varlistentry id="G-CHECKSUM-MD5--CAPS" role="constant">
<term><literal>G_CHECKSUM_MD5</literal></term>
<listitem><simpara> Use the MD5 hashing algorithm
</simpara></listitem>
</varlistentry>
<varlistentry id="G-CHECKSUM-SHA1--CAPS" role="constant">
<term><literal>G_CHECKSUM_SHA1</literal></term>
<listitem><simpara> Use the SHA-1 hashing algorithm
</simpara></listitem>
</varlistentry>
<varlistentry id="G-CHECKSUM-SHA256--CAPS" role="constant">
<term><literal>G_CHECKSUM_SHA256</literal></term>
<listitem><simpara> Use the SHA-256 hashing algorithm
</simpara></listitem>
</varlistentry>
</variablelist><para role="since">Since 2.16</para></refsect2>
<refsect2 id="g-checksum-type-get-length" role="function" condition="since:2.16">
<title>g_checksum_type_get_length ()</title>
<indexterm zone="g-checksum-type-get-length" role="2.16"><primary sortas="checksum_type_get_length">g_checksum_type_get_length</primary></indexterm><programlisting><link linkend="gssize">gssize</link>              g_checksum_type_get_length          (<link linkend="GChecksumType">GChecksumType</link> checksum_type);</programlisting>
<para>
Gets the length in bytes of digests of type <parameter>checksum_type</parameter></para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>checksum_type</parameter>&#160;:</term>
<listitem><simpara> a <link linkend="GChecksumType"><type>GChecksumType</type></link>
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the checksum length, or -1 if <parameter>checksum_type</parameter> is
not supported.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.16</para></refsect2>
<refsect2 id="GChecksum" role="struct" condition="since:2.16">
<title>GChecksum</title>
<indexterm zone="GChecksum" role="2.16"><primary sortas="Checksum">GChecksum</primary></indexterm><programlisting>typedef struct _GChecksum GChecksum;</programlisting>
<para>
An opaque structure representing a checksumming operation.
To create a new GChecksum, use <link linkend="g-checksum-new"><function>g_checksum_new()</function></link>. To free
a GChecksum, use <link linkend="g-checksum-free"><function>g_checksum_free()</function></link>.</para>
<para>
</para><para role="since">Since 2.16</para></refsect2>
<refsect2 id="g-checksum-new" role="function" condition="since:2.16">
<title>g_checksum_new ()</title>
<indexterm zone="g-checksum-new" role="2.16"><primary sortas="checksum_new">g_checksum_new</primary></indexterm><programlisting><link linkend="GChecksum">GChecksum</link> *         g_checksum_new                      (<link linkend="GChecksumType">GChecksumType</link> checksum_type);</programlisting>
<para>
Creates a new <link linkend="GChecksum"><type>GChecksum</type></link>, using the checksum algorithm <parameter>checksum_type</parameter>. 
If the <parameter>checksum_type</parameter> is not known, <link linkend="NULL--CAPS"><literal>NULL</literal></link> is returned.
A <link linkend="GChecksum"><type>GChecksum</type></link> can be used to compute the checksum, or digest, of an
arbitrary binary blob, using different hashing algorithms.
</para>
<para>
A <link linkend="GChecksum"><type>GChecksum</type></link> works by feeding a binary blob through <link linkend="g-checksum-update"><function>g_checksum_update()</function></link>
until there is data to be checked; the digest can then be extracted
using <link linkend="g-checksum-get-string"><function>g_checksum_get_string()</function></link>, which will return the checksum as a
hexadecimal string; or <link linkend="g-checksum-get-digest"><function>g_checksum_get_digest()</function></link>, which will return a
vector of raw bytes. Once either <link linkend="g-checksum-get-string"><function>g_checksum_get_string()</function></link> or
<link linkend="g-checksum-get-digest"><function>g_checksum_get_digest()</function></link> have been called on a <link linkend="GChecksum"><type>GChecksum</type></link>, the checksum
will be closed and it won't be possible to call <link linkend="g-checksum-update"><function>g_checksum_update()</function></link>
on it anymore.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>checksum_type</parameter>&#160;:</term>
<listitem><simpara> the desired type of checksum
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the newly created <link linkend="GChecksum"><type>GChecksum</type></link>, or <link linkend="NULL--CAPS"><literal>NULL</literal></link>. 
  Use <link linkend="g-checksum-free"><function>g_checksum_free()</function></link> to free the memory allocated by it.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.16</para></refsect2>
<refsect2 id="g-checksum-copy" role="function" condition="since:2.16">
<title>g_checksum_copy ()</title>
<indexterm zone="g-checksum-copy" role="2.16"><primary sortas="checksum_copy">g_checksum_copy</primary></indexterm><programlisting><link linkend="GChecksum">GChecksum</link> *         g_checksum_copy                     (const <link linkend="GChecksum">GChecksum</link> *checksum);</programlisting>
<para>
Copies a <link linkend="GChecksum"><type>GChecksum</type></link>. If <parameter>checksum</parameter> has been closed, by calling
<link linkend="g-checksum-get-string"><function>g_checksum_get_string()</function></link> or <link linkend="g-checksum-get-digest"><function>g_checksum_get_digest()</function></link>, the copied
checksum will be closed as well.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>checksum</parameter>&#160;:</term>
<listitem><simpara> the <link linkend="GChecksum"><type>GChecksum</type></link> to copy
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the copy of the passed <link linkend="GChecksum"><type>GChecksum</type></link>. Use <link linkend="g-checksum-free"><function>g_checksum_free()</function></link>
  when finished using it.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.16</para></refsect2>
<refsect2 id="g-checksum-free" role="function" condition="since:2.16">
<title>g_checksum_free ()</title>
<indexterm zone="g-checksum-free" role="2.16"><primary sortas="checksum_free">g_checksum_free</primary></indexterm><programlisting><link linkend="void">void</link>                g_checksum_free                     (<link linkend="GChecksum">GChecksum</link> *checksum);</programlisting>
<para>
Frees the memory allocated for <parameter>checksum</parameter>.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>checksum</parameter>&#160;:</term>
<listitem><simpara> a <link linkend="GChecksum"><type>GChecksum</type></link>
</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.16</para></refsect2>
<refsect2 id="g-checksum-reset" role="function" condition="since:2.18">
<title>g_checksum_reset ()</title>
<indexterm zone="g-checksum-reset" role="2.18"><primary sortas="checksum_reset">g_checksum_reset</primary></indexterm><programlisting><link linkend="void">void</link>                g_checksum_reset                    (<link linkend="GChecksum">GChecksum</link> *checksum);</programlisting>
<para>
Resets the state of the <parameter>checksum</parameter> back to its initial state.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>checksum</parameter>&#160;:</term>
<listitem><simpara> the <link linkend="GChecksum"><type>GChecksum</type></link> to reset
</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.18</para></refsect2>
<refsect2 id="g-checksum-update" role="function" condition="since:2.16">
<title>g_checksum_update ()</title>
<indexterm zone="g-checksum-update" role="2.16"><primary sortas="checksum_update">g_checksum_update</primary></indexterm><programlisting><link linkend="void">void</link>                g_checksum_update                   (<link linkend="GChecksum">GChecksum</link> *checksum,
                                                         const <link linkend="guchar">guchar</link> *data,
                                                         <link linkend="gssize">gssize</link> length);</programlisting>
<para>
Feeds <parameter>data</parameter> into an existing <link linkend="GChecksum"><type>GChecksum</type></link>. The checksum must still be
open, that is <link linkend="g-checksum-get-string"><function>g_checksum_get_string()</function></link> or <link linkend="g-checksum-get-digest"><function>g_checksum_get_digest()</function></link> must
not have been called on <parameter>checksum</parameter>.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>checksum</parameter>&#160;:</term>
<listitem><simpara> a <link linkend="GChecksum"><type>GChecksum</type></link>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&#160;:</term>
<listitem><simpara> buffer used to compute the checksum
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>length</parameter>&#160;:</term>
<listitem><simpara> size of the buffer, or -1 if it is a null-terminated string.
</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.16</para></refsect2>
<refsect2 id="g-checksum-get-string" role="function" condition="since:2.16">
<title>g_checksum_get_string ()</title>
<indexterm zone="g-checksum-get-string" role="2.16"><primary sortas="checksum_get_string">g_checksum_get_string</primary></indexterm><programlisting>const <link linkend="gchar">gchar</link> *       g_checksum_get_string               (<link linkend="GChecksum">GChecksum</link> *checksum);</programlisting>
<para>
Gets the digest as an hexadecimal string.
</para>
<para>
Once this function has been called the <link linkend="GChecksum"><type>GChecksum</type></link> can no longer be
updated with <link linkend="g-checksum-update"><function>g_checksum_update()</function></link>.
</para>
<para>
The hexadecimal characters will be lower case.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>checksum</parameter>&#160;:</term>
<listitem><simpara> a <link linkend="GChecksum"><type>GChecksum</type></link>
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the hexadecimal representation of the checksum. The
  returned string is owned by the checksum and should not be modified
  or freed.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.16</para></refsect2>
<refsect2 id="g-checksum-get-digest" role="function" condition="since:2.16">
<title>g_checksum_get_digest ()</title>
<indexterm zone="g-checksum-get-digest" role="2.16"><primary sortas="checksum_get_digest">g_checksum_get_digest</primary></indexterm><programlisting><link linkend="void">void</link>                g_checksum_get_digest               (<link linkend="GChecksum">GChecksum</link> *checksum,
                                                         <link linkend="guint8">guint8</link> *buffer,
                                                         <link linkend="gsize">gsize</link> *digest_len);</programlisting>
<para>
Gets the digest from <parameter>checksum</parameter> as a raw binary vector and places it
into <parameter>buffer</parameter>. The size of the digest depends on the type of checksum.
</para>
<para>
Once this function has been called, the <link linkend="GChecksum"><type>GChecksum</type></link> is closed and can
no longer be updated with <link linkend="g-checksum-update"><function>g_checksum_update()</function></link>.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>checksum</parameter>&#160;:</term>
<listitem><simpara> a <link linkend="GChecksum"><type>GChecksum</type></link>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>buffer</parameter>&#160;:</term>
<listitem><simpara> output buffer
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>digest_len</parameter>&#160;:</term>
<listitem><simpara> an inout parameter. The caller initializes it to the size of <parameter>buffer</parameter>.
  After the call it contains the length of the digest.
</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.16</para></refsect2>
<refsect2 id="g-compute-checksum-for-data" role="function" condition="since:2.16">
<title>g_compute_checksum_for_data ()</title>
<indexterm zone="g-compute-checksum-for-data" role="2.16"><primary sortas="compute_checksum_for_data">g_compute_checksum_for_data</primary></indexterm><programlisting><link linkend="gchar">gchar</link> *             g_compute_checksum_for_data         (<link linkend="GChecksumType">GChecksumType</link> checksum_type,
                                                         const <link linkend="guchar">guchar</link> *data,
                                                         <link linkend="gsize">gsize</link> length);</programlisting>
<para>
Computes the checksum for a binary <parameter>data</parameter> of <parameter>length</parameter>. This is a
convenience wrapper for <link linkend="g-checksum-new"><function>g_checksum_new()</function></link>, <link linkend="g-checksum-get-string"><function>g_checksum_get_string()</function></link>
and <link linkend="g-checksum-free"><function>g_checksum_free()</function></link>.
</para>
<para>
The hexadecimal string returned will be in lower case.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>checksum_type</parameter>&#160;:</term>
<listitem><simpara> a <link linkend="GChecksumType"><type>GChecksumType</type></link>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&#160;:</term>
<listitem><simpara> binary blob to compute the digest of
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>length</parameter>&#160;:</term>
<listitem><simpara> length of <parameter>data</parameter>
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the digest of the binary data as a string in hexadecimal.
  The returned string should be freed with <link linkend="g-free"><function>g_free()</function></link> when done using it.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.16</para></refsect2>
<refsect2 id="g-compute-checksum-for-string" role="function" condition="since:2.16">
<title>g_compute_checksum_for_string ()</title>
<indexterm zone="g-compute-checksum-for-string" role="2.16"><primary sortas="compute_checksum_for_string">g_compute_checksum_for_string</primary></indexterm><programlisting><link linkend="gchar">gchar</link> *             g_compute_checksum_for_string       (<link linkend="GChecksumType">GChecksumType</link> checksum_type,
                                                         const <link linkend="gchar">gchar</link> *str,
                                                         <link linkend="gssize">gssize</link> length);</programlisting>
<para>
Computes the checksum of a string.
</para>
<para>
The hexadecimal string returned will be in lower case.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>checksum_type</parameter>&#160;:</term>
<listitem><simpara> a <link linkend="GChecksumType"><type>GChecksumType</type></link>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>str</parameter>&#160;:</term>
<listitem><simpara> the string to compute the checksum of
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>length</parameter>&#160;:</term>
<listitem><simpara> the length of the string, or -1 if the string is null-terminated.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the checksum as a hexadecimal string. The returned string
  should be freed with <link linkend="g-free"><function>g_free()</function></link> when done using it.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.16</para></refsect2>

</refsect1>




</refentry>
