Calculating MD5 for a long file

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
user_2170816
Level 3
Level 3
5 replies posted 5 questions asked First question asked

Hello,

I am trying to calculate MD5 of a about 600k file while it is being dowloaded. Firstly, I started to work with cyrpto snip. I have changed it a little bit like following:

char md5_test_vectors[] =

"MIIFiTCCBHGgAwIBAgIQb0tfWSndfGHKL7ONsxZ1AzANBgkqhkiG9w0BAQsFADBCMIIFiTCCBHGgAwIBAgIQb0tfWSndfGHKL7Oa\

MIIFiTCCBHGgAwIBAgIQb0tfWSndfGHKL7ONsxZ1AzANBgkqhkiG9w0BAQsFADBCMIIFiTCCBHGgAwIBAgIQb0tfWSndfGHKL7Oa";

void sw_md5_test(void)

{

    md5_context md5_ctx;

    wiced_time_t t1, t2;

    uint8_t hash_value[MD5_LENGTH];

    char test_vector_ptr[100];

    int i = 0;

    int chunk_count = 0;

    uint32_t file_size = strlen(md5_test_vectors);

    uint8_t offset = 100;

    WPRINT_APP_INFO( ( "file_size= %d \r\n", file_size ) );

    WPRINT_APP_INFO( ( "file= %s \r\n", md5_test_vectors ) );

    md5_starts( &md5_ctx );

  while(chunk_count < 2)

  {

  for(i = 0; i<offset ; i++){

  test_vector_ptr =  md5_test_vectors[i+chunk_count*offset];

  }

  test_vector_ptr[offset] = '\0';

  if(file_size - offset * chunk_count < offset){

  offset =  file_size - offset * chunk_count;

  }

  WPRINT_APP_INFO( ( "1nMD5(%s) =  \r\n", test_vector_ptr ) );

  md5_update( &md5_ctx, (unsigned char *)test_vector_ptr, offset );

  WPRINT_APP_INFO( ( "chunk_count = %d \r\n", chunk_count ) );

  WPRINT_APP_INFO( ( "offset = %d \r\n", offset ) );

  chunk_count ++;

  }

  md5_finish( &md5_ctx, (unsigned char *)hash_value );

  dump_bytes( hash_value, MD5_LENGTH );

}

  WPRINT_APP_INFO( ( "offset = %d \r\n", offset ) );

  chunk_count ++;

  }

  md5_finish( &md5_ctx, (unsigned char *)hash_value );

  du}mp_bytes( hash_value, MD5_LENGTH );

}

md5_test_vectors is just a dummy string which have 200 length. As you can see, this dummy string is composed of 2 equal parts.

If I calculate the first 100 length, the result is true. However the over all string does not give the correct result. I am checking the correct result with a online calculater: Free online md5 hash calculator based on php script

Breifly, if I make md5_starts, md5_update, md5_finish for a short input, result is correct.

But, md5_starts, md5_update . . . md5_update, md5_finish loop for a long input string is not giving a correct output.

I hope I explained my problem clearly.

Thank you in advance.

Oguz Kerem.

md5_test_vectors

0 Likes
1 Solution
Anonymous
Not applicable

Yes, we have tested this with different md5 hash calculator and found that if you include "\" in the test vector the generated hash value is different in all the cases. So remove "\" and use the vector.

View solution in original post

2 Replies
JoYi_1844281
Level 6
Level 6
100 sign-ins 100 replies posted 50 likes received

remark this line of code

test_vector_ptr[offset] = '\0'; 

Anonymous
Not applicable

Yes, we have tested this with different md5 hash calculator and found that if you include "\" in the test vector the generated hash value is different in all the cases. So remove "\" and use the vector.