3

I am getting the same value for IOS and Winows md5 hashing but in the case of java i am getting a different value,

IOS code for md5 hashing

- (NSString*)md5HexDigest:(NSString*)input
{
    NSData *data = [input dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5([data bytes], (CC_LONG)[data length], result);

    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
    for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
        [ret appendFormat:@"%02x",result[i]];
    }
    return ret;
}

Windows Code for md5 hashing

private static string GetMD5(string text)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            byte[] message = UE.GetBytes(text);

            MD5 hashString = new MD5CryptoServiceProvider();
            string hex = "";

            hashValue = hashString.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }

Java Code for md5 hasing: Tried with UTF-8,16,32 , but not maching with the IOS and Windows

 public String MD5(String md5)  {
   try {

       String dat1 = md5.trim();
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(dat1.getBytes("UTF-16"));
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
          sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
       }
        System.out.println("Digest(in hex format):: " + sb.toString());
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
    }
   catch(UnsupportedEncodingException e)
   {
   }
    return null;
}

thanks

7
  • What if you use UTF-16LE? Commented Mar 24, 2015 at 14:17
  • 1
    Does your weird Integer.toHexString() code actually give correct results? Commented Mar 24, 2015 at 14:19
  • UTF-16LE worked , thanks Kayaman. Commented Mar 24, 2015 at 14:30
  • @Kayaman Just curious: why does making use of UTF-16LE work here? What was wrong with the existing code by OP? What is the difference, other words? Thanks in advance. Commented Mar 24, 2015 at 14:34
  • 2
    @Unheilig Using just UTF-16 puts the BOM (0xFEFF or 0xFFFE) in the beginning, to specify the endianness. Using UTF-16BE or UTF-16LE explicitly leaves the BOM out (apparently). Unicode is a b*tch. Commented Mar 24, 2015 at 14:40

1 Answer 1

1

Here a short overview what getBytes() returns related to the specified character set (all credits go to @Kayaman)

"123".getBytes("UTF-8")   :                31 32 33 
"123".getBytes("UTF-16")  : FE FF 00 31 00 32 00 33 
"123".getBytes("UTF-16LE"):       31 00 32 00 33 00 
"123".getBytes("UTF-16BE"):       00 31 00 32 00 33 

It shows that the BOM is added only if the endianness is not specified. Then it depends on your architecture if LE or BE is used.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.