Java MD5 String md5(String s)

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

Description

Returns a 32 chararacter hexadecimal representation of an MD5 hash of the given String.

License

Open Source License

Parameter

Parameter Description
s the String to hash

Return

the md5 hash

Declaration

public static String md5(String s) 

Method Source Code

//package com.java2s;
import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;

public class Main {
    private static MessageDigest digest;

    /**//from   ww  w.j av  a 2 s  . c  o  m
     * Returns a 32 chararacter hexadecimal representation of an MD5 hash of the given String.
     * 
     * @param s the String to hash
     * @return the md5 hash
     */
    public static String md5(String s) {
        try {
            byte[] bytes = digest.digest(s.getBytes("UTF-8"));
            StringBuilder b = new StringBuilder(32);
            for (byte aByte : bytes) {
                String hex = Integer.toHexString((int) aByte & 0xFF);
                if (hex.length() == 1)
                    b.append('0');
                b.append(hex);
            }
            return b.toString();
        } catch (UnsupportedEncodingException e) {
            // utf-8 always available
        }
        return null;
    }
}

Related

  1. md5(String raw)
  2. md5(String s)
  3. md5(String s)
  4. MD5(String s)
  5. md5(String s)
  6. md5(String s)
  7. md5(String s)
  8. md5(String s)
  9. md5(String s)