libgcrypt使用举例
2012-10-12 11:33
405 查看
libgcrypt使用举例1,计算输入字符串的sha-1值:
libgcrypt使用举例2,计算文件的sha-1值,下面代码来自http://www.libimobiledevice.org/:
本文出自 “GONE WITH THE WIND” 博客,请务必保留此出处http://h2appy.blog.51cto.com/609721/1022006
#include <gcrypt.h> #include <stdio.h> #include <stdlib.h> // compile with: // // gcc sha1.c -lstdc++ -lgcrypt -lgpg-error -I/local/include -L/local/lib -o sha1 // // // Example run: // // thomas@t40$ ./sha1 foo // 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33 // thomas@t40$ echo -n foo | sha1sum // 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33 - int main(int argc, char **argv){ /* Test for arg string */ if ( argc < 2 ){ fprintf( stderr, "Usage: %s <string>\n", argv[0] ); exit( 1 ); } /* Length of message to encrypt */ int msg_len = strlen( argv[1] ); /* Length of resulting sha1 hash - gcry_md_get_algo_dlen * returns digest lenght for an algo */ int hash_len = gcry_md_get_algo_dlen( GCRY_MD_SHA1 ); /* output sha1 hash - this will be binary data */ unsigned char hash[ hash_len ]; /* output sha1 hash - converted to hex representation * 2 hex digits for every byte + 1 for trailing \0 */ char *out = (char *) malloc( sizeof(char) * ((hash_len*2)+1) ); char *p = out; /* calculate the SHA1 digest. This is a bit of a shortcut function * most gcrypt operations require the creation of a handle, etc. */ gcry_md_hash_buffer( GCRY_MD_SHA1, hash, argv[1], msg_len ); /* Convert each byte to its 2 digit ascii * hex representation and place in out */ int i; for ( i = 0; i < hash_len; i++, p += 2 ) { snprintf ( p, 3, "%02x", hash[i] ); } printf( "%s\n", out ); free( out ); }
libgcrypt使用举例2,计算文件的sha-1值,下面代码来自http://www.libimobiledevice.org/:
#ifdef HAVE_CONFIG_H #include <config.h> #endif #include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <signal.h> #ifdef HAVE_OPENSSL #include <openssl/sha.h> #else #include <gcrypt.h> #endif #include <unistd.h> #include <ctype.h> #include <time.h> #include <libimobiledevice/libimobiledevice.h> #include <libimobiledevice/lockdown.h> #include <libimobiledevice/mobilebackup.h> #include <libimobiledevice/notification_proxy.h> #include <libimobiledevice/afc.h> #define MOBILEBACKUP_SERVICE_NAME "com.apple.mobilebackup" #define NP_SERVICE_NAME "com.apple.mobile.notification_proxy" #define LOCK_ATTEMPTS 50 #define LOCK_WAIT 200000 #ifdef WIN32 #define sleep(x) Sleep(x*1000) #endif static mobilebackup_client_t mobilebackup = NULL; static lockdownd_client_t client = NULL; static idevice_t phone = NULL; static int quit_flag = 0; enum cmd_mode { CMD_BACKUP, CMD_RESTORE, CMD_LEAVE }; enum plist_format_t { PLIST_FORMAT_XML, PLIST_FORMAT_BINARY }; enum device_link_file_status_t { DEVICE_LINK_FILE_STATUS_NONE = 0, DEVICE_LINK_FILE_STATUS_HUNK, DEVICE_LINK_FILE_STATUS_LAST_HUNK }; static void sha1_of_data(const char *input, uint32_t size, unsigned char *hash_out) { #ifdef HAVE_OPENSSL SHA1((const unsigned char*)input, size, hash_out); #else gcry_md_hash_buffer(GCRY_MD_SHA1, hash_out, input, size); #endif } static int compare_hash(const unsigned char *hash1, const unsigned char *hash2, int hash_len) { int i; for (i = 0; i < hash_len; i++) { if (hash1[i] != hash2[i]) { return 0; } } return 1; } static void compute_datahash(const char *path, const char *destpath, uint8_t greylist, const char *domain, const char *appid, const char *version, unsigned char *hash_out) { #ifdef HAVE_OPENSSL SHA_CTX sha1; SHA1_Init(&sha1); #else gcry_md_hd_t hd = NULL; gcry_md_open(&hd, GCRY_MD_SHA1, 0); if (!hd) { printf("ERROR: Could not initialize libgcrypt/SHA1\n"); return; } gcry_md_reset(hd); #endif FILE *f = fopen(path, "rb"); if (f) { unsigned char buf[16384]; size_t len; while ((len = fread(buf, 1, 16384, f)) > 0) { #ifdef HAVE_OPENSSL SHA1_Update(&sha1, buf, len); #else gcry_md_write(hd, buf, len); #endif } fclose(f); #ifdef HAVE_OPENSSL SHA1_Update(&sha1, destpath, strlen(destpath)); SHA1_Update(&sha1, ";", 1); #else gcry_md_write(hd, destpath, strlen(destpath)); gcry_md_write(hd, ";", 1); #endif if (greylist == 1) { #ifdef HAVE_OPENSSL SHA1_Update(&sha1, "true", 4); #else gcry_md_write(hd, "true", 4); #endif } else { #ifdef HAVE_OPENSSL SHA1_Update(&sha1, "false", 5); #else gcry_md_write(hd, "false", 5); #endif } #ifdef HAVE_OPENSSL SHA1_Update(&sha1, ";", 1); #else gcry_md_write(hd, ";", 1); #endif if (domain) { #ifdef HAVE_OPENSSL SHA1_Update(&sha1, domain, strlen(domain)); #else gcry_md_write(hd, domain, strlen(domain)); #endif } else { #ifdef HAVE_OPENSSL SHA1_Update(&sha1, "(null)", 6); #else gcry_md_write(hd, "(null)", 6); #endif } #ifdef HAVE_OPENSSL SHA1_Update(&sha1, ";", 1); #else gcry_md_write(hd, ";", 1); #endif if (appid) { #ifdef HAVE_OPENSSL SHA1_Update(&sha1, appid, strlen(appid)); #else gcry_md_write(hd, appid, strlen(appid)); #endif } else { #ifdef HAVE_OPENSSL SHA1_Update(&sha1, "(null)", 6); #else gcry_md_write(hd, "(null)", 6); #endif } #ifdef HAVE_OPENSSL SHA1_Update(&sha1, ";", 1); #else gcry_md_write(hd, ";", 1); #endif if (version) { #ifdef HAVE_OPENSSL SHA1_Update(&sha1, version, strlen(version)); #else gcry_md_write(hd, version, strlen(version)); #endif } else { #ifdef HAVE_OPENSSL SHA1_Update(&sha1, "(null)", 6); #else gcry_md_write(hd, "(null)", 6); #endif } #ifdef HAVE_OPENSSL SHA1_Final(hash_out, &sha1); #else unsigned char *newhash = gcry_md_read(hd, GCRY_MD_SHA1); memcpy(hash_out, newhash, 20); #endif } #ifndef HAVE_OPENSSL gcry_md_close(hd); #endif } static void print_hash(const unsigned char *hash, int len) { int i; for (i = 0; i < len; i++) { printf("%02x", hash[i]); } } ......
本文出自 “GONE WITH THE WIND” 博客,请务必保留此出处http://h2appy.blog.51cto.com/609721/1022006
相关文章推荐
- libgcrypt使用举例
- 内置函数使用举例
- 举例详解AngularJS中ngShow和ngHide的使用方法
- 举例说明如何使用正则表达式去掉字符串中的一些特殊符号
- Android ListView/GridView onItemClick函数参数说明及使用举例
- 举例:MySQL 使用DQL命令查询数据
- 内存泄漏检测工具VLD在VS2010中的使用举例
- std::string使用举例
- 举例详解CSS的z-index属性的使用
- 【面试论述】const的作用、使用举例与#define联系
- location.href 在JS中是 什么意思 怎么使用它 举例说明谢谢了,大神帮忙啊
- 构建器使用举例-静态工厂和构造器的替代方法
- Linux Yum 命令使用举例(很全很实用)
- fputs_fgets使用举例
- Labview示例查找使用举例
- 同步和异步有何异同,什么场景使用,举例说明!
- redis命令详解与使用场景举例——Set(集合)
- visual studio 之 mysql 的配置及使用举例
- ffmpeg教程五 —— 使用举例
- 举例详解Python中smtplib模块处理电子邮件的使用