2014年01月13日 星期一 10:48
APR的md5模块提供了计算MD5值的基本方法。针对小文件,可以采用简单的One Step方式(apr_md5),针对大文件,可以采取分步方式。
编程示例如下:
#include <stdio.h>
#include <apr.h>
#include <apr_pools.h>
#include <apr_errno.h>
#include <apr_strings.h>
#include <apr_md5.h>
#include <apr_file_io.h>
void apr_err(const char *s, apr_status_t rv)
{
char buf[120];
fprintf(stderr, "%s: %s (%d)\n", s,
apr_strerror(rv, buf, sizeof buf), rv);
}
int main(int argc,char **argv) {
apr_initialize();
apr_pool_t *pool;
apr_pool_create(&pool,NULL);
apr_status_t st;
char *filename;
if(argc > 1) {
filename=argv[1];
} else {
filename="apr_md5.c";
}
apr_md5_ctx_t md5;
apr_md5_init(&md5);
apr_file_t *file;
st=apr_file_open(&file,filename,
APR_READ,APR_REG,pool);
if(st != APR_SUCCESS) {
apr_err("apr_file_open()",st);
return st;
}
apr_size_t n=1024;
char *buf=apr_pcalloc(pool,n);
while(APR_EOF != apr_file_eof(file)) {
n=1024;
st=apr_file_read(file,buf,&n);
apr_md5_update(&md5,buf,n);
}
unsigned char result[APR_MD5_DIGESTSIZE];
apr_md5_final(result,&md5);
for(int i=0;i<APR_MD5_DIGESTSIZE;i++) {
printf("%02x",result[i]);
}
printf("\n");
apr_off_t size=0;
apr_file_seek(file,APR_END,&size);
char *content=apr_pcalloc(pool,size);
apr_off_t nsize=0;
apr_file_seek(file,APR_SET,&nsize);
apr_file_read(file,content,&size);
apr_md5(result,content,size);
for(int i=0;i<APR_MD5_DIGESTSIZE;i++) {
printf("%02x",result[i]);
}
printf("\n");
apr_file_close(file);
apr_pool_destroy(pool);
apr_terminate();
return 0;
}
参考资料:
http://apr.apache.org/docs/apr-util/1.3/group___a_p_r___m_d5.html
http://apr.apache.org/docs/apr/1.3/group__apr__file__io.html
Zeuux © 2025
京ICP备05028076号