void FillVersion() { #define TO_STR(x) #x std::string ver_str; std::string md5_str; std::stringstream ss; ss << "svn version " << _SVN_VERSION_ << ", " << "compiled by " << TO_STR(_BUILDER_) << ", " << __DATE__ << __TIME__; ss >> ver_str; time_t now = time(); char str_tm[256]; memset(str_tm, 0, sizeof(str_tm)); ctime_r(&now, &str_tm); char exe_file[256]; sprintf(exe_file, "/proc/%d/exe", getpid()); uint8_t md5[16]; CalcFileMD5(exe_file, md5); const char *hex = "0123456789abcdef"; for (int i = 0; i < 16; ++i) { md5_str[i*2+0] = hex[ (md5[i]&0xF0)>>4 ]; md5_str[i*2+0] = hex[ (md5[i]&0x0F) ]; } #undef TO_STR }
short descipher(char *pathInFile,char *pathOutFile) { char MD5[MD5_LEN+1];//plus one for character end of line CalcFileMD5(pathInFile,MD5); FileCipherWithDES fileCipherWithDES=getFileCipherWithDES(MD5); if(!strcmp(fileCipherWithDES.MD5,"00000000000000000000000000000000")) { //the query failed !!CORREGIR ESTO return -1; } if(counterMode(pathInFile,pathOutFile,fileCipherWithDES.initialVector,fileCipherWithDES.key)<0) { return -2; } return 0; }
short cipher(char *pathInFile,char *pathOutFile) { uint64_t initialVector=(uint64_t)getRandom(),key=(uint64_t)getRandom();//initial vector, key if(counterMode(pathInFile,pathOutFile,initialVector,key)<0) { return -1; } FileCipherWithDES fileCipherWithDES; strcpy(fileCipherWithDES.idUser,user.id); CalcFileMD5(pathOutFile,fileCipherWithDES.MD5); fileCipherWithDES.initialVector=initialVector; fileCipherWithDES.key=key; if(insertFileCipherWithDES(fileCipherWithDES)<0) { return -2; } return 0; }
int main(int argc, char* argv[]) { char md5_sum[MD5_LEN + 1]; if(!CalcFileMD5("./MD5C.c", md5_sum)) puts("Error occured!"); else printf("Success! MD5 sum is :%s \n", md5_sum); }
int recv_Image(int *client_sockfd , int CNT) { char sourceMD5[MD5_LEN + 1]; char fileMD5[MD5_LEN + 1]; char sendMsg[BUFFER_SIZE]; char recvMsg[BUFFER_SIZE]; char cntString[BUFFER_SIZE]; char name[BUFFER_SIZE] = "../files/recvImage/recv_image"; //复制到recvImage内 strcat(name , IMAGE_FORMAT); char osg_image_name[BUFFER_SIZE] = "../files/originalImage/recv_image";//从server端接收的源图片 strcat(osg_image_name , IMAGE_FORMAT); /*int t = CNT % 1001; memset(cntString , 0 , sizeof(cntString)); int idx = 0; while(t > 0) { int tmp = t % 10; cntString[idx++] = tmp + '0'; t = t / 10; } strcat(osg_image_name , cntString); strcat(osg_image_name , ".png"); strcat(name , cntString); strcat(name , ".png"); //printf("%s\n" , osg_image_name); */ int RET = 0; //接收源文件的MD5值 RET = recv(*client_sockfd , sourceMD5 , sizeof(sourceMD5) , 0); send(*client_sockfd , "get sourceMD5" , sizeof(sendMsg) , 0); RET = recv(*client_sockfd , recvMsg , sizeof(recvMsg) , 0); send(*client_sockfd , "get file size" , sizeof(sendMsg) , 0); //如果服务器读取文件失败 if(strcmp(recvMsg , "failed") == 0) return -1; if(RET <= 0) { printf("\nConnection error!\n"); return -1; } //获取接收osgImage的size和传送次数 int lSize; memcpy(&lSize , recvMsg , sizeof(lSize)); //RET = recv(*client_sockfd , recvMsg , sizeof(int) , 0); int cnt = lSize / BYTES_PER_TRANS; //memcpy(&cnt , recvMsg , sizeof(cnt)); printf("lSize: %d BYTES_PER_SEND: %d\n" , lSize , BYTES_PER_TRANS); printf("\n"); if(lSize == 0) return -1; FILE* fp = fopen(osg_image_name, "w"); if(NULL == fp) { printf("File:\t%s Can Not Open To Write\n", osg_image_name); fclose(fp); return -1; } //连续recv,直到recv完整数据 int INDEX = 0; //0 ~ cnt - 1 for(int i=0; i<cnt; i++) { RET = 0; while(RET < BYTES_PER_TRANS) { RET += recv(*client_sockfd , recv_osg_image + RET , sizeof(recv_osg_image) , 0); //printf("count: %d RET: %d\n" , i + 1 , RET); } if(RET <= 0) { printf("\nclient_recvImage line 75: recv error!\n"); fclose(fp); return -1; } //接收osgImage并写入本地 //printf("count: %d recvSize: %d\n" , i + 1 , RET); if(fwrite(recv_osg_image, sizeof(char), RET, fp) < RET){ printf("File: %s write failed", recv_osg_image); } INDEX += RET; sprintf(sendMsg , "Received"); send(*client_sockfd , sendMsg , sizeof(sendMsg) , 0); } // cnt RET = 0; while(RET < lSize - cnt * BYTES_PER_TRANS) { RET += recv(*client_sockfd , recv_osg_image + RET , sizeof(recv_osg_image) , 0); //printf("count: %d RET: %d\n" , cnt + 1 , RET); } if(RET <= 0) { printf("\nclient_recvImage line 141: Connection error!\n"); fclose(fp); return -1; } //接收osgImage并写入本地 //printf("count: %d recvSize: %d\n\n" , cnt + 1 , RET); if(fwrite(recv_osg_image, sizeof(char), RET, fp) < RET){ printf("File: %s write failed", recv_osg_image); } fclose(fp); //sprintf(sendMsg , "Client has received your osg image!\n"); send(*client_sockfd , sendMsg , sizeof(sendMsg) , 0); //MD5 judge if(!CalcFileMD5(osg_image_name, fileMD5)) puts("Error occured!"); else //printf("Success! MD5 sum is :%s \n", fileMD5); if(strcmp(sourceMD5 , fileMD5) != 0) { printf("client_recvImage line 159: MD5 not equal!\n"); return -1; } //复制到另一个文件 if(copyFile(name , osg_image_name) == -1) return -1; //printf("recvImage line 169: copyed!\n"); return 0; }
int CheckMD5(const char *namePath, const char *md5) { char calcMD5[33] = {0}; CalcFileMD5(namePath, calcMD5); return strcmp(md5, calcMD5); }