Java MD5 String md5(String string)

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

Description

Use md5 method to encryption

License

Open Source License

Parameter

Parameter Description
string input string

Declaration

private static String md5(String string) 

Method Source Code

//package com.java2s;
/**/*w w w.  j a va 2  s . c  o  m*/
 * Copyright 2017 Institute of Computing Technology, Chinese Academy of Sciences.
 * Licensed under the terms of the Apache 2.0 license.
 * Please see LICENSE file in the project root for terms
 */

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**
     * Use md5 method to encryption
     * @param string input string
     */
    private static String md5(String string) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("md5");
            md.update(string.getBytes());
            byte[] md5Bytes = md.digest();
            return bytes2Hex(md5Bytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * Convert byteArray to String
     * @param byteArray
     * @return string of input byteArray
     */
    private static String bytes2Hex(byte[] byteArray) {
        StringBuffer strBuf = new StringBuffer();
        for (int i = 0; i < byteArray.length; i++) {
            if (byteArray[i] >= 0 && byteArray[i] < 16) {
                strBuf.append("0");
            }
            strBuf.append(Integer.toHexString(byteArray[i] & 0xFF));
        }
        return strBuf.toString();
    }
}

Related

  1. md5(String str)
  2. MD5(String str)
  3. MD5(String string)
  4. md5(String string)
  5. md5(String string)
  6. md5(String string)
  7. md5(String string)
  8. md5(String string)
  9. md5(String string)