package org.springblade.openinterface.dianxin.util;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author lidexi
 * @date 2021/6/23 18:55
 */

public class MD5Util {

	private static final Charset charset = Charset.forName("utf-8");

	/**
	 * 工具类，避免实例化
	 */
	private MD5Util() {
	}

	/**
	 * MD5加密
	 *
	 * @param str 待加密字符串
	 * @return 加密后字符串
	 */
	public static byte[] encode(String str) {
		byte[] bytes = str.getBytes(charset);
		if (bytes == null) {
			return bytes;
		}
		MessageDigest digest;
		try {
			digest = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
		digest.update(bytes);
		return digest.digest();
	}
}
