MD5 who are GLib friendly?

Does anyone know about the MD5 / SHA1 / etc routine, which is easy to use with GLib (i.e. you can give it a GIOChannel, etc.)?

+6
c linux glib
source share
2 answers

If you don't have a good reason, use the built-in MD5, SHA1, and SHA256 with GChecksum . It does not have a built-in function to create a checksum from the input / output stream, but you can write a simple one in 10 lines, and you still have to write a complex one.

+6
source share

You usually need to make library glue ...

 void get_channel_md5( GIOChannel* channel, unsigned char output[16] ) { md5_context ctx; gint64 fileSize = <get file size somehow?>; gint64 filePos = 0ll; gsize bufferSize = g_io_channel_get_buffer_size( channel ); void* buffer = malloc( bufferSize ); md5_starts( &ctx ); // hash buffer at a time: while ( filePos < fileSize ) { gint64 size = fileSize - filePos; if ( size > bufferSize ) size = bufferSize; g_io_channel_read( channel, buffer ); md5_update( &ctx, buffer, (int)size ); filePos += bufferSize; } free( buffer ); md5_finish( &ctx, output ); } 
+2
source share

All Articles