Java MD5 String MD5(String src)

Here you can find the source of MD5(String src)

Description

MD

License

Open Source License

Declaration

public static String MD5(String src) 

Method Source Code

//package com.java2s;
/**//from w  w w . ja  v  a2 s  .  co  m
 *  Copyright (c)  2016-2020 Weibo, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Weibo, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Weibo.
 */

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

public class Main {

    public static String MD5(String src) {
        if (src == null) {
            return "";
        }
        return MD5(src.getBytes());
    }

    public static String MD5(byte[] src) {
        if (src == null) {
            return "";
        }
        byte[] result = null;
        try {
            MessageDigest alg = MessageDigest.getInstance("MD5");
            result = alg.digest(src);
        } catch (NoSuchAlgorithmException e) {

        }
        return byte2hex(result);
    }

    public static String byte2hex(byte[] b) {
        if (b == null) {
            return "";
        }
        StringBuffer hs = new StringBuffer();
        String stmp = null;
        for (int n = 0; n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0XFF);
            if (stmp.length() == 1) {
                hs.append("0");
            }
            hs.append(stmp);
        }
        return hs.toString();
    }
}

Related

  1. md5(String source)
  2. md5(String source)
  3. md5(String source)
  4. md5(String source, int bit)
  5. MD5(String sourceStr)
  6. md5(String src)
  7. md5(String src)
  8. md5(String src)
  9. md5(String srcStr)