// 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; }
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; }
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; }
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); }
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); }
/* 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); }
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; }
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); }
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; }
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; }
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); }
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; }
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; }
// 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; }
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; }
void generate_md5_hashes(char* input_data, int count, unsigned char* result) { MD5_CTX md5; unsigned char md5_result[16] = {0}; int i; for(i = 0; i < count; i++) { MD5_Init(&md5); MD5_Update(&md5, input_data, strlen(input_data)); input_data += INPUT_MAX_LENGTH; MD5_Final(md5_result, &md5); memcpy(result, md5_result, 16); result += 16; } }
int ntlm_generate_signing_key(BYTE* exported_session_key, PSecBuffer sign_magic, BYTE* signing_key) { int length; BYTE* value; MD5_CTX md5; length = 16 + sign_magic->cbBuffer; value = (BYTE*) malloc(length); if (!value) return -1; /* Concatenate ExportedSessionKey with sign magic */ CopyMemory(value, exported_session_key, 16); CopyMemory(&value[16], sign_magic->pvBuffer, sign_magic->cbBuffer); MD5_Init(&md5); MD5_Update(&md5, value, length); MD5_Final(signing_key, &md5); free(value); return 1; }
LiveJournalRequest::LiveJournalRequest(LiveJournalClient *client, const char *mode) { m_client = client; m_buffer = new Buffer; addParam("mode", mode); if (client->data.owner.User.ptr) addParam("user", client->data.owner.User.ptr); MD5_CTX md5; MD5_Init(&md5); MD5_Update(&md5, client->getPassword().utf8(), strlen(client->getPassword().utf8())); unsigned char pass[MD5_DIGEST_LENGTH]; MD5_Final(pass, &md5); string hpass; for (unsigned i = 0; i < MD5_DIGEST_LENGTH; i++){ char b[5]; sprintf(b, "%02x", pass[i]); hpass += b; } addParam("hpassword", hpass.c_str()); }
ClientWardenModule* WardenWin::GetModuleForClient(WorldSession* /*session*/) { ClientWardenModule* mod = new ClientWardenModule; uint32 len = sizeof(Module_79C0768D657977D697E10BAD956CCED1_Data); // data assign mod->CompressedSize = len; mod->CompressedData = new uint8[len]; memcpy(mod->CompressedData, Module_79C0768D657977D697E10BAD956CCED1_Data, len); memcpy(mod->Key, Module_79C0768D657977D697E10BAD956CCED1_Key, 16); // md5 hash MD5_CTX ctx; MD5_Init(&ctx); MD5_Update(&ctx, mod->CompressedData, len); MD5_Final((uint8*)&mod->ID, &ctx); return mod; }
ClientWardenModule* WardenWin::GetModuleForClient() { ClientWardenModule *mod = new ClientWardenModule; uint32 length = sizeof(Module.Module); // data assign mod->CompressedSize = length; mod->CompressedData = new uint8[length]; memcpy(mod->CompressedData, Module.Module, length); memcpy(mod->Key, Module.ModuleKey, 16); // md5 hash MD5_CTX ctx; MD5_Init(&ctx); MD5_Update(&ctx, mod->CompressedData, length); MD5_Final((uint8*)&mod->Id, &ctx); return mod; }
/* * Selects the cipher, and keys if by computing the MD5 checksum of the * passphrase and using the resulting 16 bytes as the key. */ int cipher_set_key_string(struct sshcipher_ctx *cc, struct sshcipher *cipher, const char *pphrase, int do_encrypt) { MD5_CTX md; u_char digest[16]; int ret = SSH_ERR_LIBCRYPTO_ERROR; if (MD5_Init(&md) != 1 || MD5_Update(&md, (const u_char *)pphrase, strlen(pphrase)) != 1 || MD5_Final(digest, &md) != 1) goto out; ret = cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt); out: memset(digest, 0, sizeof(digest)); memset(&md, 0, sizeof(md)); return ret; }
static void ipb2_set_key(char *key, int index) { static unsigned char key_hash[MD5_BINARY_SIZE]; unsigned char *kh = key_hash; unsigned char *workspace_ptr = workspace + PROCESSED_SALT_SIZE; unsigned char v; int i; saved_key_len = strnfcpy_count(saved_key, key, PLAINTEXT_LENGTH); MD5_Init(&ctx); MD5_Update(&ctx, saved_key, saved_key_len); MD5_Final(key_hash, &ctx); for (i = 0; i < MD5_BINARY_SIZE; ++i) { v = *kh++; *workspace_ptr++ = itoa16_shr_04[ARCH_INDEX(v)]; *workspace_ptr++ = itoa16_and_0f[ARCH_INDEX(v)]; } }
ClientWardenModule *WardenMac::GetModuleForClient(WorldSession *session) { ClientWardenModule *mod = new ClientWardenModule; uint32 len = sizeof(Module_0DBBF209A27B1E279A9FEC5C168A15F7_Data); // data assign mod->CompressedSize = len; mod->CompressedData = new uint8[len]; memcpy(mod->CompressedData, Module_0DBBF209A27B1E279A9FEC5C168A15F7_Data, len); memcpy(mod->Key, Module_0DBBF209A27B1E279A9FEC5C168A15F7_Key, 16); // md5 hash MD5_CTX ctx; MD5_Init(&ctx); MD5_Update(&ctx, mod->CompressedData, len); MD5_Final((uint8*)&mod->ID, &ctx); return mod; }
std::string Digestor::HashPassword(const std::string& plain_pwd) { #if USE_SIMPLE_PWD_HASH return Md5str(plain_pwd); #else uint8_t sha512[64] = {0}; SHA512_CTX sha512_ctx; SHA512_Init(&sha512_ctx); SHA512_Update(&sha512_ctx, plain_pwd.c_str(), plain_pwd.size()); SHA512_Final(sha512, &sha512_ctx); uint8_t md5[16] = {0}; MD5_CTX md5_ctx; MD5_Init(&md5_ctx); MD5_Update(&md5_ctx, sha512, 64); MD5_Final(md5, &md5_ctx); return BinaryHashToHexString(md5, 16); #endif }
/* * Compute the MD5 checksum of a file. The md parameter must * point to a buffer containing at least MD5_DIGEST_SIZE bytes. * * Do not confuse OpenSSL's MD5_DIGEST_LENGTH with our own * MD5_DIGEST_SIZE macro. */ int MD5_File(char *path, char *md) { char buf[1024]; MD5_CTX ctx; ssize_t n; int fd; fd = open(path, O_RDONLY); if (fd == -1) return (-1); MD5_Init(&ctx); while ((n = read(fd, buf, sizeof(buf))) > 0) MD5_Update(&ctx, buf, n); close(fd); if (n == -1) return (-1); MD5_End(md, &ctx); return (0); }
static void client_chap(const void *server_nonce, size_t snoncelen, unsigned char server_identifier, const char *password) { MD5_CTX ctx; unsigned char md[MD5_DIGEST_LENGTH]; char *h; MD5_Init(&ctx); MD5_Update(&ctx, &server_identifier, 1); MD5_Update(&ctx, password, strlen(password)); MD5_Update(&ctx, server_nonce, snoncelen); MD5_Final(md, &ctx); hex_encode(md, 16, &h); printf("responseData=%s\n", h); free(h); }
int ssl3_PRF( const u_char* secret, uint32_t secret_len, const u_char* random1, uint32_t random1_len, const u_char* random2, uint32_t random2_len, u_char* out, uint32_t out_len ) { MD5_CTX md5; SHA_CTX sha; u_char buf[20]; uint32_t off; u_char i; if( !out ) return NM_ERROR( DSSL_E_INVALID_PARAMETER ); for( off=0, i = 1; off < out_len; off+=16, ++i ) { u_char md5_buf[16]; uint32_t cnt; uint32_t j; MD5_Init(&md5); SHA1_Init(&sha); /* salt: A, BB, CCC, ... */ for( j=0; j < i; j++ ) buf[j]='A' + (i-1); SHA1_Update( &sha, buf, i ); if( secret ) SHA1_Update( &sha, secret, secret_len ); SHA1_Update( &sha, random1, random1_len ); SHA1_Update( &sha, random2, random2_len ); SHA1_Final( buf, &sha ); MD5_Update( &md5, secret, secret_len ); MD5_Update( &md5, buf, 20 ); MD5_Final( md5_buf, &md5 ); cnt = out_len - off < 16 ? out_len - off : 16; memcpy( out + off, md5_buf, cnt ); } return DSSL_RC_OK; }
//Return value is on the heap. char* md5(const char *filename) { //Open the input file. FILE *in_file = fopen(filename, "rb"); if (!in_file) { printf ("%s can't be opened.\n", filename); return NULL; } //Initialise the md5 function. MD5_CTX md_context; MD5_Init (&md_context); //Read in the data from the file and feed it to the md5 function. int bytes; unsigned char data[1024]; while ((bytes = fread(data, 1, 1024, in_file)) != 0) { MD5_Update(&md_context, data, bytes); } //Recieve the final md5 value inside c. unsigned char c[MD5_DIGEST_LENGTH]; MD5_Final(c,&md_context); //Allocate memory for the return value. char tmp[2]; char *hash = malloc((MD5_DIGEST_LENGTH * 2) + 1); //Format the md5 digits as chars. for (int i = 0; i < MD5_DIGEST_LENGTH; i++) { sprintf(tmp, "%02x", c[i]); memcpy(hash+(i*2), tmp, 2); } hash[MD5_DIGEST_LENGTH*2] = '\0'; fclose (in_file); return hash; }
/* * Computes the proper response to a RSA challenge, and sends the response to * the server. */ static void respond_to_rsa_challenge(struct ssh *ssh, BIGNUM * challenge, RSA * prv) { u_char buf[32], response[16]; MD5_CTX md; int r, len; /* Decrypt the challenge using the private key. */ /* XXX think about Bleichenbacher, too */ if ((r = rsa_private_decrypt(challenge, challenge, prv)) != 0) { ssh_packet_disconnect(ssh, "%s: rsa_private_decrypt: %s", __func__, ssh_err(r)); } /* Compute the response. */ /* The response is MD5 of decrypted challenge plus session id. */ len = BN_num_bytes(challenge); if (len <= 0 || (u_int)len > sizeof(buf)) ssh_packet_disconnect(ssh, "respond_to_rsa_challenge: bad challenge length %d", len); memset(buf, 0, sizeof(buf)); BN_bn2bin(challenge, buf + sizeof(buf) - len); MD5_Init(&md); MD5_Update(&md, buf, 32); MD5_Update(&md, session_id, 16); MD5_Final(response, &md); debug("Sending response to host key RSA challenge."); /* Send the response back to the server. */ if ((r = sshpkt_start(ssh, SSH_CMSG_AUTH_RSA_RESPONSE)) != 0 || (r = sshpkt_put(ssh, &response, sizeof(response))) != 0 || (r = sshpkt_send(ssh)) != 0) fatal("%s: %s", __func__, ssh_err(r)); ssh_packet_write_wait(ssh); memset(buf, 0, sizeof(buf)); memset(response, 0, sizeof(response)); memset(&md, 0, sizeof(md)); }
int gen_md5(headers_t *headers, char *target) { unsigned long key1 = parse_hixie76_key(headers->key1); unsigned long key2 = parse_hixie76_key(headers->key2); char *key3 = headers->key3; MD5_CTX c; char in[HIXIE_MD5_DIGEST_LENGTH] = { key1 >> 24, key1 >> 16, key1 >> 8, key1, key2 >> 24, key2 >> 16, key2 >> 8, key2, key3[0], key3[1], key3[2], key3[3], key3[4], key3[5], key3[6], key3[7] }; MD5_Init(&c); MD5_Update(&c, (void *)in, sizeof in); MD5_Final((void *)target, &c); target[HIXIE_MD5_DIGEST_LENGTH] = '\0'; return 1; }