tags:

views:

115

answers:

2

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.

+2  A: 

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.

rmeador
+4  A: 

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

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