void hash_given_data( void* data, size_t data_size) { MHASH td; byte _res[20]; int i; td = mhash_init( MHASH_SHA1); if (td==MHASH_FAILED) { err_quit(_("mhash_init() failed.")); } /* also hash the pool */ mhash( td, rnd_pool, 20); mhash(td, data, data_size); mhash_deinit( td, _res); /* addition may do as well as xor */ for(i=0;i<20;i++) rnd_pool[i] ^= _res[i]; /* Step 1 was completed. The pool was updated. */ }
int conf_input_wrapper(char* buf, int max_size, FILE* in) { int retval=0; int c=0; char* tmp=NULL; void* key=NULL; int keylen=0; /* FIXME Add support for gzipped config. :) */ #ifdef WITH_MHASH /* Read a character at a time until we are doing md */ if(conf->do_configmd){ retval=fread(buf,1,max_size,in); }else { c=fgetc(in); retval= (c==EOF) ? 0 : (buf[0] = c,1); } #else retval=fread(buf,1,max_size,in); #endif #ifdef WITH_MHASH if(conf->do_configmd||conf->config_check){ if(((conf->do_configmd==1)&&conf->config_check)||!conf->confmd){ if(conf->do_configmd==1){ conf->do_configmd+=1; } if((key=get_conf_key())!=NULL){ keylen=get_conf_key_len(); if( (conf->confmd= mhash_hmac_init(conf->confhmactype, key, keylen, mhash_get_hash_pblock(conf->confhmactype)))== MHASH_FAILED){ error(0, "mhash_hmac_init() failed for %i for config check. Aborting\n", conf->confhmactype); exit(EXIT_FAILURE); } } else { conf->do_configmd=0; return retval; } } /* FIXME This does not handle the case that @@end_config is on buffer boundary. */ if((tmp=strnstr(buf,"@@end_config",retval))!=NULL){ /* We have end of config don't feed the last line to mhash */ mhash(conf->confmd,(void*)buf,tmp-buf); } else { mhash(conf->confmd,(void*)buf,retval); } } #endif return retval; }
int rasqal_digest_buffer(rasqal_digest_type type, const unsigned char *output, const unsigned char * const input, size_t len) { hashid hash_type; unsigned int output_len; MHASH m; if(type > RASQAL_DIGEST_LAST) return -1; hash_type = rasqal_digest_to_hashid[type]; if(hash_type == (hashid)-1) return -1; output_len = mhash_get_block_size(hash_type); if(!input) return output_len; m = mhash_init(hash_type); if(m == MHASH_FAILED) return -1; mhash(m, (const void *)input, len); mhash_deinit(m, (void*)output); return output_len; }
static void digest_update(PX_MD * h, const uint8 *data, unsigned dlen) { MHASH mh = (MHASH) h->p.ptr; mhash(mh, data, dlen); }
/* create md5 hash from input string * returns NULL on failure * user must free returned string */ char * Stats::Md5Hash(const char *thread) { MHASH td; char *tmpcstr; int i; unsigned char *hash; char *rethash; td = mhash_init(MHASH_MD5); if (td == MHASH_FAILED) return NULL; if (thread==NULL) return NULL; mhash(td, thread, strlen(thread)); //mhash_deinit(td, hash); hash = (unsigned char*) mhash_end(td); rethash = (char*) calloc(mhash_get_block_size(MHASH_MD5)*2+1, sizeof(char)); for (i = 0; i < mhash_get_block_size(MHASH_MD5); i++) { sprintf(&rethash[i*2], "%.2x", hash[i]); } return rethash; }
/* test HMAC */ int verify_hmac(const void *data, size_t datalen, hashid hid, unsigned char *hmac, char *key, int keylen) { MHASH td; unsigned char *our_hmac = NULL; int rc = -1; td = mhash_hmac_init(hid, key, keylen, mhash_get_hash_pblock(hid)); if (!td) return -1; our_hmac = malloc(mhash_get_block_size(hid)); if (!our_hmac) return -1; (void)mhash(td, data, datalen); if (mhash_hmac_deinit(td, our_hmac)) goto out_free; rc = memcmp(our_hmac, hmac, mhash_get_block_size(hid)); out_free: if (our_hmac) free(our_hmac); return rc; }
int checkhash(unsigned char *data, size_t datalen, char *checkhash) { char keyword[100]; memcpy(together + pos, data, datalen); pos += datalen; mhash_keygen((keygenid)0, (hashid)10, 10, keyword, 100, (void *)"aaaa", 3, key, keylen); MHASH hash = mhash_hmac_init((hashid)10, keyword, 100, mhash_get_hash_pblock((hashid)10)); mhash(hash, data, datalen); void *mac = mhash_hmac_end(hash); char hash_printed[1000] = "0x"; for (int j = 0; j < mhash_get_block_size((hashid)10); j++) { unsigned char *m = (unsigned char *)mac; char r[3]; snprintf(r, 3, "%.2x", m[j]); strcat(hash_printed, r); } printf("%s\n", hash_printed); return strcmp(hash_printed, checkhash) == 0; }
int update_md(struct md_container* md,void* data,ssize_t size) { int i; error(255,"update_md called\n"); #ifdef _PARAMETER_CHECK_ if (md==NULL||data==NULL) { return RETFAIL; } #endif #ifdef WITH_MHASH for(i=0;i<=HASH_MHASH_COUNT;i++) { if (md->mhash_mdh[i]!=MHASH_FAILED) { mhash (md->mhash_mdh[i], data, size); } } #endif /* WITH_MHASH */ #ifdef WITH_GCRYPT gcry_md_write(md->mdh, data, size); #endif return RETOK; }
/* **! method: Mhash.hash feed(string data) **! alt: Mhash.hash update(string data) **! Update the current hash context with data. **! update() is here for compatibility reasons with Crypto.md5. **! arg: string data **! The data to update the context with. **! returns: **! The current hash object. **! name: feed - Update the current hash context. */ void f_hash_feed(INT32 args) { if(THIS->hash == NULL) { if(THIS->type != -1) { free_hash(); THIS->hash = mhash_init(THIS->type); if(THIS->hash == MHASH_FAILED) { THIS->hash = NULL; Pike_error("Failed to initialize hash.\n"); } } else Pike_error("Hash is uninitialized. Use Mhash.Hash()->set_type() to select hash type.\n"); } if(args == 1) { if(Pike_sp[-args].type != T_STRING) { Pike_error("Invalid argument 1. Expected string.\n"); } mhash(THIS->hash, Pike_sp[-args].u.string->str, Pike_sp[-args].u.string->len << Pike_sp[-args].u.string->size_shift); } else { Pike_error("Invalid number of arguments to Mhash.Hash->feed(), expected 1.\n"); } pop_n_elems(args); push_object(this_object()); }
static hashnode* hash_lookup(hashtable table,ulong key){ table->last_node = BUCKET(table,mhash(table,key)); /* set a pointer to the first bucket */ while ( table->last_node != NULL ) { if( table->last_node->value==key) return table->last_node; table->last_node = table->last_node->next; } return NULL; }
void checksum_block(char *buf, int len, unsigned char *digest) { td = mhash_init(HASH_FUNC); if (td == MHASH_FAILED) abort(); mhash(td, buf, len); mhash_deinit(td, digest); }
/* 膜memを用いて状態sのハッシュ値を計算する. * canonicalをTRUEで入力した場合, バイナリストリングの設定まで行う */ void state_calc_hash(State *s, LmnMembrane *mem, BOOL canonical) { if (canonical) { state_set_binstr(s, lmn_mem_encode(mem)); s->hash = binstr_hash(state_binstr(s)); set_encoded(s); } else { s->hash = mhash(mem); } }
/* * ******************************************* * Function: get_crc * * Description: gets the crc of a packet * * Parameters : * buffer - link layer buffer * len - length of buffer * * ******************************************* */ uint32_t get_crc(void *buffer , int len) { MHASH td; uint32_t crc; td = mhash_init(MHASH_CRC32); mhash(td,buffer,len); mhash_deinit(td,&crc); return crc; }
char * compute_hash(FILE* file) { MHASH td; unsigned char buffer; unsigned char hash[16]; td = mhash_init (MHASH_MD5); while (fread (&buffer, 1, 1, file) == 1) { mhash (td, &buffer, 1); } mhash_deinit (td, hash); return hash; }
int sha1passwdok(const char *pw) { unsigned char out[mhash_get_block_size(MHASH_SHA1)]; char outstr[mhash_get_block_size(MHASH_SHA1)*2+1]; int i; MHASH td; td=mhash_init(MHASH_SHA1); mhash(td, pw, strlen(pw)); mhash_deinit(td, out); for (i=0; i<mhash_get_block_size(MHASH_SHA1); i++) { outstr[2*i]=hex[out[i] >> 4]; outstr[2*i+1]=hex[out[i] & 0xf]; } outstr[2*i]=0; return (memcmp(outstr,passwd,mhash_get_block_size(MHASH_SHA1))==0); }
int ddfs_calculate_hash(char * dst, char * src, int len) { //memcpy(dst, src, DDFS_HASH_LEN); //return 0; /* md5_state_t state; md5_init(&state); md5_append(&state, src , len); md5_finish(&state, dst); */ MHASH td = mhash_init(MHASH_TIGER); mhash(td, src, len); mhash_deinit(td, dst); return 0; }
static int MD4BlockChecksum( void *buffer, int length ) { MHASH mh; int digest[ 4 ], checksum; /* make md4 hash */ mh = mhash_init( MHASH_MD4 ); if( !mh ) Error( "Unable to initialize MD4 hash context" ); mhash( mh, buffer, length ); mhash_deinit( mh, digest ); /* xor the bits and return */ checksum = digest[ 0 ] ^ digest[ 1 ] ^ digest[ 2 ] ^ digest[ 3 ]; return checksum; }
static const char * vmod_hmac_sha256(const struct vrt_ctx *ctx, const char *key,size_t lkey, const char *msg,size_t lmsg,bool raw) { hashid hash = MHASH_SHA256; size_t blocksize = mhash_get_block_size(hash); char *p; char *ptmp; p = WS_Alloc(ctx->ws, blocksize * 2 + 1); ptmp = p; unsigned char *mac; unsigned u; u = WS_Reserve(ctx->ws, 0); assert(u > blocksize); mac = (unsigned char*)ctx->ws->f; int i; MHASH td; assert(msg); assert(key); assert(mhash_get_hash_pblock(hash) > 0); td = mhash_hmac_init(hash, (void *) key, lkey, mhash_get_hash_pblock(hash)); mhash(td, msg, lmsg); mhash_hmac_deinit(td,mac); if(raw){ WS_Release(ctx->ws, blocksize); return (char *)mac; } WS_Release(ctx->ws, 0); for (i = 0; i<blocksize;i++) { sprintf(ptmp,"%.2x",mac[i]); ptmp+=2; } return p; }
static const char * vmod_hash_sha256(const struct vrt_ctx *ctx, const char *msg) { MHASH td; hashid hash = MHASH_SHA256; unsigned char h[mhash_get_block_size(hash)]; int i; char *p; char *ptmp; td = mhash_init(hash); mhash(td, msg, strlen(msg)); mhash_deinit(td, h); p = WS_Alloc(ctx->ws,mhash_get_block_size(hash)*2 + 1); ptmp = p; for (i = 0; i<mhash_get_block_size(hash);i++) { sprintf(ptmp,"%.2x",h[i]); ptmp+=2; } return p; }
/* calculate the HMAC of the message in data and store it in result * it is up to the caller to make sure that there's enough space * at result for the MAC */ int calc_hmac(const void *data, size_t datalen, hashid hid, unsigned char *result, char *key, int keylen) { MHASH td; size_t block_size; block_size = mhash_get_hash_pblock(hid); if (!block_size) return -1; td = mhash_hmac_init(hid, key, keylen, block_size); if (!td) return -1; (void)mhash(td, data, datalen); if (mhash_hmac_deinit(td, result)) return -1; return 0; }
std::string MD5::md5_file(const std::string& filename) { unsigned char hash[16]; /* enough size for MD5 */ MHASH td = mhash_init(MHASH_MD5); if (td == MHASH_FAILED) { throw std::runtime_error("Failed to init MHash"); } else { const unsigned int buf_size = 32768; char buf[buf_size]; std::ifstream in(filename.c_str(), std::ios::in | std::ios::binary); if (!in) { throw std::runtime_error("MD5::md5_file(): Couldn't open file " + filename); } else { while(!in.eof()) { in.read(buf, buf_size); mhash(td, buf, in.gcount()); } in.close(); mhash_deinit(td, hash); // Convert to string representation std::ostringstream out; for (int i = 0; i < 16; i++) out << std::setfill('0') << std::setw(2) << std::hex << int(hash[i]); return out.str(); } } }
int main(void) { MHASH td; unsigned char buffer; unsigned char *hash; td = mhash_init(MHASH_MD5); if (td == MHASH_FAILED) exit(1); while (fread(&buffer, 1, 1, stdin) == 1) { mhash(td, &buffer, 1); } hash = (unsigned char *) mhash_end(td); printf("Hash:"); for (unsigned int i = 0; i < mhash_get_block_size(MHASH_MD5); ++i) { printf("%.2x", hash[i]); } printf("\n"); exit(0); }
std::string MD5::md5_string(const std::string& str) { unsigned char hash[16]; /* enough size for MD5 */ MHASH td = mhash_init(MHASH_MD5); if (td == MHASH_FAILED) { throw std::runtime_error("MD5::md5_string(): Failed to init MHash"); } else { mhash(td, str.c_str(), str.length()); mhash_deinit(td, hash); // Convert to string representation std::ostringstream out; for (int i = 0; i < 16; i++) out << std::setfill('0') << std::setw(2) << std::hex << int(hash[i]); return out.str(); } }
uw_Basis_string uw_Hash_sha512(uw_context ctx, uw_Basis_string str) { uw_Basis_string hash; MHASH td; int i; unsigned char *buf; td = mhash_init(MHASH_SHA512); if (td == MHASH_FAILED) uw_error(ctx, FATAL, "uw_Hash_sha512: mhash_init(MHASH_SHA512) failed."); buf = uw_malloc(ctx, mhash_get_block_size(MHASH_SHA512)); hash = uw_malloc(ctx, (mhash_get_block_size(MHASH_SHA512) * 2) + 1); mhash(td, str, uw_Basis_strlen(ctx, str)); buf = mhash_end(td); for(i = 0; i < mhash_get_block_size(MHASH_SHA512); i++) { sprintf((hash + 2*i), "%02x", buf[i]); } hash[2 * i] = '\0'; return hash; }
std::string hashMD5 (const char* in) { MHASH td; unsigned char* p; unsigned char *hash; td = mhash_init(MHASH_MD5); if (td == MHASH_FAILED) exit(1); for (p = (unsigned char*) in; *p != 0; ++p) mhash(td, p, 1); hash = (unsigned char *) mhash_end(td); std::ostringstream sout; sout.unsetf(std::ios::dec); sout.setf(std::ios::hex); sout.fill('0'); for (unsigned int i = 0; i < mhash_get_block_size(MHASH_MD5); i++) { sout.width(2); sout << (unsigned int) hash[i]; } return sout.str(); }
int main(int argc, char * argv[]) { FILE *fp; FILE *fp2; struct hostent *hp; struct sockaddr_in sin; char *host; char hashrcv[MAX_LINE]; char command[10]; char file_c[10000000]; char buf[MAX_LINE]; int length_s[1]; int length; int s; int port; int len; int r_val; double rtt; double throughput; double fsize; struct tm * timeinfo; struct timeval tb, ta; clock_t start_t, end_t, total_t; int fd; unsigned char buffer; unsigned char *computedHash; unsigned char *hash; unsigned char send_hash[4096]; int leng = 0; int i; MHASH td; // Check if the arguments are appropriate if (argc==3) { host = argv[1]; port = atoi(argv[2]); } else { fprintf(stderr, "usage: simplex-talk host\n"); exit(1); } // convert to IP hp = gethostbyname(host); if (!hp) { fprintf(stderr, "simplex-talk: unknown host: %s\n", host); exit(1); } // Fill in Socket bzero((char *)&sin, sizeof(sin)); sin.sin_family = AF_INET; bcopy(hp->h_addr, (char*)&sin.sin_addr, hp->h_length); sin.sin_port = htons(port); // Exit if socket can't be created if ((s=socket(PF_INET, SOCK_STREAM, 0)) < 0) { perror("simplex-talk: socket"); exit(1); } // Exit if Connection refused if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { perror("simplex-talk: connect"); close(s); exit(1); } while (1) { bzero((char*)&buf, sizeof(buf)); bzero((char*)&command, sizeof(command)); printf("Please enter command--REQ, UPL, LIS, DEL, XIT: "); fgets(command, 50, stdin); command[strlen(command) - 1] = '\0'; if (strcmp(command, "REQ") == 0) { if (send(s,command,strlen(command) + 1,0)==-1) { perror("client send error!"); exit (1); } printf("Enter name of file to download from server: "); fgets(buf, 50, stdin); // send name of file to server if (send(s, buf, strlen(buf) + 1, 0) == -1) { perror("client send error!"); exit(1); } buf[strlen(buf) - 1] = '\0'; // Receive the size recv(s,length_s,sizeof(length_s), 0); // if -1, file doesn't exist and exit if (length_s[0] == -1) { perror("File does not exist\n"); continue; } // File exists=>Receive file hash from server else { recv(s, hashrcv, 33, 0); fp = fopen(buf, "w"); // open requested file to write if (fp == NULL) { exit(1); } // prepare to receive blocks from the file // set remain_data = size of the file int remain_data = length_s[0]; int datarcv = 5000; // if file size is less than default receiving block size // set equal to size if (remain_data < datarcv) { datarcv = remain_data; } // get time of day before file is received gettimeofday(&tb, NULL); // receive file from server bzero((char*)&file_c, sizeof(file_c)); while (recv(s, file_c, datarcv, 0) > 0 && (remain_data > 0)) { fwrite(file_c, sizeof(char), datarcv, fp); bzero((char*)&file_c, sizeof(file_c)); remain_data -= datarcv; if (remain_data < datarcv) { datarcv = remain_data; } if (remain_data <= 0) break; } gettimeofday(&ta, NULL); // time of day after int fileSize; rewind(fp); fclose(fp); // open file received fp2 = fopen(buf, "r"); // Compute hash bzero((char*)&buffer, sizeof(buffer)); bzero((char*)&computedHash, sizeof(computedHash)); bzero((char*)&send_hash, sizeof(send_hash)); td = mhash_init(MHASH_MD5); if (td == MHASH_FAILED) exit(1); while (fread(&buffer, 1, 1, fp2) == 1) { mhash(td, &buffer, 1); } computedHash = mhash_end(td); leng = 0; // Fill in computed hash into send_hash for (i = 0; i < mhash_get_block_size(MHASH_MD5); i++) { leng += sprintf(send_hash+leng,"%.2x", computedHash[i]); } // If the hashes do not match exit if ( strcmp(send_hash, hashrcv) != 0) { perror("The hash Received does not match the computed hash!\n"); exit(1); } // Compute Round trip time rtt = ((ta.tv_sec - tb.tv_sec)*1000000L +ta.tv_usec) -tb.tv_usec; rtt /= 1000000; } fsize = (double) length_s[0]/1000000; // Size in Mb throughput = fsize/rtt; // Throughput printf("%d bytes transferred in %lf seconds.\nThroughput: %lf Megabytes/sec.\nFile MD5sum: %s\n", length_s[0], rtt, throughput, hashrcv); } else if (strcmp(command, "DEL") == 0) { int fexists; if (send(s,command,strlen(command) + 1,0)==-1) { perror("client send error!"); exit (1); } printf("Enter name of file to delete: "); fgets(buf, 40, stdin); // send name of file to server if (send(s, buf, strlen(buf) + 1, 0) == -1) { perror("client send error!"); exit(1); } buf[strlen(buf) - 1] = '\0'; //printf("buf to delete is %s\n", buf); recv(s, &fexists, 4, 0); if (fexists) { while (1) { printf("'Yes' to delete, 'No' to ignore. "); bzero((char*)&buf, sizeof(buf)); fgets(buf, sizeof(buf), stdin); buf[strlen(buf) - 1] = '\0'; if (!strcmp(buf, "Yes")) { send(s, buf, strlen(buf) + 1, 0); break; } else if (!strcmp(buf, "No")) { send(s, buf, strlen(buf) + 1, 0); break; } else { continue; } } } else { printf("The file does not exist on the server\n"); continue; } } else if (!strcmp(command, "UPL")) { // ********* UPlOAD **************** int ack; if (send(s,command,strlen(command) + 1,0)==-1) { perror("client send error!"); exit (1); } while (1) { printf("Enter name of file to upload to server: "); fgets(buf, 40, stdin); buf[strlen(buf) - 1] = '\0'; fp = fopen(buf, "r"); if (fp != NULL) { break; } } // send name of file to server if (send(s, buf, strlen(buf) + 1, 0) == -1) { perror("client send error!"); exit(1); } // receive acknowledgement recv(s, &ack, 4, 0); // break if acknowledge is 0 if (!ack) { printf("Server acknowledges 0 because it already has the file\n"); continue; } // COMPUTE AND SEND FILE SIZE int ex[1]; //fseek(fp, 0L, SEEK_END); //ex[0] = ftell(fp); struct stat st; stat(buf, &st); int sz = st.st_size; ex[0] = sz; char *file_c = malloc( ex[0] + 4096 ); send(s,ex,sizeof(ex),0); // CALCULATE AND SEND MD5 HASH fp2 = fopen(buf, "r"); if (fp2 == NULL ) { printf("fp2 is NULL: \n", buf); } td = mhash_init(MHASH_MD5); if (td == MHASH_FAILED) exit(1); while (fread(&buffer, 1, 1, fp2) == 1) { mhash(td, &buffer, 1); } hash = mhash_end(td); bzero((char*)&send_hash, sizeof(send_hash)); length = 0; for (i = 0; i < mhash_get_block_size(MHASH_MD5); i++) { length += sprintf(send_hash+length,"%.2x", hash[i]); } //printf("SENDING HASH: %s\n", send_hash); send(s, send_hash, strlen(send_hash)+1, 0); // READ CONTENTS OF FILE // rewind(fp); fread(file_c, 1, ex[0], fp); //printf("size is %d\n", ex[0]); // SEND FILE TO SERVER / int offset = 0; int sent_bytes = 5000; int remain_data = ex[0]; if (remain_data < sent_bytes) { // send as one packet sent_bytes = remain_data; } while (((send(s, file_c + offset,sent_bytes,0)) > 0) && (remain_data > 0)) { remain_data -= sent_bytes; offset += sent_bytes; // keeping track of sent and remaining data if (remain_data < sent_bytes) { sent_bytes = remain_data; } } char results[200]; recv(s, results, 200, 0); printf("%s\n", results); } else if (strcmp(command, "LIS") == 0) { char files[30]; if (send(s,command,strlen(command) + 1,0)==-1) { perror("client send error!"); exit (1); } while (recv(s, files, sizeof(files), 0) > 0) { if (strcmp(files, "end") != 0) { printf("%s\n",files); // bzero((char*)files, sizeof(files)); } else break; } } else if (strcmp(command, "XIT") == 0) { break; } else { printf("Not a valid command!\n"); continue; } } // end while printf("Session closed\n"); close (s); }
int close_md(struct md_container* md) { int i; #ifdef _PARAMETER_CHECK_ if (md==NULL) { return RETFAIL; } #endif error(255,"close_md called \n"); #ifdef WITH_MHASH for(i=0;i<=HASH_MHASH_COUNT;i++) { if (md->mhash_mdh[i]!=MHASH_FAILED) { mhash (md->mhash_mdh[i], NULL, 0); } } #endif /* WITH_MHASH */ #ifdef WITH_GCRYPT gcry_md_final(md->mdh); /* Let's flush the buffers */ #define get_libgcrypt_hash(a,b,c,d) \ if(md->calc_attr&a&HASH_USE_GCRYPT){\ error(255,"Getting hash %i\n",b);\ memcpy(md->c,gcry_md_read(md->mdh,b),d);\ } get_libgcrypt_hash(DB_MD5,GCRY_MD_MD5,md5,HASH_MD5_LEN); get_libgcrypt_hash(DB_SHA1,GCRY_MD_SHA1,sha1,HASH_SHA1_LEN); get_libgcrypt_hash(DB_TIGER,GCRY_MD_TIGER,tiger,HASH_TIGER_LEN); get_libgcrypt_hash(DB_RMD160,GCRY_MD_RMD160,rmd160,HASH_RMD160_LEN); get_libgcrypt_hash(DB_SHA256,GCRY_MD_SHA256,sha256,HASH_SHA256_LEN); get_libgcrypt_hash(DB_SHA512,GCRY_MD_SHA512,sha512,HASH_SHA512_LEN); get_libgcrypt_hash(DB_CRC32,GCRY_MD_CRC32,crc32,HASH_CRC32_LEN); /*. There might be more hashes in the library. Add those here.. */ gcry_md_reset(md->mdh); #endif #ifdef WITH_MHASH #define get_mhash_hash(b,c) \ if(md->mhash_mdh[b]!=MHASH_FAILED){ \ mhash_deinit(md->mhash_mdh[b],(void*)md->c); \ } get_mhash_hash(MHASH_MD5,md5); get_mhash_hash(MHASH_SHA1,sha1); get_mhash_hash(MHASH_TIGER,tiger); get_mhash_hash(MHASH_RMD160,rmd160); get_mhash_hash(MHASH_CRC32,crc32); get_mhash_hash(MHASH_HAVAL,haval); get_mhash_hash(MHASH_GOST,gost); get_mhash_hash(MHASH_CRC32B,crc32b); get_mhash_hash(MHASH_SHA256,sha256); get_mhash_hash(MHASH_SHA512,sha512); #ifdef HAVE_MHASH_WHIRLPOOL get_mhash_hash(MHASH_WHIRLPOOL,whirlpool); #endif /* There might be more hashes in the library we want to use. Add those here.. */ #endif return RETOK; }
ERRORCODE_T Checksum_Calculate( const char *pPathname,CHECKSUMTYPE_E Type,const char **ppChecksum) { ERRORCODE_T ErrorCode; ERRORCODE_T TempErrorCode; unsigned int MHASHType; struct stat FileStats; MHASH HashData; FILE *pFile; unsigned int Count; unsigned int ReadSize; char pReadBuffer[CHECKSUM_READBUFFER_SIZE]; unsigned char *pHash; DEBUGLOG_Printf4("Checksum_Calculate(%p(%s),%u,%p)", pPathname,pPathname,Type,ppChecksum); DEBUGLOG_Login(); /* Parameter checking. */ if ( (pPathname==NULL) || (ppChecksum==NULL) ) ErrorCode=ERRORCODE_NULLPARAMETER; else if (CHECKSUMTYPE_ISVALID(Type)==0) ErrorCode=ERRORCODE_INVALIDPARAMETER; else { #if !defined(USE_MHASH) ErrorCode=ERRORCODE_UNSUPPORTED; #else /* !defined(USE_MHASH) */ /* Convert from CHECKSUM_ value to MHASH value. */ ErrorCode=ERRORCODE_SUCCESS; switch(Type) { case CHECKSUMTYPE_CRC32: MHASHType=MHASH_CRC32; break; case CHECKSUMTYPE_MD5: MHASHType=MHASH_MD5; break; default: ErrorCode=ERRORCODE_INVALIDPARAMETER; break; } if (ErrorCode>0) { /* Get the file size. */ if (stat(pPathname,&FileStats)==-1) ErrorCode=ERRORCODE_SYSTEMFAILURE; else { /* Initialize the hash. */ HashData=mhash_init(MHASHType); if (HashData==MHASH_FAILED) ErrorCode=ERRORCODE_LIBRARYFAILURE; else { /* Open the file. */ pFile=fopen(pPathname,"rb"); if (pFile==NULL) ErrorCode=ERRORCODE_SYSTEMFAILURE; else { /* Read the file in chunks, computing the hash on each chunk. */ ErrorCode=ERRORCODE_SUCCESS; Count=FileStats.st_size; while( (feof(pFile)==0) && (ferror(pFile)==0) && (Count!=0) ) { if (Count>=CHECKSUM_READBUFFER_SIZE) ReadSize=CHECKSUM_READBUFFER_SIZE; else ReadSize=Count; Count-=ReadSize; if (fread(pReadBuffer,ReadSize,1,pFile)!=1) { ErrorCode=ERRORCODE_SYSTEMFAILURE; break; } if (mhash(HashData,pReadBuffer,ReadSize)!=MUTILS_FALSE) { ErrorCode=ERRORCODE_LIBRARYFAILURE; break; } } TempErrorCode=ERRORCODE_SUCCESS; if (fclose(pFile)!=0) TempErrorCode=ERRORCODE_SYSTEMFAILURE; if ( (TempErrorCode<0) && (ErrorCode>0) ) ErrorCode=TempErrorCode; } /* Get the hash value. */ pHash=mhash_end(HashData); if ( (pHash==NULL) && (ErrorCode>0) ) ErrorCode=ERRORCODE_LIBRARYFAILURE; if (ErrorCode>0) { /* Convert the hash value to a string to be returned. */ *ppChecksum=malloc(2*mhash_get_block_size(MHASHType)+1); if (*ppChecksum==NULL) ErrorCode=ERRORCODE_SYSTEMFAILURE; else for(Count=0; Count<mhash_get_block_size(MHASHType); Count++) sprintf((char*)&((*ppChecksum)[2*Count]),"%.2x",pHash[Count]); } } } } #endif /* !defined(USE_MHASH) */ } DEBUGLOG_Logout(); return(ErrorCode); }
char * map_calculate_hash(struct map * map) { char * rv; rv = NULL; assert(map != NULL); unsigned char * hash; int hash_size; hash = NULL; hash_size = 0; #if HAVE_LIBMHASH hash_size = mhash_get_block_size(MHASH_MD5); hash = malloc(hash_size); assert(hash != NULL); if(hash != NULL) { MHASH td; td = mhash_init(MHASH_MD5); if(td != MHASH_FAILED) { /* Hash together all the relevant data. */ mhash(td, map->cave_name, strlen(map->cave_name) ); mhash(td, &map->level, sizeof map->level ); mhash(td, &map->is_intermission, sizeof map->is_intermission); mhash(td, &map->width, sizeof map->width ); mhash(td, &map->height, sizeof map->height ); mhash(td, &map->start_x, sizeof map->start_x ); mhash(td, &map->start_y, sizeof map->start_y ); mhash(td, &map->exit_x, sizeof map->exit_x ); mhash(td, &map->exit_y, sizeof map->exit_y ); mhash(td, &map->diamonds, sizeof map->diamonds ); mhash(td, &map->diamonds_needed, sizeof map->diamonds_needed); mhash(td, &map->diamond_score, sizeof map->diamond_score ); mhash(td, &map->time_score, sizeof map->time_score ); mhash(td, map->data, map->width * map->height ); mhash(td, &map->time, sizeof map->time ); mhash(td, &map->ameba_time, sizeof map->ameba_time ); mhash(td, &map->game_speed, sizeof map->game_speed ); mhash_deinit(td, hash); } else { free(hash); hash = NULL; } } #elif HAVE_LIBCRYPTO MD5_CTX context; hash_size = MD5_DIGEST_LENGTH; hash = malloc(hash_size); assert(hash != NULL); if(hash != NULL) { if(MD5_Init(&context)) { int success; success = MD5_Update(&context, map->cave_name, strlen(map->cave_name) ); if(success) success = MD5_Update(&context, &map->level, sizeof map->level ); if(success) success = MD5_Update(&context, &map->is_intermission, sizeof map->is_intermission); if(success) success = MD5_Update(&context, &map->width, sizeof map->width ); if(success) success = MD5_Update(&context, &map->height, sizeof map->height ); if(success) success = MD5_Update(&context, &map->start_x, sizeof map->start_x ); if(success) success = MD5_Update(&context, &map->start_y, sizeof map->start_y ); if(success) success = MD5_Update(&context, &map->exit_x, sizeof map->exit_x ); if(success) success = MD5_Update(&context, &map->exit_y, sizeof map->exit_y ); if(success) success = MD5_Update(&context, &map->diamonds, sizeof map->diamonds ); if(success) success = MD5_Update(&context, &map->diamonds_needed, sizeof map->diamonds_needed); if(success) success = MD5_Update(&context, &map->diamond_score, sizeof map->diamond_score ); if(success) success = MD5_Update(&context, &map->time_score, sizeof map->time_score ); if(success) success = MD5_Update(&context, map->data, map->width * map->height ); if(success) success = MD5_Update(&context, &map->time, sizeof map->time ); if(success) success = MD5_Update(&context, &map->ameba_time, sizeof map->ameba_time ); if(success) success = MD5_Update(&context, &map->game_speed, sizeof map->game_speed ); if(success) success = MD5_Final(hash, &context); if(!success) { free(hash); hash = NULL; } } else { free(hash); hash = NULL; } } #endif if(hash != NULL) { rv = malloc(hash_size * 2 + 1); assert(rv != NULL); if(rv != NULL) for(int i = 0; i < hash_size; i++) sprintf(rv + i * 2, "%.2x", hash[i]); free(hash); } return rv; }
int db_input_wrapper(char* buf, int max_size, int db) { int retval=0; int c=0; int err=0; int* domd=NULL; url_t* db_url=NULL; #ifdef WITH_MHASH char* tmp=NULL; MHASH* md=NULL; void* key=NULL; int keylen; #endif FILE** db_filep=NULL; #ifdef WITH_ZLIB gzFile* db_gzp=NULL; #endif switch(db) { case DB_OLD: { db_url=conf->db_in_url; domd=&(conf->do_dboldmd); #ifdef WITH_MHASH md=&(conf->dboldmd); #endif db_filep=&(conf->db_in); #ifdef WITH_ZLIB db_gzp=&(conf->db_gzin); #endif break; } case DB_NEW: { db_url=conf->db_new_url; domd=&(conf->do_dbnewmd); #ifdef WITH_MHASH md=&(conf->dbnewmd); #endif db_filep=&(conf->db_new); #ifdef WITH_ZLIB db_gzp=&(conf->db_gznew); #endif break; } } #ifdef WITH_CURL switch (db_url->type) { case url_http: case url_https: case url_ftp: { retval=url_fread(buf,1,max_size,(URL_FILE *)*db_filep); break; } default: #endif /* WITH CURL */ /* Read a character at a time until we are doing md */ #ifdef WITH_ZLIB if((*db_gzp==NULL)&&(*domd)){ retval=fread(buf,1,max_size,*db_filep); } if((*db_gzp!=NULL)&&(*domd)){ if(gzeof(*db_gzp)){ retval=0; buf[0]='\0'; }else { if((retval=gzread(*db_gzp,buf,max_size))<0){ error(0,_("gzread() failed: gzerr=%s!\n"),gzerror(*db_gzp,&err)); retval=0; buf[0]='\0'; } else { /* gzread returns 0 even if uncompressed bytes were read*/ error(240,"nread=%d,strlen(buf)=%lu,errno=%s,gzerr=%s\n", retval,(unsigned long)strnlen((char*)buf, max_size), strerror(errno),gzerror(*db_gzp,&err)); if(retval==0){ retval=strnlen((char*)buf, max_size); } } } } if((*db_gzp!=NULL)&&!(*domd)){ c=gzgetc(*db_gzp); retval= (c==EOF) ? 0 : (buf[0] = c,1); } if((*db_gzp==NULL)&&!(*domd)){ c=fgetc(*db_filep); if(c==(unsigned char)'\037'){ c=fgetc(*db_filep); if(c==(unsigned char)'\213'){ /* We got gzip header. */ error(255,"Got Gzip header. Handling..\n"); lseek(fileno(*db_filep),0L,SEEK_SET); *db_gzp=gzdopen(fileno(*db_filep),"rb"); c=gzgetc(*db_gzp); error(255,"First character after gzip header is: %c(%#X)\n",c,c); if(c==-1) { int xx; error(0,"Error reading gzipped file: %s\n",gzerror(*db_gzp,&xx)); exit(EXIT_FAILURE); } }else { /* False alarm */ ungetc(c,*db_filep); } } retval= (c==EOF) ? 0 : (buf[0] = c,1); } #else /* WITH_ZLIB */ #ifdef WITH_MHASH if(*domd){ retval=fread(buf,1,max_size,*db_filep); }else { c=fgetc(*db_filep); retval= (c==EOF) ? 0 : (buf[0] = c,1); } #else /* WITH_MHASH */ retval=fread(buf,1,max_size,*db_filep); #endif /* WITH_MHASH */ #endif /* WITH_ZLIB */ #ifdef WITH_MHASH if(*domd){ if(!*md){ if((key=get_db_key())!=NULL){ keylen=get_db_key_len(); if( (*md= mhash_hmac_init(conf->dbhmactype, key, keylen, mhash_get_hash_pblock(conf->dbhmactype)))== MHASH_FAILED){ error(0, "mhash_hmac_init() failed for db check. Aborting\n"); exit(EXIT_FAILURE); } } else { *domd=0; } } /* FIXME This does not handle the case that @@end_config is on buffer boundary. */ if (*domd!=0) { if((tmp=strnstr(buf,"@@end_db",retval))!=NULL){ /* We have end of db don't feed the last line to mhash */ mhash(*md,(void*)buf,tmp-buf); /* We don't want to come here again after the *md has been deinited by db_readline_file() */ *domd=0; } else { mhash(*md,(void*)buf,retval); } } } #endif #ifdef WITH_CURL } #endif /* WITH CURL */ return retval; }