pointers - Changing a c code to work line by line -


#include <mhash.h> #include <stdio.h> #include <stdlib.h>  int main(void)  {         int i;         mhash td;         unsigned char buffer;         unsigned char *hash;          td = mhash_init(mhash_whirlpool);          if (td == mhash_failed) exit(1);          while (fread(&buffer, 1, 1, stdin) == 1) {              mhash(td, &buffer, 1);         }          hash = mhash_end(td);          (i = 0; < mhash_get_block_size(mhash_whirlpool); i++) {             printf("%.2x", hash[i]);         }         printf("\n");          exit(0);  } 

hi, have above code mhash example page. need change it, keep reading stdin, , calculate hash line line, instead of waiting eof

cat textfile | whirlpool_line_hash 

my understanding keep while loop (which waits eof) , make hash calculation , print after received 10 (0x0a). after print mhash needs reset, right? not c @ all, need fast program, want in c. fail @ comparing pointer integer ;-) can please help?

it works except 1 little caveat, hash buffer returned mhash_end dynamically allocated buffer, it's better free when done using it:

free(hash); 

you can use fgets read 1 line @ most. in terms of performance, read , feed hash char each time not best thing do, instead, can read entire line , feed block hash function update. try this:

char line[4096]; int len; while (fgets(line, sizeof line, stdin) != null) { // read line each time     len = strlen(line);     char *p = strrchr(line, '\n');     if (p != null)         mhash(td, line, len - 1);  // strip new line     else         mhash(td, line, len); }  hash = mhash_end(td); (i = 0; < mhash_get_block_size(mhash_whirlpool); i++) {     printf("%.2x", hash[i]); } free(hash); 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -