libgcrypt使用举例

来源:互联网 发布:辐射4捏脸数据 美女 编辑:程序博客网 时间:2024/05/21 17:15
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://h2appy.blog.51cto.com/609721/1022006

libgcrypt使用举例1,计算输入字符串的sha-1值:

  1. #include <gcrypt.h> 
  2. #include <stdio.h> 
  3. #include <stdlib.h> 
  4.  
  5. // compile with: 
  6. //  
  7. // gcc sha1.c -lstdc++ -lgcrypt -lgpg-error -I/local/include -L/local/lib -o sha1  
  8. //  
  9. //  
  10. // Example run: 
  11. //  
  12. // thomas@t40$ ./sha1 foo 
  13. // 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33 
  14. // thomas@t40$ echo -n foo | sha1sum 
  15. // 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33 - 
  16.  
  17. int main(int argc, char **argv){ 
  18.     /* Test for arg string */ 
  19.     if ( argc < 2 ){ 
  20.         fprintf( stderr, "Usage: %s <string>\n", argv[0] ); 
  21.         exit( 1 ); 
  22.     } 
  23.  
  24.     /* Length of message to encrypt */ 
  25.     int msg_len = strlen( argv[1] ); 
  26.  
  27.     /* Length of resulting sha1 hash - gcry_md_get_algo_dlen 
  28.     * returns digest lenght for an algo */ 
  29.     int hash_len = gcry_md_get_algo_dlen( GCRY_MD_SHA1 ); 
  30.  
  31.     /* output sha1 hash - this will be binary data */ 
  32.     unsigned char hash[ hash_len ]; 
  33.  
  34.     /* output sha1 hash - converted to hex representation 
  35.     * 2 hex digits for every byte + 1 for trailing \0 */ 
  36.     char *out = (char *) malloc( sizeof(char) * ((hash_len*2)+1) ); 
  37.     char *p = out; 
  38.  
  39.     /* calculate the SHA1 digest. This is a bit of a shortcut function 
  40.     * most gcrypt operations require the creation of a handle, etc. */ 
  41.     gcry_md_hash_buffer( GCRY_MD_SHA1, hash, argv[1], msg_len ); 
  42.  
  43.     /* Convert each byte to its 2 digit ascii 
  44.     * hex representation and place in out */ 
  45.     int i; 
  46.     for ( i = 0; i < hash_len; i++, p += 2 ) { 
  47.         snprintf ( p, 3, "%02x", hash[i] ); 
  48.     } 
  49.  
  50.     printf( "%s\n", out ); 
  51.     free( out ); 

 

libgcrypt使用举例2,计算文件的sha-1值,下面代码来自http://www.libimobiledevice.org/:

 

  1. #ifdef HAVE_CONFIG_H 
  2. #include <config.h> 
  3. #endif 
  4.  
  5. #include <stdio.h> 
  6. #include <string.h> 
  7. #include <errno.h> 
  8. #include <stdlib.h> 
  9. #include <signal.h> 
  10. #ifdef HAVE_OPENSSL 
  11. #include <openssl/sha.h> 
  12. #else 
  13. #include <gcrypt.h> 
  14. #endif 
  15. #include <unistd.h> 
  16. #include <ctype.h> 
  17. #include <time.h> 
  18.  
  19. #include <libimobiledevice/libimobiledevice.h> 
  20. #include <libimobiledevice/lockdown.h> 
  21. #include <libimobiledevice/mobilebackup.h> 
  22. #include <libimobiledevice/notification_proxy.h> 
  23. #include <libimobiledevice/afc.h> 
  24.  
  25. #define MOBILEBACKUP_SERVICE_NAME "com.apple.mobilebackup" 
  26. #define NP_SERVICE_NAME "com.apple.mobile.notification_proxy" 
  27.  
  28. #define LOCK_ATTEMPTS 50 
  29. #define LOCK_WAIT 200000 
  30.  
  31. #ifdef WIN32 
  32. #define sleep(x) Sleep(x*1000) 
  33. #endif 
  34.  
  35. static mobilebackup_client_t mobilebackup = NULL; 
  36. static lockdownd_client_t client = NULL; 
  37. static idevice_t phone = NULL; 
  38.  
  39. static int quit_flag = 0; 
  40.  
  41. enum cmd_mode { 
  42.     CMD_BACKUP, 
  43.     CMD_RESTORE, 
  44.     CMD_LEAVE 
  45. }; 
  46.  
  47. enum plist_format_t { 
  48.     PLIST_FORMAT_XML, 
  49.     PLIST_FORMAT_BINARY 
  50. }; 
  51.  
  52. enum device_link_file_status_t { 
  53.     DEVICE_LINK_FILE_STATUS_NONE = 0, 
  54.     DEVICE_LINK_FILE_STATUS_HUNK, 
  55.     DEVICE_LINK_FILE_STATUS_LAST_HUNK 
  56. }; 
  57.  
  58. static void sha1_of_data(const char *input, uint32_t size, unsigned char *hash_out) 
  59. #ifdef HAVE_OPENSSL 
  60.     SHA1((const unsigned char*)input, size, hash_out); 
  61. #else 
  62.     gcry_md_hash_buffer(GCRY_MD_SHA1, hash_out, input, size); 
  63. #endif 
  64.  
  65. static int compare_hash(const unsigned char *hash1, const unsigned char *hash2, int hash_len) 
  66.     int i; 
  67.     for (i = 0; i < hash_len; i++) { 
  68.         if (hash1[i] != hash2[i]) { 
  69.             return 0; 
  70.         } 
  71.     } 
  72.     return 1; 
  73.  
  74. 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) 
  75. #ifdef HAVE_OPENSSL 
  76.     SHA_CTX sha1; 
  77.     SHA1_Init(&sha1); 
  78. #else 
  79.     gcry_md_hd_t hd = NULL; 
  80.     gcry_md_open(&hd, GCRY_MD_SHA1, 0); 
  81.     if (!hd) { 
  82.         printf("ERROR: Could not initialize libgcrypt/SHA1\n"); 
  83.         return
  84.     } 
  85.     gcry_md_reset(hd); 
  86. #endif 
  87.     FILE *f = fopen(path, "rb"); 
  88.     if (f) { 
  89.         unsigned char buf[16384]; 
  90.         size_t len; 
  91.         while ((len = fread(buf, 1, 16384, f)) > 0) { 
  92. #ifdef HAVE_OPENSSL 
  93.             SHA1_Update(&sha1, buf, len); 
  94. #else 
  95.             gcry_md_write(hd, buf, len); 
  96. #endif 
  97.         } 
  98.         fclose(f); 
  99. #ifdef HAVE_OPENSSL 
  100.         SHA1_Update(&sha1, destpath, strlen(destpath)); 
  101.         SHA1_Update(&sha1, ";", 1); 
  102. #else 
  103.         gcry_md_write(hd, destpath, strlen(destpath)); 
  104.         gcry_md_write(hd, ";", 1); 
  105. #endif 
  106.         if (greylist == 1) { 
  107. #ifdef HAVE_OPENSSL 
  108.             SHA1_Update(&sha1, "true", 4); 
  109. #else 
  110.             gcry_md_write(hd, "true", 4); 
  111. #endif 
  112.         } else { 
  113. #ifdef HAVE_OPENSSL 
  114.             SHA1_Update(&sha1, "false", 5); 
  115. #else 
  116.             gcry_md_write(hd, "false", 5); 
  117. #endif 
  118.         } 
  119. #ifdef HAVE_OPENSSL 
  120.         SHA1_Update(&sha1, ";", 1); 
  121. #else 
  122.         gcry_md_write(hd, ";", 1); 
  123. #endif 
  124.         if (domain) { 
  125. #ifdef HAVE_OPENSSL 
  126.             SHA1_Update(&sha1, domain, strlen(domain)); 
  127. #else 
  128.             gcry_md_write(hd, domain, strlen(domain)); 
  129. #endif 
  130.         } else { 
  131. #ifdef HAVE_OPENSSL 
  132.             SHA1_Update(&sha1, "(null)", 6); 
  133. #else 
  134.             gcry_md_write(hd, "(null)", 6); 
  135. #endif 
  136.         } 
  137. #ifdef HAVE_OPENSSL 
  138.         SHA1_Update(&sha1, ";", 1); 
  139. #else 
  140.         gcry_md_write(hd, ";", 1); 
  141. #endif 
  142.         if (appid) { 
  143. #ifdef HAVE_OPENSSL 
  144.             SHA1_Update(&sha1, appid, strlen(appid)); 
  145. #else 
  146.             gcry_md_write(hd, appid, strlen(appid)); 
  147. #endif 
  148.         } else { 
  149. #ifdef HAVE_OPENSSL 
  150.             SHA1_Update(&sha1, "(null)", 6); 
  151. #else 
  152.             gcry_md_write(hd, "(null)", 6); 
  153. #endif 
  154.         } 
  155. #ifdef HAVE_OPENSSL 
  156.         SHA1_Update(&sha1, ";", 1); 
  157. #else 
  158.         gcry_md_write(hd, ";", 1); 
  159. #endif 
  160.         if (version) { 
  161. #ifdef HAVE_OPENSSL 
  162.             SHA1_Update(&sha1, version, strlen(version)); 
  163. #else 
  164.             gcry_md_write(hd, version, strlen(version)); 
  165. #endif 
  166.         } else { 
  167. #ifdef HAVE_OPENSSL 
  168.             SHA1_Update(&sha1, "(null)", 6); 
  169. #else 
  170.             gcry_md_write(hd, "(null)", 6); 
  171. #endif 
  172.         } 
  173. #ifdef HAVE_OPENSSL 
  174.         SHA1_Final(hash_out, &sha1); 
  175. #else 
  176.         unsigned char *newhash = gcry_md_read(hd, GCRY_MD_SHA1); 
  177.         memcpy(hash_out, newhash, 20); 
  178. #endif 
  179.     } 
  180. #ifndef HAVE_OPENSSL 
  181.     gcry_md_close(hd); 
  182. #endif 
  183.  
  184. static void print_hash(const unsigned char *hash, int len) 
  185.     int i; 
  186.     for (i = 0; i < len; i++) { 
  187.         printf("%02x", hash[i]); 
  188.     } 
  189. ...... 

 

本文出自 “GONE WITH THE WIND” 博客,请务必保留此出处http://h2appy.blog.51cto.com/609721/1022006

0 0
原创粉丝点击