【Java工具类】摘要算法之MD5、SHA1工具类

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestUtil {
    
    
    //以16进制编码进行输出
    final static char hex[] = {
    
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    /**
     * 对文件进行MD5摘要
     */
    public static String MD5(String path) {
    
    

        String pathName = path;
        String md5 = "";
        try {
    
    
            File file = new File(pathName);
            FileInputStream ins = new FileInputStream(file);
            FileChannel ch = ins.getChannel();
            MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(byteBuffer);
            ins.close();
            md5 = toHexString(md.digest());
        } catch (NoSuchAlgorithmException | IOException e) {
    
    
            e.printStackTrace();
        }
        return md5;
    }

    /**
     * 对文件进行SHA1摘要
     *
     * @param path
     * @return
     */
    public static String SHA1(String path) {
    
    
        String pathName = path;
        String sha1 = "";
        try {
    
    
            File file = new File(pathName);
            FileInputStream ins = new FileInputStream(file);
            FileChannel ch = ins.getChannel();
            MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            sha.update(byteBuffer);
            ins.close();
            sha1 = toHexString(sha.digest());
        } catch (NoSuchAlgorithmException | IOException e) {
    
    
            e.printStackTrace();
        }
        return sha1;
    }

    public static String toHexString(byte[] tmp) {
    
    
        String s;
        char str[] = new char[tmp.length * 2];
        int k = 0;
        for (int i = 0; i < tmp.length; i++) {
    
    
            byte byte0 = tmp[i];
            str[k++] = hex[byte0 >>> 4 & 0xf];
            str[k++] = hex[byte0 & 0xf];
        }
        s = new String(str);
        return s;
    }


    public static void main(String[] args) {
    
    
        String path = "E:\\data\\istack-commons-runtime-3.0.8.jar";
        System.out.println(MD5(path));
        System.out.println(SHA1(path));
    }
}

main方法执行结果

FFC22E80F61AB7295C02493856B4C520
AFB12C586558A39B53039EE8C37F20ADC9E02F07

也可以使用是Apache下面的一个加解密开发包commons-codec

<!--commons-codec是Apache开源组织提供的用于摘要运算、编码解码的包。常见的编码解码工具Base64、MD5、Hex、SHA1、DES等-->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.15</version>
        </dependency>

DigestUtils.sha1(data);
DigestUtils.md5Hex(data)

猜你喜欢

转载自blog.csdn.net/qq877728715/article/details/115213133