Java MD5 String md5(String input)

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

Description

md

License

Apache License

Declaration

public static String md5(String input) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static final String ENCRY_MD5 = "md5";

    public static String md5(String input) {
        return encrypt(input, ENCRY_MD5);
    }//from   w w w  .ja  va 2s .  c om

    public static String encrypt(String input, String... algorithms) {
        String algorithm = ENCRY_MD5;
        if (input == null || "".equals(input.trim())) {
            throw new IllegalArgumentException("Please input the encrypted content !");
        }
        if (algorithms.length > 0) {
            algorithm = algorithms[0];
        }
        byte[] bytes = null;
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            md.update(input.getBytes("UTF8"));
            bytes = md.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return byte2hex(bytes);
    }

    private static String byte2hex(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; ++i) {
            sb.append(Integer.toHexString((bytes[i] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    }
}

Related

  1. md5(String Input)
  2. md5(String input)
  3. md5(String input)
  4. md5(String input)
  5. md5(String input)
  6. md5(String input)
  7. md5(String input)
  8. md5(String input)
  9. md5(String input)