Java获取字符串(16bit,32bit)和文件MD5工具

自己搜集整理的MD5的Java工具类,支持16位32位64位的字符串大小写MD5,使用Apache的库实现文件的MD5

还有很多整理的工具类,项目地址:https://github.com/KingBoyWorld/aurora.git,下载后切换到utils_feature分支,Common模块中有相应的工具包。

package com.kingboy.common.utils.md5;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.poi.util.IOUtils;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5,String-File
 * @Author kingboy
 * @Date 2017/7/22 下午1:00
 * @Description MD5Utils is used to
 */
public class MD5Utils {

    private static final String ALGORITHM_MD5 = "MD5";

    private static final String UTF_8 = "UTF-8";

    /**
     * MD5 16bit 小写.
     * @param readyEncryptStr ready encrypt string
     * @return String encrypt result string
     * @throws NoSuchAlgorithmException
     * */
    public static final String MD5_16bit_lower(String readyEncryptStr) throws NoSuchAlgorithmException {
        if(readyEncryptStr != null){
            return MD5Utils.MD5_32bit_lower(readyEncryptStr).substring(8, 24);
        }else{
            return null;
        }
    }

    /**
     * MD5 16bit 大写.
     * @param readyEncryptStr ready encrypt string
     * @return String encrypt result string
     * @throws NoSuchAlgorithmException
     * */
    public static final String MD5_16bit_upper(String readyEncryptStr) throws NoSuchAlgorithmException {
        return MD5_16bit_lower(readyEncryptStr).toUpperCase();
    }

    /**
     * MD5 32bit 小写.
     * @param readyEncryptStr ready encrypt string
     * @return String encrypt result string
     * @throws NoSuchAlgorithmException
     * */
    public static final String MD5_32bit_lower(String readyEncryptStr) throws NoSuchAlgorithmException{
        if(readyEncryptStr != null){
            //Get MD5 digest algorithm's MessageDigest's instance.
            MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
            //Use specified byte update digest.
            md.update(readyEncryptStr.getBytes());
            //Get cipher text
            byte [] b = md.digest();
            //The cipher text converted to hexadecimal string
            StringBuilder su = new StringBuilder();
            //byte array switch hexadecimal number.
            for(int offset = 0,bLen = b.length; offset < bLen; offset++){
                String haxHex = Integer.toHexString(b[offset] & 0xFF);
                if(haxHex.length() < 2){
                    su.append("0");
                }
                su.append(haxHex);
            }
            return su.toString();
        }else{
            return null;
        }
    }

    /**
     * MD5 32bit 大写.
     * @param readyEncryptStr ready encrypt string
     * @return String encrypt result string
     * @throws NoSuchAlgorithmException
     * */
    public static final String MD5_32bit_upper(String readyEncryptStr) throws NoSuchAlgorithmException{
        return MD5_32bit_lower(readyEncryptStr).toUpperCase();
    }

    /**
     * MD5 64bit 大写.
     * @param readyEncryptStr ready encrypt string
     * @return String encrypt result string
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     * */
    public static final String MD5_64bit(String readyEncryptStr) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
        BASE64Encoder base64Encoder = new BASE64Encoder();
        return base64Encoder.encode(md.digest(readyEncryptStr.getBytes(UTF_8)));
    }


    /**
     * 获取文件MD5,使用apache的org.apache.commons.codec.digest.DigestUtils;
     * @param filePath 文件路径
     * @return String
     */
    public static String MD5_File(String filePath) throws Exception {
        try (FileInputStream fis= new FileInputStream(filePath)) {
            return DigestUtils.md5Hex(IOUtils.toByteArray(fis));

        }
    }


    public static void main(String[] args) throws Exception {
         System.out.println("16位小写:" + MD5Utils.MD5_16bit_lower("king"));
         System.out.println("16位大写:" + MD5Utils.MD5_16bit_upper("king"));
         System.out.println("32位小写:" + MD5Utils.MD5_32bit_lower("king"));
         System.out.println("32位大写:" + MD5Utils.MD5_32bit_upper("king"));
         System.out.println("64位大写:" + MD5Utils.MD5_64bit("king"));
         System.out.println("FILEMD5:" + MD5Utils.MD5_File("/Users/kingboy/Desktop/1.txt"));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值