Java Utililty Methods MD5 Sum

List of utility methods to do MD5 Sum

Description

The list of methods to do MD5 Sum are organized into topic(s).

Method

Stringmd5sum(InputStream file)
mdsum
InputStream in = file;
try {
    MessageDigest digest = MessageDigest.getInstance("MD5");
    byte[] buffer = new byte[8192];
    int read = 0;
    while ((read = in.read(buffer)) > 0) {
        digest.update(buffer, 0, read);
    byte[] md5sum = digest.digest();
    BigInteger bigInt = new BigInteger(1, md5sum);
    return bigInt.toString(16);
} catch (Exception e) {
    throw new RuntimeException("Unable to process file for MD5", e);
} finally {
    if (in != null)
        try {
            in.close();
        } catch (IOException e) {
            throw new RuntimeException("Unable to close input stream for MD5 calculation", e);
Stringmd5sum(InputStream in)
mdsum
in.reset();
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] dataBytes = new byte[1024];
int nread;
while ((nread = in.read(dataBytes)) != -1) {
    md.update(dataBytes, 0, nread);
return getMd5String(md);
...
byte[]md5sum(String data)
mdsum
return MessageDigest.getInstance("MD5").digest(data.getBytes());
Stringmd5sum(String input)
Return the MD5 checksum of the given string
MessageDigest digest = null;
try {
    digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException ex) {
    throw new RuntimeException("Unable to compute md5sum for string", ex);
assert (digest != null);
digest.update(input.getBytes());
...
Stringmd5sum(String inString)
Calculate the MD5 checksum of the input string
MessageDigest algorithm = null;
try {
    algorithm = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException nsae) {
    System.err.println("Cannot find digest algorithm");
    return null;
byte[] defaultBytes = inString.getBytes();
...
Stringmd5sum(String message)
Generates an MD5 sum for the given string.
MessageDigest md5;
byte[] md5sumBytes;
String sum = null;
try {
    md5 = MessageDigest.getInstance(encodeAlgorithm);
    md5sumBytes = md5.digest(message.getBytes(characterEncoding));
    sum = (new BigInteger(1, md5sumBytes)).toString(md5Base);
} catch (NoSuchAlgorithmException e) {
...
Stringmd5Sum(String msg)
md Sum
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(msg.getBytes(), 0, msg.length());
return new String(byteArrayToHexString(md.digest()));
Stringmd5sum(String str)
mdsum
try {
    return md5sum(str.getBytes("UTF-8"));
} catch (UnsupportedEncodingException uee) {
    return null;
Stringmd5sum(String string)
mdsum
return calcHash(string, "MD5");
Stringmd5sum(String url)
mdsum
if (url == null)
    return "";
StringBuilder sb = new StringBuilder();
try {
    for (byte b : MessageDigest.getInstance("MD5").digest(url.getBytes("UTF-8")))
        sb.append(Integer.toHexString(0x100 | (b & 0xff)).substring(1));
} catch (NoSuchAlgorithmException e) {
    throw new RuntimeException(e);
...