⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 md6sum.c

📁 目前最新的MD6 Hash 算法源码
💻 C
📖 第 1 页 / 共 2 页
字号:
  if ((err=md6_full_init(&st,d,K,keylen,L,r)))
    { printf("Bad MD6 parameters; can't initialize md6. "
	     "errcode = %d\n",err);
      return;
    }
  if (print_input_output)
    compression_hook = compression_hook_1;
}

void hash_update(char* data, 
		 uint64_t databitlen)
{ int err;
  if ((err=md6_update(&st, 
		      (unsigned char *)data, 
		      databitlen)))
    { printf("MD6 update error. error code: %d\n",err);
      return;
    }
}

void hash_final()
{ int err;
  if ((err=md6_final(&st,NULL)))
    { printf("MD6 finalization error. error code: %d\n",err);
      return;
    }
  end_timer();
}

void hash_filep(FILE *inFile)
{ uint64_t bytes;
  char data[1024];
  if (inFile==NULL)
    { printf("hash_filep has NULL input file pointer.\n");
      return;
    }
  hash_init();
  while ((bytes = fread (data, 1, 1024, inFile)) != 0)
    hash_update(data,bytes*8);
  hash_final();
}

void hash_stdin()
{ hash_filep(stdin);
}

void hash_file( char *filename )
{ FILE *inFile = fopen (filename, "rb");
  if ( inFile == NULL ) 
    { printf("%s can't be opened.\n", filename);
      return;
    }
  hash_filep(inFile);
  fclose(inFile);
}

void hash_b(uint64_t bitlen)
/* Hash dummy input file of length bitlen bits.
** File (hex) repeats with period 7:
**   11 22 33 44 55 66 77 11 22 33 44 55 66 77 11 22 33 ...
*/
{ int i;
  char data[700];  /* nice if length is multiple of 7 for periodicity */
  for (i=0;i<700;i++)
    data[i] =  0x11 + (char)((i % 7)*(0x11));
  hash_init();
  while (bitlen>0)
    { uint64_t part_len = min(700*8,bitlen);
      hash_update(data,part_len);
      bitlen = bitlen - part_len;
    }
  hash_final();
}

void hash_B(uint64_t B)
/* Hash dummy input file of length B bytes.
*/
{ hash_b(B*8);
}

/* Routines to handle command-line options
*/

void optd(char *optstr)
{ /* set MD6 digest length 
  ** -dnn sets digest length to nn, 1 <= nn <= 512 
  */ 
  int dopt = get_int(optstr+2);
  if (dopt<1 || dopt>512)
    printf("Illegal digest size option %s ignored.\n",optstr);
  else
    d = dopt;
  r = md6_default_r(d,keylen);
}

void opth()
{ /* print md6sum help string */
  printf(help_string);
}

void optK(char *optstr)
{ /* set MD6 key */
  optstr += 2;
  keylen = 0;
  while (*optstr && keylen<64) K[keylen++] = *optstr++;
  K[keylen] = 0;
  r = md6_default_r(d,keylen);
}

void optL(char *optstr)
{ /* set MD6 mode parameter
  ** -Lnn where 0<=n<=64
  ** nn = 0 means fully sequential
  ** nn = 64 means fully hierarchical
  ** intermediate values give a blended approach
  */
  int Lopt = get_int(optstr+2);
  if (Lopt<0 || Lopt>64)
    printf("Illegal L options %s ignored.\n",optstr);
  else
    L = Lopt;
}

void optr(char *optstr)
{ /* set MD6 number of rounds
  ** -rnn where 0<=r<=255
  */
  int ropt = get_int(optstr+2);
  if (r<0 || r>255)
    printf("Illegal r options %s ignored.\n",optstr);
  else
    r = ropt;
    
}

void optM(char *optstr)
{ /* hash a message given as a command-line argument */
  char *p = optstr + 2;
  msglenbytes = 0;
  while (*p && msglenbytes<4990) msg[msglenbytes++] = *p++;
  msg[msglenbytes] = 0;
  hash_init();
  hash_update(msg,msglenbytes*8);
  hash_final();
}

void optb(char *optstr)
/* -bnnnn hash dummy file of length nnnn bits */
{ 
  uint64_t bitlen = get_int(optstr+2);
  hash_b(bitlen);
}

void optB(char *optstr)
/* -Bnnnn hash dummy file of length nnnn bytes */
{ uint64_t B = get_int(optstr+2);
  hash_b(B*8);
}

void check_line(char *line)
/* print filename if its hash doesn't agree with what's given in line 
*/
{ char *x;
  char hexhashval[1000];
  int hexhashlen;
  char filename[1000];
  char decfilename[1000];
  int filenamelen;
  /* collect hash value */
  x = line;
  hexhashlen = 0;
  while (*x && *x!=' ' && hexhashlen<900) 
    hexhashval[hexhashlen++] = *x++;
  hexhashval[hexhashlen] = 0;
  if (*x != ' ')
    { printf("Badly formed hash check file line: %s\n",line);
      return;
    }
  x++;
  /* collect filename and decode it */
  filenamelen = 0;
  while (*x && *x != '\n' && filenamelen<900) 
    filename[filenamelen++] = *x++;
  filename[filenamelen] = 0;
  decode(decfilename,filename);
  if (filename[0]=='-')
    { /* handle "filenames" starting with '-' specially,
      ** even though legitimate filenames may start with '-'.
      */
      if (filenamelen==1) 
	return; 	/* skip standard input */
      switch( filename[1] )
	{ 
	case 'M': optM(decfilename); break;
	case 'b': optb(decfilename); break;
	case 'B': optB(decfilename); break;
	default: hash_file(decfilename); break;
	}
    }
  else
    { /* now compute hash of file */
      hash_file(decfilename);
    }
  if (strcmp(hexhashval,(char *)st.hexhashval)!=0)
    printf("%s\n",decfilename);
}

void optc(int argc, char **argv, int i)
/* Recompute hashes, and check them for current validity.
** -c file     causes hashes from given file to be checked.
**             (This file is e.g. output from previous run of md6sum.)
**             Names of files that's don't hash the same are printed.
*/
{
  FILE *checkfilep;
  char line[1000];

  if (i == argc-1)
    { printf("md6sum error: no file given for -c option.\n");
      return;
    }
  checkfilep = fopen(argv[i+1],"r");
  if (checkfilep==NULL)
    { printf("Hash check file %s can't be opened.\n",argv[i+1]);
      return;
    }
  while (fgets( line, 990, checkfilep))
    { if (strlen(line)==0) continue;
      line[strlen(line)-1] = 0; /* kill '\n' */
      if (line[0]=='-') /* handle md6sum option */
	{ if (strlen(line)==1) 
	    { printf("Hash check file contains illegal line with single '-'; ignored.\n");
	    }
	  switch ( line[1] )
	    { 
	    case 'd': optd(line); break;
	    case 'K': optK(line); break;
	    case 'L': optL(line); break;
	    case 'r': optr(line); break;
	    case ' ': break; /* ignore lines starting with '- ' or '--' */
	    case '-': break; 
	    default: printf("Unrecognized md6sum option in check file: %s\n",argv[i]);
 	             break;
	    };
	  continue;
	}
      /* now handle typical line with hash value */
      check_line(line);
    }
  fclose(checkfilep);
}

void optt()
/* turn on timing printout */
{
  print_times = 1;
}

/* Routine to print hashvalue filename line.
** Prints time_of_day first if it hasn't been printed already.
*/

int tod_printed = 0;

void print_tod()
{ /* print time-of-day if it hasn't been printed yet. */
  time_t now;
  if (!tod_printed)
    { time(&now);
      printf("-- %s",ctime(&now));
      tod_printed = 1;
    }
}

void opti()
/* turn on printing of input/output values for compression function calls */
{ print_tod();
  print_input_output = 1;
  outFile = stdout;
}

void optI()
/* turn on printing of input/output values AND intermediate values */
{ print_tod();
  print_input_output = 1;
  print_intermediate = 1;
  outFile = stdout;
}

void opts(char *optstr)
{ uint64_t trials = get_int(optstr+2);
  uint64_t i;
  int err;
  double elapsed_time;
  uint64_t elapsed_ticks;
  if (trials == 0) trials = 1;
  start_timer();
  for (i=0;i<trials;i++)
    { st.initialized = 0;
      if ((err = md6_full_init(&st,d,K,keylen,L,r)))
	printf("MD6 initialization error %d for option -s.\n",err);
    }
  end_timer();
  elapsed_time = end_time - start_time;
  printf("-- Setup trials = %lld\n",(long long int)trials);
  printf("-- Elapsed time = %.3f seconds\n",elapsed_time);
  elapsed_ticks = end_ticks - start_ticks;
  printf("-- Total clock ticks = %lld\n",(long long int)elapsed_ticks);
  printf("-- Clock ticks / setup = %lld\n",(long long int)(elapsed_ticks/trials));
}

void print_hash(char *filename)
{ print_tod();
  if (print_input_output == 0)
    printf("%s %s\n",st.hexhashval,filename);
  else
    printf("Final hash value = %s\n",st.hexhashval);
  print_time(); /* running time */
}



/* main
*/
int main(int argc, char **argv)
{ int i;
  char encfilename[1000];

  /* set default md6 parameter settings */
  d = 256;                
  keylen = 0;
  L = 64;
  r = md6_default_r(d,keylen);

  /* Process command line options */
  if ( argc == 1 )
    { hash_stdin();
      print_hash("-");
    }
  for (i=1;i<argc;i++)
    {
      if (strlen(argv[i])==0) continue;
      if (argv[i][0]!='-')
	{ /* argument is filename */
	  hash_file(argv[i]);
	  encode(encfilename,argv[i]);
	  print_hash(encfilename);
	  print_time();
	}
      else
	{
	  if (strlen(argv[i])==1)
	    { hash_stdin();
	      print_hash("-");
	      continue;
	    }
	  switch ( argv[i][1] )
	    { 
	    case 'b': optb(argv[i]); print_hash(argv[i]); break;
	    case 'B': optB(argv[i]); print_hash(argv[i]); break;
	    case 'c': optc(argc,argv,i); i+=1; break;
	    case 'd': optd(argv[i]); printf("-d%d\n",d); break;
	    case 'h': opth(); break;
	    case 'i': opti(); break;
	    case 'I': optI(); break;
	    case 'K': optK(argv[i]); printf("-K%s\n",K); break;
	    case 'L': optL(argv[i]); printf("-L%d\n",L); break;
	    case 'M': optM(argv[i]); print_hash(argv[i]); break;
	    case 'r': optr(argv[i]); printf("-r%d\n",r); break;
	    case 's': opts(argv[i]); break; 
	    case 't': optt(); break;
	    default:  hash_file(argv[i]);
	              encode(encfilename,argv[i]);
		      print_hash(encfilename);
		      print_time();
		      break;
	    }
	}
    }
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -