package com.oop.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.Map;
public class CrowdFundingUtils {
/*判断map集合 因为collection和map集合为同级所以下面不能用所以重写*/
public static <K,V> boolean mapEffective(Map<K,V> map){
return map!=null&&map.size()>0;
}
/*判断集合是否有效*/
public static <E> boolean collecionEffective(Collection<E> collection){
return collection!=null&&collection.size()>0;
}
/*声明方法判断字符串是否有效*/
public static boolean stringEffective(String source){
return source!=null&&source.length()>0;
}
/*
* MD5加密工具方法
* */
public static String md5(String source){
//判断传入的明文字符串是否有效
if(!stringEffective(source)){
throw new RuntimeException("明文不是有效的字符串,请核对后在操作");
}
String algortm="MD5";
//声明Stringbuider
StringBuilder builder=new StringBuilder();
//加密使用的字符数组
char[] chars={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
try {
//执行加密的核心对象
MessageDigest digest=MessageDigest.getInstance(algortm);
//将要加密的明文转换成字节数组的形式
byte[] inpurByte=source.getBytes();
//执行加密
byte[] outputByte=digest.digest(inpurByte);
//遍历outputBytes
for (int i=0;i<inpurByte.length;i++){
//获取当前字节数值
byte b=outputByte[i];
//获取底四位的值
int lowValue=b&15;
//右移四位和15做与运算
int highValue=(b>>4)&15;
//以高四位、低四位的值为下标从字符数组中获取对应的字符
char highCharacter=chars[highValue];
char lowCharacter=chars[lowValue];
//拼接
builder.append(highCharacter).append(lowCharacter);
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return builder.toString();
}
}
MD5加密工具类
最新推荐文章于 2021-04-20 10:34:21 发布
311

被折叠的 条评论
为什么被折叠?



