FlacAudioHeader.getMd5() does not process character "0" correctly

Issue #160 resolved
Xaver created an issue

A call to FlacAudioHeader.getMd5() does not always yield the correct hash. For example, a call might result in the following value:

ecc38f78313d594a749fe27d1d1e (length = 28)

While the returned value should be:

ec0c038f783103d5094a749fe27d1d1e (length = 32)

It seems that somehow zeros (0) are omitted from the result:

ec c 38f7831 3d5 94a749fe27d1d1e
ec0c038f783103d5094a749fe27d1d1e

Comments (5)

  1. IJabz repo owner

    I dont know much about this, but this is how it is currently done, can you see whats wrong with it ?

        private String readMd5()
        {
            StringBuilder sb = new StringBuilder();
            if(rawdata.limit()>=34)
            {
                for (int i = 18; i < 34; i++)
                {
                    byte dataByte = rawdata.get(i);
                    sb.append(String.format("%x", dataByte));
                }
            }
            return sb.toString();
        }
    
  2. Xaver reporter

    The issue is due to the formatting. This should be a correct & fast alternative (adapted from this stackoverflow answer):

      private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
    
      private String readMd5()
      {
          char[] hexChars = new char[32]; // MD5 is always 32 characters
    
          if(rawdata.limit()>=34)
          {
              for (int i = 0; i < 16; i++)
              {
                  int v = rawdata.get(i + 18) & 0xFF; // Offset 18
                  hexChars[i * 2] = hexArray[v >>> 4];
                  hexChars[i * 2 + 1] = hexArray[v & 0x0F];
              }
          }
    
          return new String(hexChars);
      }
    
  3. Log in to comment