/* 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; }
static void digest_free(PX_MD * h) { MHASH mh = (MHASH) h->p.ptr; uint8 *buf = mhash_end(mh); mhash_free(buf); px_free(h); }
static int get_digest(void) { if(THIS->res == NULL && THIS->hash != NULL) { THIS->res = mhash_end(THIS->hash); THIS->hash = NULL; } if(THIS->res == NULL) { Pike_error("No hash result available!\n"); } return mhash_get_block_size(THIS->type); }
static void digest_reset(PX_MD * h) { MHASH mh = (MHASH) h->p.ptr; hashid id = mhash_get_mhash_algo(mh); uint8 *res = mhash_end(mh); mhash_free(res); mh = mhash_init(id); h->p.ptr = mh; }
static void digest_finish(PX_MD * h, uint8 *dst) { MHASH mh = (MHASH) h->p.ptr; unsigned hlen = digest_result_size(h); hashid id = mhash_get_mhash_algo(mh); uint8 *buf = mhash_end(mh); memcpy(dst, buf, hlen); mhash_free(buf); mh = mhash_init(id); h->p.ptr = mh; }
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 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(); }
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; }
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); }
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); }
int main(void) { int i, buf_len; MHASH td1, td2, td3; const unsigned char *buf = "This is a test buffer to test saving and restoring, see?"; unsigned char *hash1, *hash2; hashid alg; char mem[1024]; int mem_size = sizeof(mem); buf_len = strlen(buf); /* NOTE: For laziness sake, I just loop through the enum, skipping invalid integers. If the enum should change, this loop will have to change! */ for (alg = 0; alg <= mhash_count(); ++alg) { /* if algorithm does not exist */ if (mhash_get_hash_name_static( alg)==NULL) continue; printf("Testing save/restore for algorithm %s: ", mhash_get_hash_name(alg)); td1 = mhash_init(alg); if (td1 == MHASH_FAILED) { fprintf(stderr, "Failed to init td1.\n"); exit(1); } for (i = 0; i < buf_len; ++i) mhash(td1, buf+i, 1); hash1 = mhash_end(td1); /* printf("Hash 1: "); for (i = 0; i < mhash_get_block_size(alg); ++i) printf("%.2x", hash1[i]); printf("\n"); */ td2 = mhash_init(alg); if (td2 == MHASH_FAILED) { fprintf(stderr, "Failed to init td2.\n"); exit(1); } for (i = 0; i < buf_len/2; ++i) mhash(td2, buf+i, 1); if (mhash_save_state_mem(td2, mem, &mem_size)!=0) { fprintf(stderr, "Error saving state. Size: %d\n", mem_size); exit(1); } td3 = mhash_restore_state_mem( mem); if (td3 == MHASH_FAILED) { fprintf(stderr, "Error restoring state.\n"); exit(1); } for (i = buf_len/2; i < buf_len; ++i) mhash(td3, buf+i, 1); hash2 = mhash_end(td3); /* printf("Hash 2: "); for (i = 0; i < mhash_get_block_size(alg); ++i) printf("%.2x", hash2[i]); printf("\n"); */ if (memcmp(hash1, hash2, mhash_get_block_size(alg)) == 0) { printf("Ok\n"); } else { printf("Failed\n"); exit(1); } } exit(0); }
int main(void) { mutils_word32 i; mutils_word32 buf_len; MHASH td1, td2, td3; const mutils_word8 *buf = (mutils_word8 *) "This is a test buffer to test saving and restoring, see?"; mutils_word8 *hash1; mutils_word8 *hash2; hashid alg; mutils_word8 mem[1024]; mutils_word32 mem_size = sizeof(mem); buf_len = mutils_strlen(buf); /* * NOTE: For laziness sake, I just loop through the enum, * skipping invalid integers. * * If the enum should change, this loop will have to change! */ for (alg = 0; alg <= mhash_count(); ++alg) { /* if algorithm does not exist */ if (mhash_get_hash_name_static(alg) == (mutils_word8 *) NULL) continue; printf("Testing save/restore for algorithm %s: ", mhash_get_hash_name(alg)); td1 = mhash_init(alg); if (td1 == MHASH_FAILED) { fprintf(stderr, "Failed to init td1.\n"); exit(MUTILS_INVALID_FUNCTION); } for (i = 0; i < buf_len; ++i) { mhash(td1, buf+i, 1); } hash1 = mhash_end(td1); td2 = mhash_init(alg); if (td2 == MHASH_FAILED) { fprintf(stderr, "Failed to init td2.\n"); exit(MUTILS_INVALID_FUNCTION); } for (i = 0; i < buf_len/2; ++i) { mhash(td2, buf+i, 1); } if (mhash_save_state_mem(td2, mem, &mem_size)!=0) { fprintf(stderr, "Error saving state. Size: %d\n", mem_size); exit(MUTILS_INVALID_RESULT); } td3 = mhash_restore_state_mem( mem); if (td3 == MHASH_FAILED) { fprintf(stderr, "Error restoring state.\n"); exit(MUTILS_INVALID_RESULT); } for (i = buf_len/2; i < buf_len; ++i) { mhash(td3, buf+i, 1); } hash2 = mhash_end(td3); if (mutils_memcmp(hash1, hash2, mhash_get_block_size(alg)) == 0) { printf("Ok\n"); } else { printf("Failed\n"); exit(MUTILS_INVALID_RESULT); } } exit(MUTILS_OK); }
int main(int argc, char* argv[]) { FILE *fp; struct sockaddr_in sin; char buf[MAX_LINE]; int len; int port; int s, new_s,i; int opt = 1; MHASH td; unsigned char buffer; unsigned char *hash; unsigned char send_hash[256]; char hashrcv[40]; char packet[100]; int length = 0; time_t rawtime; FILE *fp2; unsigned char symlink[100]; char fname[40]; int length_s[2]; char file_c[10000000]; struct tm * timeinfo; struct timeval tb, ta; int leng = 0; double rtt, fsize, throughput; unsigned char *computedHash; /* ENSURE PROPER ARGUMENT STRUCTURE */ if (argc == 2) { port = atoi(argv[1]); } else { perror("Needs port number as argument\n"); exit(1); } /* FILLING SOCKADDR_IN STRUCT */ bzero((char*)&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(port); /* SOCKET CREATION */ if((s=socket(PF_INET, SOCK_STREAM, 0)) < 0) { perror("simplex-talk:socket"); exit(1); } /* ENABLE OPTION TO REUSE SOCKET */ if((setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)& opt, sizeof(int))) < 0) { perror("simplex-talk:setsocket\n"); exit(1); } /* BIND SOCKET */ if ((bind(s,(struct sockaddr *)&sin, sizeof(sin))) < 0) { perror("simplex-talk:bind\n"); exit(1); } /* LISTEN TO SOCKET */ if ((listen(s, MAX_PENDING))<0) { perror ("simplex-talk:listen"); exit(1); } while(1) { if ((new_s = accept(s,(struct sockaddr *)&sin, &len)) < 0) { perror("simplex-talk:accept"); exit(1); } printf("Waiting for operation from client...\n"); while(1) { bzero((char*)&buf, sizeof(buf)); /* ACCEPT AND MAKE NEW SOCKET */ if ((len=recv(new_s, buf, sizeof(buf),0)) == -1) { perror("Server Receiving Error!\n"); exit(1); } if (len == 0) { break; } if (strcmp(buf, "REQ") == 0) { bzero((char*)&buf, sizeof(buf)); if ((len=recv(new_s, buf, sizeof(buf), 0)) == -1) { perror("Server Receiving Error!\n"); exit(1); } buf[strlen(buf) - 1] = '\0'; // concat with path to test files bzero((char*)&symlink, sizeof(symlink)); strcpy(symlink,"Prog3TestFiles/"); strcat(symlink,buf); strcpy(buf, symlink); /* OPEN FILE FROM FILE NAME SENT TO SERVER */ fp = fopen(buf, "r"); if (fp == NULL) { int fne[1]; fne[0] = -1; send(new_s,fne,4,0); } else { /* COMPUTE AND SEND FILE SIZE */ int ex[1]; struct stat st; stat(buf, &st); int sz = st.st_size; ex[0] = sz; char *file_c = malloc( ex[0] + 4096 ); send(new_s,ex,sizeof(ex),0); /* CALCULATE AND SEND MD5 HASH */ fp2 = fopen(buf, "r"); 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]); } send(new_s, send_hash, strlen(send_hash)+1, 0); /* READ CONTENTS OF FILE */ rewind(fp); fread(file_c, 1, ex[0], fp); /* SEND FILE TO CLIENT */ 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(new_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; } } } }// end REQ if // delete command else if (strcmp(buf, "DEL") == 0) { int fexists; if ((len=recv(new_s, buf, sizeof(buf), 0)) == -1) { perror("Server Receiving Error!\n"); exit(1); } buf[strlen(buf) - 1] = '\0'; strcpy(fname, buf); fp = fopen(buf, "r"); // check if file exists if (fp == NULL) fexists = 0; else fexists = 1; // send whether the file exists or not send(new_s, &fexists, 4, 0); // receive confirmation of delete bzero((char *)&buf, sizeof(buf)); recv(new_s, buf, sizeof(buf), 0); if (!strcmp(buf, "Yes")) { remove(fname); } } else if (!strcmp(buf, "UPL")) { int ack; bzero((char*)&buf, sizeof(buf)); if ((len=recv(new_s, buf, sizeof(buf), 0)) == -1) { perror("Server Receiving Error!\n"); exit(1); } fp = fopen(buf, "r"); if (fp != NULL) { ack = 0; continue; } else { ack = 1; } // send ack send(new_s, &ack, 4, 0); // receive size of file recv(new_s, length_s, sizeof(length_s), 0); // receive hash recv(new_s, hashrcv, 33, 0); fp = fopen(buf, "w"); // open requested file to write if (fp == NULL) { printf("fp is null\n"); 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(new_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"); if (fp2 == NULL) { printf("fp2 is null\n"); exit(1); } // 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 char results[300]; sprintf(results,"%d bytes transferred in %lf seconds.\nThroughput: %lf Megabytes/sec.\nFile MD5sum: %s", length_s[0], rtt, throughput, hashrcv); send(new_s, results, 300, 0); } else if (strcmp(buf, "LIS") == 0) { DIR *dirp; struct dirent *dp; char contents[30]; dirp = opendir("."); while((dp = readdir(dirp)) != NULL) { sprintf(contents, dp->d_name); send(new_s, contents,30, 0); } strcpy(contents, "end"); send(new_s, contents, sizeof(contents), 0); closedir(dirp); }else if (strcmp(buf, "XIT") == 0) { break; } else { printf("not a valid command!\n"); } } close(new_s); } }
WIN32DLL_DEFINE MHASH mhash_hmac_init(__const hashid type, void *key, mutils_word32 keysize, mutils_word32 block) { MHASH ret = MHASH_FAILED; MHASH tmptd; mutils_word8 *ipad; mutils_word8 _ipad[MAX_BLOCK_SIZE]; mutils_word32 i; mutils_boolean ipad_alloc = MUTILS_FALSE; mutils_boolean res; if (block == 0) { block = 64; /* the default for ripemd,md5,sha-1 */ } ret = mhash_init_int(type); if (ret != MHASH_FAILED) { /* Initial hmac calculations */ ret->hmac_block = block; if ( ret->hmac_block > MAX_BLOCK_SIZE) { ipad = mutils_malloc(ret->hmac_block); if (ipad == NULL) { return MHASH_FAILED; } ipad_alloc = MUTILS_TRUE; } else { ipad = _ipad; } if (keysize > ret->hmac_block) { tmptd = mhash_init(type); mhash(tmptd, key, keysize); ret->hmac_key_size = mhash_get_block_size(type); ret->hmac_key = mhash_end(tmptd); } else { ret->hmac_key = mutils_malloc(ret->hmac_block); mutils_bzero(ret->hmac_key, ret->hmac_block); mutils_memcpy(ret->hmac_key, key, keysize); ret->hmac_key_size = ret->hmac_block; } /* IPAD */ for (i = 0; i < ret->hmac_key_size; i++) { ipad[i] = (0x36) ^ ret->hmac_key[i]; } for (; i < ret->hmac_block; i++) { ipad[i] = (0x36); } res = mhash(ret, ipad, ret->hmac_block); if (ipad_alloc == MUTILS_TRUE) { mutils_free(ipad); } } return(ret); }