hz-zhhq-app-service/greenH5modul/.svn/pristine/cf/cf28127c1052f5813d0c8b3095a...

64 lines
1.6 KiB
Plaintext
Raw Normal View History

2025-01-21 13:12:35 +08:00
package com.nationalelectric.greenH5.identityAuth.util.devon.secure;
import com.nationalelectric.greenH5.identityAuth.util.devon.HexUtils;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5工具类
*
* TODO: 2017/8/14 重构为Hash接口digest方法提供MD5, SHA1, SHA256等实现类
*
* @author <a href="mailto:93785732@qq.com">ZhengDaHong@fzfx</a>
* @since 2017/5/2 11:18
*/
public class Md5Utils {
public static final String HASH_ALGORITHM_NAME = "MD5";
private static final String CHAR_ENCODING = "UTF-8";
/**
* 对传入的字符串做MD5哈希处理返回大写结果
*
* @param source
* @return
*/
public static String md5WithUpperCase(String source) {
return md5(source, false);
}
/**
* 对传入的字符串做MD5哈希处理返回小写结果
*
* @param source
* @return
*/
public static String md5WithLowerCase(String source) {
return md5(source, true);
}
/**
* 对传入的字符串做MD5哈希处理
*
* @param source
* @param useLowerCase
* @return
*/
private static String md5(String source, boolean useLowerCase) {
try {
MessageDigest md5 = MessageDigest.getInstance(HASH_ALGORITHM_NAME);
return HexUtils.encode(md5.digest(source.getBytes(CHAR_ENCODING)), useLowerCase);
} catch (NoSuchAlgorithmException e) {
// Formalization, should not occurs actually.
throw new IllegalStateException("No such algorithm which named 'MD5'", e);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Unsupported encoding named 'UTF-8'", e);
}
}
}