简介
GLib提供了计算校验和(也叫摘要)的方法,支持MD5、SHA-1、SHA-256等散列算法。
数据结构
数据校验和的数据结构是不透明结构体,只能作为整体使用,无法单独引用其内部成员。
typedef struct _GChecksum GChecksum;
函数列表
gssize g_checksum_type_get_length ()
GChecksum * g_checksum_new ()
GChecksum * g_checksum_copy ()
void g_checksum_free ()
void g_checksum_reset ()
void g_checksum_update ()
const gchar * g_checksum_get_string ()
void g_checksum_get_digest ()
gchar * g_compute_checksum_for_data ()
gchar * g_compute_checksum_for_string ()
gchar * g_compute_checksum_for_bytes ()
函数功能分类
// 获取摘要算法的长度(以字节计)
gssize g_checksum_type_get_length ()
// 创建一个GChecksum对象,摘要算法可以在参数中指定
GChecksum * g_checksum_new ()
// 复制一个GChecksum对象
GChecksum * g_checksum_copy ()
// 释放一个GChecksum对象
void g_checksum_free ()
// 将GChecksum对象重置为其初始状态
void g_checksum_reset ()
// 将数据累加到GChecksum上,如果要计算的数据比较大,此函数可被多次调用
void g_checksum_update ()
// 将摘要转换成字符串
const gchar * g_checksum_get_string ()
// 获取校验和
void g_checksum_get_digest ()
// 计算校验和
gchar * g_compute_checksum_for_data ()
gchar * g_compute_checksum_for_string ()
gchar * g_compute_checksum_for_bytes ()
函数功能说明及综合演示
很多网站提供在线计算MD5、SHAx功能,可以将GLib计算出的结果与网络在线计算的结果进行比较,查看是否正确。Linux系统也有计算MD5和SHAx的命令行工具md5sum
、sha1sum
、sha256sum
等等,不过只能读入文件,对比是要特别注意文件结尾的换行符是否参与了计算。
MD5校验和
示例代码如下:
源码见glib_examples\glib_checksum\glib_checksum_md5
#include <glib.h>
gint main(gint argc, gchar **argv)
{
GChecksum *checksum_a = NULL;
GChecksum *checksum_b = NULL;
checksum_a = g_checksum_new(G_CHECKSUM_MD5);
g_checksum_update(checksum_a, "A", strlen("A"));
g_print("MD5(A):%s \n", g_checksum_get_string(checksum_a));
g_checksum_free(checksum_a);
checksum_b = g_checksum_new(G_CHECKSUM_MD5);
g_checksum_update(checksum_b, "B", strlen("B"));
g_print("MD5(B):%s \n", g_checksum_get_string(checksum_b));
g_checksum_free(checksum_b);
return 0;
}
运行结果:
[root@centos7_6 build]# ./glib_checksum_md5
md5(A):7fc56270e7a70fa81a5935b72eacbe29
md5(B):9d5ed678fe57bcca610140957afab571
MD5校验和的多步骤计算
如果需要计算的MD5特别大,甚至超过运行内存(如:16GB的文件),则需要将要计算的内容分成多个部分,分别计算。g_checksum_update可将单次计算的值更新到上次计算的结果上,最终得到完整的MD5值。
特别注意的是,在调用g_checksum_update前,不可以调用g_checksum_get_string函数,如果调用,则无法对计算的结果进行更新。在程序运行时也会有错误提示。
示例代码如下:
源码见glib_examples\glib_checksum\glib_checksum_md5_update
#include <glib.h>
gint main(gint argc, gchar **argv)
{
GChecksum *checksum_a = NULL;
GChecksum *checksum_b = NULL;
GChecksum *checksum_ab1 = NULL;
GChecksum *checksum_ab2 = NULL;
checksum_a = g_checksum_new(G_CHECKSUM_MD5);
g_checksum_update(checksum_a, "A", strlen("A"));
g_print("MD5(A):%s \n", g_checksum_get_string(checksum_a));
g_checksum_free(checksum_a);
checksum_b = g_checksum_new(G_CHECKSUM_MD5);
g_checksum_update(checksum_b, "B", strlen("B"));
g_print("MD5(B):%s \n", g_checksum_get_string(checksum_b));
g_checksum_free(checksum_b);
checksum_ab1 = g_checksum_new(G_CHECKSUM_MD5);
g_checksum_update(checksum_ab1, "AB", strlen("AB"));
g_print("MD5(AB)(once):%s \n", g_checksum_get_string(checksum_ab1));
g_checksum_free(checksum_ab1);
checksum_ab2 = g_checksum_new(G_CHECKSUM_MD5);
g_checksum_update(checksum_ab2, "A", strlen("A"));
//ERROR // g_print("MD5(A):%s \n", g_checksum_get_string(checksum_ab2));
g_checksum_update(checksum_ab2, "B", strlen("B"));
g_print("MD5(AB)(multi):%s \n", g_checksum_get_string(checksum_ab2));
g_checksum_free(checksum_ab2);
return 0;
}
运行结果:
[root@centos7_6 build]# ./glib_checksum_md5_update
md5(A):7fc56270e7a70fa81a5935b72eacbe29
md5(B):9d5ed678fe57bcca610140957afab571
md5(AB)(once):b86fc6b051f63d73de262d4c34e3a0a9
md5(AB)(update):b86fc6b051f63d73de262d4c34e3a0a9
SHA1校验和
示例代码如下:
源码见glib_examples\glib_checksum\glib_checksum_sha1
#include <glib.h>
gint main(gint argc, gchar **argv)
{
GChecksum *checksum_a = NULL;
GChecksum *checksum_b = NULL;
GChecksum *checksum_ab1 = NULL;
GChecksum *checksum_ab2 = NULL;
checksum_a = g_checksum_new(G_CHECKSUM_SHA1);
g_checksum_update(checksum_a, "A", strlen("A"));
g_print("SHA1(A):%s \n", g_checksum_get_string(checksum_a));
g_checksum_free(checksum_a);
checksum_b = g_checksum_new(G_CHECKSUM_SHA1);
g_checksum_update(checksum_b, "B", strlen("B"));
g_print("SHA1(B):%s \n", g_checksum_get_string(checksum_b));
g_checksum_free(checksum_b);
checksum_ab1 = g_checksum_new(G_CHECKSUM_SHA1);
g_checksum_update(checksum_ab1, "AB", strlen("AB"));
g_print("SHA1(AB)(once):%s \n", g_checksum_get_string(checksum_ab1));
g_checksum_free(checksum_ab1);
checksum_ab2 = g_checksum_new(G_CHECKSUM_SHA1);
g_checksum_update(checksum_ab2, "A", strlen("A"));
//ERROR // g_print("SHA1(A):%s \n", g_checksum_get_string(checksum_ab2));
g_checksum_update(checksum_ab2, "B", strlen("B"));
g_print("SHA1(AB)(multi):%s \n", g_checksum_get_string(checksum_ab2));
g_checksum_free(checksum_ab2);
return 0;
}
运行结果:
[root@centos7_6 build]# ./glib_checksum_sha1
SHA1(A):6dcd4ce23d88e2ee9568ba546c007c63d9131c1b
SHA1(B):ae4f281df5a5d0ff3cad6371f76d5c29b6d953ec
SHA1(AB)(once):06d945942aa26a61be18c3e22bf19bbca8dd2b5d
SHA1(AB)(multi):06d945942aa26a61be18c3e22bf19bbca8dd2b5d