文件或者字符串的加密,可以获取文件或者字符串的MD5值或sha1,SHA-256, SHA-384, SHA-512

本文介绍了一个用于文件加密及校验的实用工具类,包括MD5、SHA系列算法和CRC32的实现,适用于文件或字符串的加密处理,提供了获取文件MD5值、文件夹内文件MD5值、字符串MD5值以及文件CRC32校验码的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


import java.io.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.*;
import java.util.zip.*;

/**
 * 此算法主要是针对文件或者字符串进行加密的算法,可以获取文件或者字符串的MD5值或sha1,SHA-256, SHA-384, SHA-512 的加密值
 * 整理了一些网上的代码留着备用
 * @author 张永志 1279938986@qq.com 相关密码的加密解密查询可以参考 https://www.cmd5.com/
 * 参考文章:https://blog.youkuaiyun.com/lichao_ustc/article/details/30457273
 * 参考文章:https://blog.youkuaiyun.com/plean/article/details/40187347
 *
 */

public class DigestUtil {
    /**
     *
     * @param file
     * @param algorithm
     *            所请求算法的名称 for example: MD5, SHA1, SHA-256, SHA-384, SHA-512
     *            etc.
     * @return
     */
    public static String getFileMD5(File file, String algorithm) {

        if (!file.isFile()) {
            return null;
        }

        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[1024];
        int len;

        try {
            digest = MessageDigest.getInstance(algorithm);
            in = new FileInputStream(file);
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    }

    /**
     * 获取文件夹中文件的MD5值
     *
     * @param dirFile
     * @param algorithm
     *            所请求算法的名称 for example: MD5, SHA1, SHA-256, SHA-384, SHA-512
     *            etc.
     * @param listChild
     *            是否递归子目录中的文件
     * @return
     */
    public static Map<String, String> getDirMD5(File dirFile, String algorithm,
            boolean listChild) {

        if (!dirFile.isDirectory()) {
            return null;
        }

        // <filepath,algCode>
        Map<String, String> pathAlgMap = new HashMap<String, String>();
        String algCode;
        File files[] = dirFile.listFiles();

        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory() && listChild) {
                pathAlgMap.putAll(getDirMD5(file, algorithm, listChild));
            } else {
                algCode = getFileMD5(file, algorithm);
                if (algCode != null) {
                    pathAlgMap.put(file.getPath(), algCode);
                }
            }
        }
        return pathAlgMap;
    }

    public static String getCRC32(String string) {
        CRC32 crc32 = new CRC32();
        crc32.update(string.getBytes());
        return Long.toHexString(crc32.getValue()).toUpperCase();
    }

    /**
     *
     * @param s
     *            需要加密的字符串
     * @param algorithm
     *            所请求算法的名称 for example: MD5, SHA1, SHA-256, SHA-384, SHA-512
     *            etc.
     * @return 返回加密后的字符串
     */

    public static String getStringMd5(String s, String algorithm) {
        try {
            // 返回实现指定摘要算法的 MessageDigest对象 参数可以是sha或者MD5
            MessageDigest md = MessageDigest.getInstance(algorithm);
            // 使用指定的 byte 数组对摘要进行最后更新,然后完成摘要计算。
            byte[] bytes = md.digest(s.getBytes("utf-8"));
            return toHex(bytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static String toHex(byte[] bytes) {
        // 个人在设计秘钥时可以更改字符串“0123456789ABCDEF”
        final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
        StringBuilder ret = new StringBuilder(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            // 取byte数组元素的向右移动4位后与0x0f 即将元素的高四位变为第四位作为下标取HEX_DIGITS的元素
            ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
            // 取byte数组元素后与0x0f 即将元素的底四位作为下标取HEX_DIGITS的元素
            ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
        }
        return ret.toString();
    }

    /**
     * 获取文件的 CRC32校验码
     *
     * @param fileUri
     * @return
     */
    public static String getCRC32(File file) {
        CRC32 crc32 = new CRC32();
        // MessageDigest.get
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            byte[] buffer = new byte[8192];
            int length;
            while ((length = fileInputStream.read(buffer)) != -1) {
                crc32.update(buffer, 0, length);
            }
            return Long.toHexString(crc32.getValue()).toLowerCase();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (fileInputStream != null)
                    fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值