GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Apr 23, 2014 5:45 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Glib GChecksumType Examples (C)
PostPosted: Fri Apr 24, 2009 4:17 am 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
Data Checksums — Computes the checksum for data.
You have three hashing algorithms to choose from "MD5, SHA1, SHA2-256"

http://library.gnome.org/devel/glib/sta ... ksums.html

Calculates checksum for files no bigger than 1mb.
Code:
#include <stdio.h>
#include <glib.h>

#define MAX_SIZE 1024 * 1024 /* 1 Mb */

int
main( int    argc,
      char **argv )
{
    guchar  data[MAX_SIZE];
    gsize   size = 0;
    FILE   *input;
    gchar  *sum;

    if( argc < 2 )
    {
        g_print( "USAGE: sum <filename> (max filename size is 1 MB)\n" );
        return( 0 );
    }

    input = fopen( argv[1], "rb" );
    size = fread( (void *)data, sizeof( guchar ), MAX_SIZE, input );
    fclose( input );

    sum = g_compute_checksum_for_data( G_CHECKSUM_MD5, data, size );
    g_print( "Checksum for >> %s <<: %s\n", argv[1], sum );
    g_free( sum );

    return( 0 );
}


Calculates checksum for files of variable size.
Code:
#include <stdio.h>
#include <glib.h>

#define MAX_SIZE 1024 /* 1 kb */

int
main( int    argc,
      char **argv )
{
    GChecksum   *cs;
    guchar       data[MAX_SIZE];
    gsize        size = 0;
    FILE        *input;
    const gchar *sum;

    if( argc < 2 )
    {
        g_print( "USAGE: sum <filename>\n" );
        return( 0 );
    }

    cs = g_checksum_new( G_CHECKSUM_MD5 );
    input = fopen( argv[1], "rb" );
    do
    {
        size = fread( (void *)data, sizeof( guchar ), MAX_SIZE, input );
        g_checksum_update( cs, data, size );
    }
    while( size == MAX_SIZE );
    fclose( input );

    sum = g_checksum_get_string( cs );
    g_print( "Checksum for >> %s <<: %s\n", argv[1], sum );
    g_checksum_free( cs );

    return( 0 );
}


Whats covered here:
http://library.gnome.org/devel/glib/sta ... ecksumType
http://library.gnome.org/devel/glib/sta ... #GChecksum
http://library.gnome.org/devel/glib/sta ... ecksum-new
http://library.gnome.org/devel/glib/sta ... cksum-free
http://library.gnome.org/devel/glib/sta ... sum-update
http://library.gnome.org/devel/glib/sta ... get-string
http://library.gnome.org/devel/glib/sta ... m-for-data

Whats not yet covered:
http://library.gnome.org/devel/glib/sta ... get-length
http://library.gnome.org/devel/glib/sta ... cksum-copy
http://library.gnome.org/devel/glib/sta ... ksum-reset
http://library.gnome.org/devel/glib/sta ... get-digest
http://library.gnome.org/devel/glib/sta ... for-string

Original Thread:
http://www.gtkforums.com/viewtopic.php?t=3381

And a big thanks goes out to tadeboro for they examples.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group