int check_md5_file(char * filename, int wd) { unsigned char md5[16] = {0}; struct __file_hash_table * file_hash_table; int i,j; char * file_name = strrchr(filename, '/'); file_name++; md5_file(filename, md5); file_hash_table = inotify_struct[wd].file_hash_table; for(i = 0; i < inotify_struct[wd].file_number && file_hash_table != NULL; i++){ if( strcmp(file_hash_table -> file_name, file_name) == 0){ if (strncmp( (const char *)(file_hash_table -> md5), (const char *)md5, 16) == 0){ return(0); } else{ for(j = 0; j < 16; j++) file_hash_table -> md5[j] = md5[j]; return(1); } } file_hash_table = file_hash_table -> next; } return(1); }
static csiebox_protocol_status sync_file_meta(csiebox_client* client, char* path) { char* relative = convert_to_relative_path(client, path); if (!relative) { fprintf(stderr, "convert relative fail: %s\n", path); return CSIEBOX_PROTOCOL_STATUS_FAIL; } csiebox_protocol_meta meta; memset(&meta, 0, sizeof(meta)); meta.message.header.req.magic = CSIEBOX_PROTOCOL_MAGIC_REQ; meta.message.header.req.op = CSIEBOX_PROTOCOL_OP_SYNC_META; meta.message.header.req.client_id = client->client_id; meta.message.header.req.datalen = sizeof(meta) - sizeof(csiebox_protocol_header); meta.message.body.pathlen = strlen(relative); lstat(path, &(meta.message.body.stat)); if ((meta.message.body.stat.st_mode & S_IFMT) == S_IFDIR) { } else { md5_file(path, meta.message.body.hash); } send_message(client->conn_fd, &meta, sizeof(meta)); send_message(client->conn_fd, relative, strlen(relative)); free(relative); csiebox_protocol_header header; recv_message(client->conn_fd, &header, sizeof(header)); if (header.res.status == CSIEBOX_PROTOCOL_STATUS_FAIL) { fprintf(stderr, "sync meta fail: %s\n", path); return; } return header.res.status; }
int main(int argc, char **argv) { int leng; st=ft=ht=vt=0; md_parse_opt(argc, argv); if(vt == 1) { md_show_version(); return 0; } if(ht == 1) { md_show_version(); md_show_help(); return 0; } if(st == 1) { md5_string(); } if(ft == 1) { md5_file(); } return 0; }
uint Md5::GetFileDigest(void* digest16,LPCWSTR fn){ String str = fn; char buf[MAX_PATH]; int len = str.ToMultiByte(buf,MAX_PATH,CP_THREAD_ACP); buf[len] = 0; return md5_file(buf,(byte*)digest16)?0:16; }
int main(int argc, char *argv[]) { int i; char retmd5[33]; for (i = 1; i < argc; i++) md5_file(argv[i], retmd5); }
int main(int, char** argv) { char out[33]; double nbytes; md5_file(argv[1], out, nbytes); printf("%s\n%f bytes\n", out, nbytes); return 0; }
char * cryMd5File(const char *sFile, char sOutBuf[16]) { if (md5_file(sFile, (unsigned char *)sOutBuf) != 0) { return NULL; } return sOutBuf; }
static int md5_file_wrap( const char *path, unsigned char *output ) { #if defined(POLARSSL_FS_IO) return md5_file( path, output ); #else ((void) path); ((void) output); return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); #endif }
static int md5_wrapper( char *filename, unsigned char *sum ) { int ret = md5_file( filename, sum ); if( ret == 1 ) fprintf( stderr, "failed to open: %s\n", filename ); if( ret == 2 ) fprintf( stderr, "failed to read: %s\n", filename ); return( ret ); }
/** Get the md5 sum of file. * @param filename name of the file * @return the checksum on success, NULL on error * @addtogroup alpm_misc */ char SYMEXPORT *alpm_compute_md5sum(const char *filename) { unsigned char output[16]; ASSERT(filename != NULL, return NULL); /* defined above for OpenSSL, otherwise defined in md5.h */ if(md5_file(filename, output) > 0) { return NULL; } return hex_representation(output, 16); }
int sign_file(const char* path, R_RSA_PRIVATE_KEY& key, DATA_BLOCK& signature) { char md5_buf[MD5_LEN]; double file_length; DATA_BLOCK in_block; int retval; retval = md5_file(path, md5_buf, file_length); if (retval) return retval; in_block.data = (unsigned char*)md5_buf; in_block.len = (unsigned int)strlen(md5_buf); retval = encrypt_private(key, in_block, signature); if (retval) return retval; return 0; }
int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "usage: %s: Teenagnt.exe (unpacked one)\n", argv[0]); exit(1); } const char * fname = argv[1]; uint8 digest[16]; if (!md5_file(fname, digest, 0)) { fprintf(stderr, "cannot calculate md5 for %s", fname); exit(1); } const uint8 ethalon[16] = { 0x51, 0xb6, 0xd6, 0x47, 0x21, 0xf7, 0xc4, 0xb4, 0x98, 0xbf, 0xc0, 0xf3, 0x23, 0x01, 0x3e, 0x36, }; if (memcmp(digest, ethalon, 16) != 0) { fprintf(stderr, "cannot extract data, your md5: "); print_hex(stderr, digest, 16); fprintf(stderr, ", need md5: "); print_hex(stderr, ethalon, 16); fprintf(stderr, ", sorry\n"); exit(1); } FILE *fin = fopen(fname, "rb"); if (fin == NULL) { perror("opening input file"); exit(1); } const char * dat_name = "teenagent.dat"; FILE *fout = fopen(dat_name, "wb"); if (fout == NULL) { perror("opening output file"); exit(1); } //0x0200, 0xb5b0, 0x1c890 extract(fout, fin, 0x00200, 0xb3b0, "extracting code segment"); extract(fout, fin, 0x0b5b0, 0xe790, "extracting data segment"); extract(fout, fin, 0x1c890, 0x8be2, "extracting second data segment"); fclose(fin); fclose(fout); fprintf(stderr, "please run \"gzip -n %s\"\n", dat_name); return 0; }
// done (BUSY) static csiebox_protocol_status sync_file_meta(csiebox_client* client, char* path) { char* relative = convert_to_relative_path(client, path); if (!relative) { fprintf(stderr, "convert relative fail: %s\n", path); return CSIEBOX_PROTOCOL_STATUS_FAIL; } csiebox_protocol_meta meta; memset(&meta, 0, sizeof(meta)); meta.message.header.req.magic = CSIEBOX_PROTOCOL_MAGIC_REQ; meta.message.header.req.op = CSIEBOX_PROTOCOL_OP_SYNC_META; meta.message.header.req.client_id = client->client_id; meta.message.header.req.datalen = sizeof(meta) - sizeof(csiebox_protocol_header); meta.message.body.pathlen = strlen(relative); lstat(path, &(meta.message.body.stat)); if ((meta.message.body.stat.st_mode & S_IFMT) == S_IFDIR) { } else { md5_file(path, meta.message.body.hash); } // send message check if busy int request = 1; csiebox_protocol_header header; while (1) { send_message(client->conn_fd, &request, sizeof(int)); recv_message(client->conn_fd, &header, sizeof(header)); if (header.res.status == CSIEBOX_PROTOCOL_STATUS_BUSY) { fprintf(stderr, "server busy\n"); sleep(3); } else break; } // start sync meta send_message(client->conn_fd, &meta, sizeof(meta)); send_message(client->conn_fd, relative, strlen(relative)); memset(&header, 0, sizeof(header)); recv_message(client->conn_fd, &header, sizeof(header)); switch (header.res.status) { case CSIEBOX_PROTOCOL_STATUS_OK: case CSIEBOX_PROTOCOL_STATUS_MORE: free(relative); return header.res.status; case CSIEBOX_PROTOCOL_STATUS_FAIL: fprintf(stderr, "sync meta fail: %s\n", path); return -1; default: fprintf(stderr , "unknown status %x\n", header.res.status); return -1; } return -1; }
void add_file_hash(char *filename, int wd) { struct __file_hash_table * file_hash_table; char * file_name = strrchr(filename, '/'); file_name++; file_hash_table = malloc(sizeof(struct __file_hash_table)); file_hash_table -> next = inotify_struct[wd].file_hash_table; file_hash_table -> file_name = strdup(file_name); inotify_struct[wd].file_hash_table = file_hash_table; inotify_struct[wd].file_number++; md5_file(filename, file_hash_table->md5); }
int pkg_to_old(struct pkg *p) { struct pkg_file *f = NULL; char md5[MD5_DIGEST_LENGTH * 2 + 1]; p->type = PKG_OLD_FILE; while (pkg_files(p, &f) == EPKG_OK) { if (f->sum[0] == '\0') continue; if (md5_file(f->path, md5) == EPKG_OK) strlcpy(f->sum, md5, sizeof(f->sum)); } return (EPKG_OK); }
void stor_file_hash(char *path, int wd) { struct dirent* ent = NULL; DIR *pDir; char dir[512]; char filename[1024]; struct stat statbuf; struct __file_hash_table * file_hash_table; if((pDir = opendir(path)) == NULL) { fprintf( stderr, "Cannot open directory:%s\n", path ); return; } while((ent = readdir(pDir)) != NULL) { //得到读取文件的绝对路径名 snprintf(dir, 512,"%s/%s", path, ent->d_name ); //得到文件信息 lstat(dir, &statbuf); //判断是目录还是文件 if(S_ISDIR(statbuf.st_mode)){ continue; } else { char *p = strrchr(ent->d_name, '.'); if(p && (strcmp(p, file_suf) == 0)){ sprintf(filename, "%s/%s", path ,ent->d_name); file_hash_table = malloc(sizeof(struct __file_hash_table)); file_hash_table -> next = inotify_struct[wd].file_hash_table; file_hash_table -> file_name = strdup(ent->d_name); inotify_struct[wd].file_hash_table = file_hash_table; inotify_struct[wd].file_number++; //printf("Match : %s\n", filename); md5_file(filename, file_hash_table->md5); } else { //printf( "Not match %s\n", ent->d_name ); } } } closedir(pDir); print_inotify_struct(wd); }
int pkg_to_old(struct pkg *p) { struct pkg_file *f = NULL; char md5[MD5_DIGEST_LENGTH * 2 + 1]; const char *sum; p->type = PKG_OLD_FILE; while (pkg_files(p, &f) == EPKG_OK) { sum = pkg_file_cksum(f); if (sum == NULL || sum[0] == '\0') continue; if (md5_file(pkg_file_path(f), md5) == EPKG_OK) strlcpy(f->sum, md5, sizeof(f->sum)); } return (EPKG_OK); }
static adr_data_t * create_resource(rad_locale_t *rlocale, const char *pname, char *file) { unsigned char hbuf[MD5_DIGEST_LENGTH]; if (md5_file(file, hbuf) != 0) { return (NULL); } adr_data_t *result = adr_data_new_struct(&t__ResourceDescriptor); adr_struct_set(result, "token", file_to_token(rlocale, pname, file)); adr_struct_set(result, "file", adr_data_new_string(file, LT_COPY)); adr_struct_set(result, "hashAlgorithm", adr_data_new_string("MD5", LT_CONST)); adr_struct_set(result, "hash", adr_data_new_opaque(hbuf, MD5_DIGEST_LENGTH, LT_COPY)); return (result); }
int main(int argc, char *argv[]) { if (argc != 2) { printHelp(argv[0]); return -1; } PAKFile out; // First step: Write out all resources. outputAllResources(out); // Second step: Write all game version information std::vector<GameDef> games; outputAllGames(out, games); // Third step: Write index file byte *const indexBuffer = new byte[8 + 2 * games.size()]; byte *dst = indexBuffer; WRITE_BE_UINT32(dst, kKyraDatVersion); dst += 4; WRITE_BE_UINT32(dst, games.size()); dst += 4; for (std::vector<GameDef>::const_iterator i = games.begin(), end = games.end(); i != end; ++i) { WRITE_BE_UINT16(dst, *i); dst += 2; } if (!out.addFile("INDEX", indexBuffer, 8 + 2 * games.size())) { error("couldn't write INDEX file"); } if (!out.saveFile(argv[1])) { error("couldn't save changes to '%s'", argv[1]); } uint8 digest[16]; if (!md5_file(argv[1], digest, 0)) error("couldn't calc. md5 for file '%s'", argv[1]); FILE *f = fopen(argv[1], "ab"); if (!f) error("couldn't open file '%s'", argv[1]); if (fwrite(digest, 1, 16, f) != 16) error("couldn't write md5sum to file '%s'", argv[1]); fclose(f); return 0; }
bool CFlashTool::check_md5( const std::string & filename, const std::string & smd5) { unsigned char md5[16]; unsigned char omd5[16]; const char * ptr = smd5.c_str(); if(strlen(ptr) < 32) return false; //printf("[flashtool] check file %s md5 %s\n", filename.c_str(), ptr); for(int i = 0; i < 16; i++) omd5[i] = FROMHEX(ptr[i*2])*16 + FROMHEX(ptr[i*2+1]); md5_file(filename.c_str(), 1, md5); if(memcmp(md5, omd5, 16)) return false; return true; }
char * doMD5File(const char *filename, char *buf, int is_target) { if (SummaryOpt) { struct stat st; if (stat(filename, &st) == 0) { uint64_t size = st.st_size; if (is_target) CountTargetReadBytes += size; else CountSourceReadBytes += size; } } #ifdef WITH_LIBMD return MD5File(filename, buf); #else return md5_file(filename, buf); #endif }
/* Callback from build_checksums */ static void *cksum_cb(void * vp) { file_group *gp = vp; lint_t *file = gp->grp_stp; /* Iterate over all files in group */ while(file && file != gp->grp_enp) { /* See md5.c */ md5_file(file); file=file->next; } /* free group, for each thread */ if(gp) { free(gp); } /* same as pthread_exit() */ return NULL; }
int main(int argc, char *argv[]) { struct passwd *pw = getpwuid(getuid()); const char *homedir = pw->pw_dir; FileRecord f; struct stat sbuf; char buf[2048]; struct dirent *ent; uint8_t hash_buf[16]; char *db_path; db_path = (char *)malloc(sizeof(char) * (strlen(homedir) + strlen("/.cbcache") + 1)); strcpy(db_path, homedir); strcpy(db_path + strlen(db_path), "/.cbcache"); db_init(db_path); DIR *d; d = opendir("/home/nathan/chunk_backup"); ent = readdir(d); while(ent != NULL) { if(!((strcmp(ent->d_name, ".") == 0) || (strcmp(ent->d_name, "..") == 0))) { strcpy(f.filename, ent->d_name); snprintf(buf, 2048, "/home/nathan/chunk_backup/%s", ent->d_name); stat(buf, &sbuf); f.size = sbuf.st_size; f.atime = sbuf.st_atime; f.mtime = sbuf.st_mtime; f.ctime = sbuf.st_ctime; md5_file(buf, hash_buf); memcpy(&f.hash_hi, hash_buf, 8); memcpy(&f.hash_lo, hash_buf+8, 8); db_insert(&f); } ent = readdir(d); } exit(0); }
int main( int argc, char *argv[] ) { int i; unsigned char digest[16]; if(argc<2) { printf("use: parrot_md5 <file> ...\n"); return 1; } for(i=1;i<argc;i++) { if(parrot_md5(argv[i],digest)>=0 || md5_file(argv[i],digest)) { printf("%s %s\n",md5_string(digest),argv[i]); } else { fprintf(stderr,"parrot_md5: %s: %s\n",argv[i],strerror(errno)); } } return 0; }
// Snarfed from checksquashfs bool CFlashTool::MD5Check(const std::string squashfsimage, const std::string checkmd5) { char md5binary[17]; char md5result[33]; md5_file(squashfsimage.c_str(), 1, (unsigned char*) md5binary); int i; for (i=0; i<16; i++) { snprintf(&md5result[i*2], 3, "%02x", md5binary[i]); } if (strcmp(md5result, checkmd5.c_str())) { printf("[CFlashTool] md5 check failed!\n"); return false; } printf("[CFlashTool] md5 check successfull\n"); return true; }
/* md5sum */ int tasktable_md5sum(TASKTABLE *tasktable) { unsigned char md5str[MD5_LEN]; int i = 0, ret = -1; char *p = NULL, *file = NULL; if(tasktable && tasktable->running_task.id >= 0 && tasktable->running_task.id < tasktable->ntask && md5_file(tasktable->running_task.file, md5str) == 0) { MUTEX_LOCK(tasktable->mutex); p = tasktable->running_task.md5; for(i = 0; i < MD5_LEN; i++) { p += sprintf(p, "%02x", md5str[i]); } ret = 0; tasktable->dump_task(tasktable); MUTEX_UNLOCK(tasktable->mutex); } return ret; }
int checkFile(char* filename, char* result) { int count; int anz; unsigned char md5buffer[16]; char md5string[40]=""; //get the file-md5sum and convert to string.. if( md5_file(filename, 1, (unsigned char*) &md5buffer)) { strcpy(result, "not found"); return -1; } for(count=0;count<16;count++) { char tmp[6]; sprintf((char*) &tmp, "%02x", md5buffer[count] ); strcat(md5string, tmp); } anz = sizeof(ucodes) / sizeof(*ucodes); for(count=0;count<anz;count++) { if( strcmp(ucodes[count].md5, md5string)==0) { strcpy(result, ucodes[count].name); return 1; } } strcpy(result, "unknown"); return 0; }
void CShairPlay::audio_set_coverart(void *_this, void *, const void *_buffer, int _buflen) { CShairPlay *T = (CShairPlay *) _this; int orig = _buflen; unlink(COVERART); unlink(COVERART_M2V); FILE *file = fopen(COVERART, "wb"); if (file) { g_RCInput->killTimer(T->coverArtTimer); while (_buflen > 0) _buflen -= fwrite((unsigned char *)_buffer+orig-_buflen, 1, _buflen, file); fclose(file); T->gotCoverArt = true; unsigned char md5sum[16]; md5_file(COVERART, 1, md5sum); if (T->initialized && memcmp(md5sum, T->last_md5sum, sizeof(md5sum))) T->showCoverArt(); memcpy(T->last_md5sum, md5sum, sizeof(md5sum)); } }
int main(int argc,char **argv){ int opt; while((opt=getopt(argc,argv,":vf:h"))!=-1){ switch (opt){ case 'v': show_version(); break; case 'f': md5_file(optarg); break; case ':': printf("need a filename\n"); break; case 'h': case '?': printf("unknow option\n"); show_help(); break; default: printf("a a %s\n",optarg); } } while(optind<argc){ md5_string(argv[optind]); optind++; } return 0; }
static int pkg_create_from_dir(struct pkg *pkg, const char *root, struct packing *pkg_archive) { char fpath[MAXPATHLEN]; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; char *m; int ret; const char *mtree; bool developer; struct stat st; char sha256[SHA256_DIGEST_LENGTH * 2 + 1]; int64_t flatsize = 0; ucl_object_t *obj; if (pkg_is_valid(pkg) != EPKG_OK) { pkg_emit_error("the package is not valid"); return (EPKG_FATAL); } obj = pkg_annotation_lookup(pkg, "relocated"); /* * Get / compute size / checksum if not provided in the manifest */ while (pkg_files(pkg, &file) == EPKG_OK) { const char *pkg_path = pkg_file_path(file); const char *pkg_sum = pkg_file_cksum(file); snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", obj ? pkg_object_string(obj) : "", pkg_path); if (lstat(fpath, &st) != 0 || S_ISLNK(st.st_mode)) continue; if (file->size == 0) file->size = (int64_t)st.st_size; flatsize += file->size; if (pkg_sum == NULL || pkg_sum[0] == '\0') { if (pkg->type == PKG_OLD_FILE) { if (md5_file(fpath, sha256) != EPKG_OK) return (EPKG_FATAL); } else { if (sha256_file(fpath, sha256) != EPKG_OK) return (EPKG_FATAL); } strlcpy(file->sum, sha256, sizeof(file->sum)); } } pkg_set(pkg, PKG_FLATSIZE, flatsize); if (pkg->type == PKG_OLD_FILE) { const char *desc, *display, *comment; char oldcomment[BUFSIZ]; pkg_old_emit_content(pkg, &m); packing_append_buffer(pkg_archive, m, "+CONTENTS", strlen(m)); free(m); pkg_get(pkg, PKG_DESC, &desc, PKG_MESSAGE, &display, PKG_COMMENT, &comment); packing_append_buffer(pkg_archive, desc, "+DESC", strlen(desc)); packing_append_buffer(pkg_archive, display, "+DISPLAY", strlen(display)); snprintf(oldcomment, sizeof(oldcomment), "%s\n", comment); packing_append_buffer(pkg_archive, oldcomment, "+COMMENT", strlen(oldcomment)); } else { /* * Register shared libraries used by the package if * SHLIBS enabled in conf. Deletes shlib info if not. */ struct sbuf *b = sbuf_new_auto(); pkg_register_shlibs(pkg, root); pkg_emit_manifest_sbuf(pkg, b, PKG_MANIFEST_EMIT_COMPACT, NULL); packing_append_buffer(pkg_archive, sbuf_data(b), "+COMPACT_MANIFEST", sbuf_len(b)); sbuf_clear(b); pkg_emit_manifest_sbuf(pkg, b, 0, NULL); sbuf_finish(b); packing_append_buffer(pkg_archive, sbuf_data(b), "+MANIFEST", sbuf_len(b)); sbuf_delete(b); } pkg_get(pkg, PKG_MTREE, &mtree); if (mtree != NULL) packing_append_buffer(pkg_archive, mtree, "+MTREE_DIRS", strlen(mtree)); while (pkg_files(pkg, &file) == EPKG_OK) { const char *pkg_path = pkg_file_path(file); snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", obj ? pkg_object_string(obj) : "", pkg_path); ret = packing_append_file_attr(pkg_archive, fpath, pkg_path, file->uname, file->gname, file->perm); developer = pkg_object_bool(pkg_config_get("DEVELOPER_MODE")); if (developer && ret != EPKG_OK) return (ret); } while (pkg_dirs(pkg, &dir) == EPKG_OK) { const char *pkg_path = pkg_dir_path(dir); snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", obj ? pkg_object_string(obj) : "", pkg_path); ret = packing_append_file_attr(pkg_archive, fpath, pkg_path, dir->uname, dir->gname, dir->perm); developer = pkg_object_bool(pkg_config_get("DEVELOPER_MODE")); if (developer && ret != EPKG_OK) return (ret); } return (EPKG_OK); }