filerail/crypto.h
// computes md5 hash of zip file
computes md5 hash of zip file
computes md5 hash of zip file
int filerail_md5(uint8_t *hash, const char *zip_filename) {
	int i, exit_status;
	size_t nbytes;
	off_t size;
	uint8_t buffer[BUFFER_SIZE];
	struct stat stat_path;
	FILE *fp;
	MD5_CTX mdContext;
	exit_status = 0;
	if ((fp = fopen(zip_filename, "rb")) == NULL) {
		LOG(LOG_USER | LOG_ERR, "crypto.h filerail_md5 fopen\n");
		exit_status = -1;
		goto clean_up;
	}
	if (stat(zip_filename, &stat_path) == -1) {
		LOG(LOG_USER | LOG_ERR, "crypto.h filerail_md5 stat\n");
		exit_status = -1;
		return exit_status;
	}
	size = stat_path.st_size;
  MD5_Init(&mdContext);
  while (size != 0) {
  	nbytes = fread((void *)buffer, 1, min(BUFFER_SIZE, size), fp);
  	if (nbytes != min(BUFFER_SIZE, size) && ferror(fp)) {
			LOG(LOG_USER | LOG_ERR, "crypto.h filerail_md5 fread\n");
			exit_status = -1;
			goto clean_up;
  	}
  	MD5_Update(&mdContext, buffer, nbytes);
  	size -= nbytes;
  }
  MD5_Final(hash, &mdContext);
  PRINT(printf("Hash: "));
  for(i = 0; i < MD5_DIGEST_LENGTH; i++) {
  	PRINT(printf("%02x", hash[i]));
  }
  PRINT(printf("\n"));
	clean_up:
	if (fp != NULL) {
		fclose(fp);
	}
	return exit_status;
}
int filerail_md5 ( uint8_t * hash , const char * zip_filename ) { int i , exit_status ; size_t nbytes ; off_t size ; uint8_t buffer [ BUFFER_SIZE ] ; struct stat stat_path ; FILE * fp ; MD5_CTX mdContext ; exit_status = 0 ; if ( ( fp = fopen ( zip_filename , " " ) ) == NULL ) { LOG ( LOG_USER | LOG_ERR , " \n " ) ; exit_status = -1 ; goto clean_up ; } if ( stat ( zip_filename , & stat_path ) == -1 ) { LOG ( LOG_USER | LOG_ERR , " \n " ) ; exit_status = -1 ; return exit_status ; } size = stat_path . st_size ; MD5_Init ( & mdContext ) ; while ( size != 0 ) { nbytes = fread ( ( void * ) buffer , 1 , min ( BUFFER_SIZE , size ) , fp ) ; if ( nbytes != min ( BUFFER_SIZE , size ) && ferror ( fp ) ) { LOG ( LOG_USER | LOG_ERR , " \n " ) ; exit_status = -1 ; goto clean_up ; } MD5_Update ( & mdContext , buffer , nbytes ) ; size -= nbytes ; } MD5_Final ( hash , & mdContext ) ; PRINT ( printf ( " " ) ) ; for ( i = 0 ; i < MD5_DIGEST_LENGTH ; i ++ ) { PRINT ( printf ( " " , hash [ i ] ) ) ; } PRINT ( printf ( " \n " ) ) ; clean_up : if ( fp != NULL ) { fclose ( fp ) ; } return exit_status ; }
computes md5 hash of zip file
computes md5 hash of zip file