MD5加密工具类

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();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值