0

I use the following code for generating md5 for blob data in database.

md5Checksum.update(byte[] --- read from database);
String result = new BigInteger(1,md5Checksum.digest()).toString(16);

The checksum i obtain is varying in length(30-32) for different byte arrays. For 31 char length checksum, as I understood can be an effect of the removal of leading zeros. (I handled it by adding leading zeros)

Can any one tell me why I am getting a 30 char hash in some cases?

Thanks, Mithun

herry
  • 1,708
  • 3
  • 17
  • 30
kjkszpj
  • 81
  • 1
  • 1
  • 4

2 Answers2

0

Do not convert a digest to a number!

Use something like:

byte[] b = md5Checksum.digest();
// now convert these bytes to chars

There are many different methods to convert byte[] to HexStrings:

public class HexConverter {

    // thanks to http://www.rgagnon.com/javadetails/java-0596.html
    static private final String HEXES = "0123456789ABCDEF";

    public String toHex(byte[] raw) {
        if (raw == null) {
            return null;
        }
        final StringBuilder hex = new StringBuilder(2 * raw.length);
        for (final byte b : raw) {
            hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
        }
        return hex.toString();
    }
}

or from Getting a File's MD5 Checksum in Java (this link also shows how to se DigestUtils from Apache Commons Codec)

public static String getMD5Checksum(String filename) throws Exception {
    byte[] b = createChecksum(filename);
    String result = "";

    for (int i=0; i < b.length; i++) {
        result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
    }
    return result;
}
Community
  • 1
  • 1
close2
  • 308
  • 1
  • 7
0

There is a chance that the high n bits could be zero. As you convert the byte[] to a number.If n=4,then you could lose one '0' char at the beginning. If n=8, then you lose '00' at the beginning of your md5 hex code.

bob zhou
  • 103
  • 1
  • 7