libgcrypt使用舉例1,計算輸入字元串的sha-1值:
#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 <string.h>
#include <errno.h>
#include <signal.h>
#ifdef HAVE_OPENSSL
#include <openssl/sha.h>
#else
#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)
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)
{
SHA1((const unsigned char*)input, size, hash_out);
gcry_md_hash_buffer(GCRY_MD_SHA1, hash_out, input, size);
static int compare_hash(const unsigned char *hash1, const unsigned char *hash2, int hash_len)
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)
SHA_CTX sha1;
SHA1_Init(&sha1);
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);
FILE *f = fopen(path, "rb");
if (f) {
unsigned char buf[16384];
size_t len;
while ((len = fread(buf, 1, 16384, f)) > 0) {
SHA1_Update(&sha1, buf, len);
gcry_md_write(hd, buf, len);
fclose(f);
SHA1_Update(&sha1, destpath, strlen(destpath));
SHA1_Update(&sha1, ";", 1);
gcry_md_write(hd, destpath, strlen(destpath));
gcry_md_write(hd, ";", 1);
if (greylist == 1) {
SHA1_Update(&sha1, "true", 4);
gcry_md_write(hd, "true", 4);
} else {
SHA1_Update(&sha1, "false", 5);
gcry_md_write(hd, "false", 5);
if (domain) {
SHA1_Update(&sha1, domain, strlen(domain));
gcry_md_write(hd, domain, strlen(domain));
SHA1_Update(&sha1, "(null)", 6);
gcry_md_write(hd, "(null)", 6);
if (appid) {
SHA1_Update(&sha1, appid, strlen(appid));
gcry_md_write(hd, appid, strlen(appid));
if (version) {
SHA1_Update(&sha1, version, strlen(version));
gcry_md_write(hd, version, strlen(version));
SHA1_Final(hash_out, &sha1);
unsigned char *newhash = gcry_md_read(hd, GCRY_MD_SHA1);
memcpy(hash_out, newhash, 20);
#ifndef HAVE_OPENSSL
gcry_md_close(hd);
static void print_hash(const unsigned char *hash, int len)
for (i = 0; i < len; i++) {
printf("%02x", hash[i]);
......
本文轉自 h2appy 51CTO部落格,原文連結:http://blog.51cto.com/h2appy/1022006,如需轉載請自行聯系原作者