2 Replies Latest reply on Oct 4, 2016 5:29 AM by rash

    Calculating MD5 for a long file

    oguzkerem

      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[i] =  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