64 lines
1.6 KiB
Plaintext
64 lines
1.6 KiB
Plaintext
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);
|
||
}
|
||
}
|
||
|
||
}
|