user19355
user19355

Reputation: 61

MD5 Hash Issue with Java

A java implementation creates two different digest for a same input string, if i run as stand alone application or running inside a web application.

The standalone application matches with oracle dbms The implementation is

    MessageDigest md5 = MessageDigest.getInstance("MD5");

    if (md5 != null) {
        md5.reset();
        newHashByte = md5.digest(msg.getBytes());
    }

    newHash = convertToString(newHashByte);

Hex to String conversion implementation is

StringBuffer result = new StringBuffer(64);

for (int i = 0; i < digestBits.length; i++)
    hexDigit(result, digestBits[i]);

return result.toString();

Highly appreciate if you could help us resolving this.

Upvotes: 1

Views: 717

Answers (2)

ZZ Coder
ZZ Coder

Reputation: 75496

I suspect you have different default encodings. Use the correct encoding like this,

newHashByte = md5.digest(msg.getBytes("utf-8"));

Upvotes: 7

rmeador
rmeador

Reputation: 25694

Where does msg come from in each case? I think it's likely you have a newline character on the end in one case but not the other. It's also possible that your character encodings are set differently somehow in the two scenarios. I highly doubt that anything else in your example is changing except msg.

Upvotes: 2

Related Questions