Java MD5 String md5(String s)

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

Description

md

License

Open Source License

Declaration

public static String md5(String s) 

Method Source Code


//package com.java2s;
import java.security.MessageDigest;

public class Main {
    public static String md5(String s) {
        String retn = null;//from   www. j  a v  a 2s  .c o m
        if (null == s || s.length() < 1) {
            return retn;
        }
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(s.getBytes("UTF-8"));
            retn = toHexString(bytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return retn;
    }

    private static String toHexString(byte[] bytes) {
        StringBuilder retn = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() == 1) {
                retn.append("0");
            }
            retn.append(hex);
        }

        return retn.toString();
    }
}

Related

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