libgcrypt使用举例
2017-05-09 16:16
465 查看
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://h2appy.blog.51cto.com/609721/1022006
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 <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使用举例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 <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使用举例
- 常用struts标签使用举例--html篇(转载)
- Iptables 命令使用举例
- 深入WPF中的图像画刷(ImageBrush)之1——ImageBrush使用举例
- (转)【Hibernate总结系列】使用举例
- 【开发框架】struts标签使用举例-logic
- (转)【Hibernate总结系列】使用举例
- 举例说明C++回调函数(callback)的使用 - 转帖
- 举例说明如何在C#中使用C/C++写的DLL
- struts标签使用举例-logic
- v$session_event 使用举例
- CruiseControl 2.6.2简介及使用举例
- 常用struts标签使用举例--logic篇
- java.util.regex.Pattern类使用举例
- struts标签使用举例--logic篇
- freebsd ports 使用举例:安装 mysql
- 使用easymock简单举例
- 常用struts标签使用举例--logic篇 (转载)
- MapX SetKeyField及GetKeyValue使用举例。
- (转)【Hibernate总结系列】使用举例