Java MD5 String md5Hex(String s)

Here you can find the source of md5Hex(String s)

Description

Generates the hex md5sum of a string.

License

Open Source License

Parameter

Parameter Description
s The string to md5sum

Return

The 32-character hex md5sum

Declaration

public static String md5Hex(String s) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.security.MessageDigest;

public class Main {
    /**/*from  w w  w .jav  a 2 s .  c om*/
     * Generates the hex md5sum of a string.
     * @param s The string to md5sum
     * @return The 32-character hex md5sum
     */
    public static String md5Hex(String s) {
        StringBuffer sb = new StringBuffer();
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            return null;
        }

        md.update(s.getBytes());
        byte[] digest = md.digest();
        for (int i = 0; i < digest.length; i++) {
            int b = digest[i] & 0xff;
            String hex = Integer.toHexString(b);
            if (hex.length() == 1)
                sb.append("0");
            sb.append(hex);
        }
        return sb.toString();
    }
}

Related

  1. md5Hex(String message)
  2. md5Hex(String message)
  3. md5Hex(String message)
  4. md5Hex(String message)
  5. md5Hex(String message)
  6. md5hex(String source)
  7. md5Hex(String str)
  8. md5raw(String data)
  9. md5Representation(String data)