本文整理汇总了C++中MD5_Init函数的典型用法代码示例。如果您正苦于以下问题:C++ MD5_Init函数的具体用法?C++ MD5_Init怎么用?C++ MD5_Init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MD5_Init函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: cache_mng_get_md5
// we can only get md5 of an object containing 1 range
// XXX: move code to separate thread
gboolean cache_mng_get_md5 (CacheMng *cmng, fuse_ino_t ino, gchar **md5str)
{
struct _CacheEntry *entry;
unsigned char digest[MD5_DIGEST_LENGTH];
MD5_CTX md5ctx;
ssize_t bytes;
unsigned char data[1024];
char path[PATH_MAX];
size_t i;
gchar *out;
FILE *in;
entry = g_hash_table_lookup (cmng->h_entries, GUINT_TO_POINTER (ino));
if (!entry)
return FALSE;
if (range_count (entry->avail_range) != 1) {
LOG_debug (CMNG_LOG, INO_H"Entry contains more than 1 range, can't take MD5 sum of such object !", INO_T (ino));
return FALSE;
}
cache_mng_file_name (cmng, path, sizeof (path), ino);
in = fopen (path, "rb");
if (in == NULL) {
LOG_debug (CMNG_LOG, INO_H"Can't open file for reading: %s", INO_T (ino), path);
return FALSE;
}
MD5_Init (&md5ctx);
while ((bytes = fread (data, 1, 1024, in)) != 0)
MD5_Update (&md5ctx, data, bytes);
MD5_Final (digest, &md5ctx);
fclose (in);
out = g_malloc (33);
for (i = 0; i < 16; ++i)
sprintf (&out[i*2], "%02x", (unsigned int)digest[i]);
*md5str = out;
return TRUE;
}
示例2: lua_util_md5
static int lua_util_md5(lua_State* L)
{
const char* path=lua_tostring(L,1);
if(!path)
lua_pushnil(L);
else
{
FILE* fp=fopen(path,"rb");
if(!fp)
lua_pushnil(L);
else
{
MD5_CTX ctx;
MD5_Init(&ctx);
{
char tmp[512];
size_t n;
while((n=fread(tmp,1,sizeof(tmp),fp))>0)
MD5_Update(&ctx,(unsigned char*)tmp,n);
}
fclose(fp);
unsigned char tmp[16];
MD5_Final(tmp,&ctx);
char buf[sizeof(tmp)*2];
static const char hex[]="0123456789abcdef";
for(int i=0,j=0;i<sizeof(tmp);i++)
{
buf[j++]=hex[(tmp[i]>>4)&0x0f];
buf[j++]=hex[tmp[i]&0x0f];
}
lua_pushlstring(L,buf,sizeof(buf));
}
}
return 1;
}
示例3: get_part_cksum
static int get_part_cksum(struct file *f, FILE **fp)
{
MD5_CTX md5;
int got=0;
static char buf[PART_CHUNK];
unsigned char checksum[MD5_DIGEST_LENGTH+1];
if(*fp) fseek(*fp, 0, SEEK_SET);
else if(!(*fp=open_file(f)))
{
f->part_cksum=0;
return 0;
}
if(!MD5_Init(&md5))
{
logp("MD5_Init() failed\n");
return -1;
}
got=fread(buf, 1, PART_CHUNK, *fp);
if(!MD5_Update(&md5, buf, got))
{
logp("MD5_Update() failed\n");
return -1;
}
if(!MD5_Final(checksum, &md5))
{
logp("MD5_Final() failed\n");
return -1;
}
memcpy(&(f->part_cksum), checksum, sizeof(unsigned));
// Try for a bit of efficiency - no need to calculate the full checksum
// again if we already read the whole file.
if(got<PART_CHUNK) f->full_cksum=f->part_cksum;
return 0;
}
示例4: calc_hash
void calc_hash(const char *filename)
{
int n;
unsigned char data[BUFFSIZE];
FILE *file = fopen(filename, "rb");
if (file == NULL) {
fprintf(stderr, "'%s' ", filename);
perror("文件打开失败");
return;
}
unsigned char md5[MD5_DIGEST_LENGTH];
MD5_CTX md5_c;
unsigned char sha1[SHA_DIGEST_LENGTH];
SHA_CTX sha1_c;
unsigned char sha512[SHA_DIGEST_LENGTH];
SHA512_CTX sha512_c;
MD5_Init(&md5_c);
SHA1_Init(&sha1_c);
SHA512_Init(&sha512_c);
while ((n = fread(data, 1, BUFFSIZE, file)) != 0) {
MD5_Update(&md5_c, data, n);
SHA1_Update(&sha1_c, data, n);
SHA512_Update(&sha512_c, data, n);
}
MD5_Final(md5, &md5_c);
SHA1_Final(sha1, &sha1_c);
SHA512_Final(sha512, &sha512_c);
int i;
printf("%s", filename);
printf("\n%8s: ", "MD5");
for (i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", md5[i]);
printf("\n%8s: ", "SHA1");
for (i = 0; i < SHA_DIGEST_LENGTH; i++) printf("%02x", sha1[i]);
printf("\n%8s: ", "SHA512");
for (i = 0; i < SHA_DIGEST_LENGTH; i++) printf("%02x", sha512[i]);
printf("\n");
fclose(file);
}
示例5: do_update
int32_t do_update(const std::string& _file, const std::string& _md5, const update_params& _params)
{
http_request_simple request(core::proxy_settings(), _params.is_stoped_);
request.set_url(get_update_server() + _file);
request.set_need_log(false);
if (!request.get())
return -1;
int32_t http_code = (uint32_t)request.get_response_code();
if (http_code != 200)
return -1;
auto response = request.get_response();
if (!response->available())
return -1;
int32_t size = response->available();
MD5_CTX md5handler;
unsigned char md5digest[MD5_DIGEST_LENGTH];
MD5_Init(&md5handler);
MD5_Update(&md5handler, response->read(size), size);
MD5_Final(md5digest,&md5handler);
char buffer[100];
std::string md5;
for (int32_t i = 0; i < MD5_DIGEST_LENGTH; i++)
{
sprintf(buffer, "%02x", md5digest[i]);
md5 += buffer;
}
if (md5 != _md5)
return -1;
response->reset_out();
return run_installer(*response);
}
示例6: rsync_nextblock
/* Get the next rsync block of a file. */
int
rsync_nextblock(struct rsyncfile *rf)
{
MD5_CTX ctx;
size_t blocksize;
if (rf->blockptr >= rf->end)
return (0);
blocksize = min((size_t)(rf->end - rf->blockptr), rf->blocksize);
/* Calculate MD5 of the block. */
MD5_Init(&ctx);
MD5_Update(&ctx, rf->blockptr, blocksize);
MD5_End(rf->blockmd5, &ctx);
rf->rsum = rsync_rollsum(rf->blockptr, blocksize);
snprintf(rf->rsumstr, RSUM_SIZE, "%x", rf->rsum);
rf->blocknum++;
rf->blockptr += blocksize;
return (1);
}
示例7: tc_digest
std::string tc_digest(
const char *algo,
const char *ibytes,
uint32_t nibytes)
{
std::string ret("");
if (algo && !strcmp(algo, "md5")) {
uint8_t obytes[MD5_DIGEST_LENGTH];
memset(obytes, 0, MD5_DIGEST_LENGTH);
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, ibytes, nibytes);
MD5_Final(obytes, &ctx);
char ret_cstr[2*MD5_DIGEST_LENGTH+1];
for(uint32_t i=0; i < MD5_DIGEST_LENGTH; i++)
sprintf(ret_cstr+2*i, "%02x", obytes[i]);
ret = ret_cstr;
}
return ret;
}
示例8: blob_set_path_hash
void blob_set_path_hash(blob_t *blob, const uint8_t *data, size_t size)
{
uint8_t hash[16];
char path[256];
char *p;
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, data, size);
MD5_Final(hash, &ctx);
strncpy(path, "crypto/", sizeof(path));
p = strrchr(path, '/') + 1;
for (size_t i = 0; i < sizeof(hash); i++) {
snprintf(p, 3, "%02X", hash[i]);
p += 2;
}
strncpy(p, ".bin", sizeof(path) - (p - &path[0]));
blob_set_path(blob, path);
}
示例9: calculate_md5
static int calculate_md5(char *str, const char *path) {
FILE *fd;
fd = fopen(path, "r");
if (fd != NULL) {
MD5_CTX c;
size_t i;
static unsigned char buf[BUFSIZE];
unsigned char md5dig[MD5_DIGEST_LENGTH];
MD5_Init(&c);
while ((i = fread(buf, 1, BUFSIZE, fd)) > 0)
MD5_Update(&c, buf, i);
MD5_Final(&(md5dig[0]), &c);
fclose(fd);
to_md5_hash(str, md5dig);
} else {
return 1;
}
return 0;
}
示例10: lua_md5
static int lua_md5(lua_State* L)
{
size_t l=0;
const char* s=luaL_checklstring(L,1,&l);
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx,(unsigned char*)s,l);
unsigned char md[16];
MD5_Final(md,&ctx);
char buf[sizeof(md)*2];
to_hex(md,buf,sizeof(md));
lua_pushlstring(L,buf,sizeof(buf));
return 1;
}
示例11: MD5_Init
static char *md5_fd(int fd)
{
MD5_CTX ctx;
unsigned char buf[BUFSIZ], output[16];
ssize_t n;
MD5_Init(&ctx);
while ((n = read(fd, buf, sizeof(buf))) > 0 || errno == EINTR) {
if (n < 0)
continue;
MD5_Update(&ctx, buf, n);
}
if (n < 0)
return NULL;
MD5_Final(output, &ctx);
return hex_representation(output, 16);
}
示例12: md5_identity
char * md5_identity(void *data, int len)
{
unsigned char hash[16];
char* result;
int ptr,i;
result = (char*)malloc(sizeof(char)*512);
memset(result, 0, sizeof(result));
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx,(char*)data,len);
MD5_Final(hash,&ctx);
ptr = 0;
for(i=0;i<16;i++)
{
ptr += sprintf(result+ptr, "%02x", hash[i]);
}
return result;
}
示例13: md5sum
string md5sum(const char * str, int len)
{
int n;
MD5_CTX ctx;
char buf[SLICECAP];
unsigned char out[MD5_DIGEST_LENGTH];
string md5str;
MD5_Init(&ctx);
MD5_Update(&ctx, str, len);
MD5_Final(out, &ctx);
for(n = 0; n< MD5_DIGEST_LENGTH; n++)
{
snprintf(buf, SLICECAP, "%02x", out[n]);
md5str += buf;
}
return md5str;
}
示例14: get_signature
// computes an RSA-MD5 signature over 'buf' using 'priv_key'
// signature data is returned is 'sig_buf' which should be at least
// SIGNATURE_LEN bytes in size.
// Method returns 0 on success.
int get_signature(char *buf, unsigned int buf_size, RSA *priv_key,
unsigned char *sig_buf, unsigned int *sig_len) {
unsigned char digest[16];
unsigned int digest_len = 16;
MD5_CTX md5;
MD5_Init(&md5);
MD5_Update(&md5, (unsigned char*)buf, buf_size);
MD5_Final(digest, &md5);
int ret_val = RSA_sign(NID_md5, digest, digest_len,
sig_buf, sig_len, priv_key);
if(!ret_val) {
unsigned long e = ERR_get_error();
DPRINTF(DEBUG_ERROR,"RSA_sign error: %s", ERR_error_string(e, buf));
return 1;
}
return 0;
}
示例15: main
int main( int argc, char **argv )
{
MD5_CTX ctx;
unsigned char *data="123";
unsigned char md[16];
char buf[33]={'\0'};
char tmp[3]={'\0'};
int i;
MD5_Init(&ctx);
MD5_Update(&ctx,data,strlen(data));
MD5_Final(md,&ctx);
for( i=0; i<16; i++ ){
sprintf(tmp,"%02X",md[i]);
strcat(buf,tmp);
}
printf("%s\n",buf);
return 0;
}