Java实现计算指定文件的SHA256

SHA-256

SHA-256(Secure Hash Algorithm 256-bit)是一种密码学安全哈希算法,用于将任意长度的数据转换为固定长度的哈希值,通常为256位(32字节)。SHA-256是SHA-2(Secure Hash Algorithm 2)系列算法的一部分,被广泛应用于密码学和数据完整性验证等领域。

我们可以使用SHA-256验证文件的完整性

Java实现方式

java 复制代码
public class DigestUtils {
    public static String calculateSHA256(String filePath) throws IOException, NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        try (FileInputStream fis = new FileInputStream(filePath)) {
            byte[] buffer = new byte[8192];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                md.update(buffer, 0, bytesRead);
            }
        }

        byte[] bytes = md.digest();
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }
}

Kotlin实现

Kotlin 复制代码
object DigestUtils {
    @Throws(IOException::class, NoSuchAlgorithmException::class)
    fun calculateSHA256(filePath: String): String {
        val md = MessageDigest.getInstance("SHA-256")
        FileInputStream(filePath).use { fis ->
            val buffer = ByteArray(8192)
            var bytesRead: Int
            while (fis.read(buffer).also { bytesRead = it } != -1) {
                md.update(buffer, 0, bytesRead)
            }
        }

        val bytes = md.digest()
        val sb = StringBuilder()
        for (b in bytes) {
            sb.append(String.format("%02x", b))
        }
        return sb.toString()
    }
}

MD5

MD5(Message Digest Algorithm 5)是一种广泛使用的哈希函数,用于将任意长度的数据转换为固定长度的哈希值,通常为128位(16字节)。MD5 的主要特点是产生的哈希值具有较高的唯一性和不可逆性,通常用于验证数据的完整性、密码存储、数字签名等领域。

Java

java 复制代码
public class MD5Utils {
    public static String calculateMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] hashBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));

            // Convert the byte array to a hexadecimal string representation
            BigInteger number = new BigInteger(1, hashBytes);
            StringBuilder hexString = new StringBuilder(number.toString(16));
            while (hexString.length() < 32) {
                hexString.insert(0, "0");
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Kotlin实现

Kotlin 复制代码
object MD5Utils {
    
    fun calculateMD5(input: String): String {
        try {
        val md = MessageDigest.getInstance("MD5")
        val hashBytes = md.digest(input.toByteArray(StandardCharsets.UTF_8))
        val number = BigInteger(1, hashBytes)
        val hexString = StringBuilder(number.toString(16))
        while (hexString.length < 32) {
            hexString.insert(0, "0")
        }
        return hexString.toString()
        } catch (e: NoSuchAlgorithmException) {
        e.printStackTrace()
        }    
        return ""
    }
}
相关推荐
码哥DFS11 分钟前
构造函数、实例对象、对象原型 ----三者关系
开发语言·javascript·原型模式
9523637 分钟前
Spring-cloud配置中心
java·spring·spring cloud·微服务
quantdash_cc41 分钟前
告别自建 Requests/BS4 网页爬虫:基于 QuantDash 搭建零维保的高性能量化行情流水线
开发语言·爬虫·python·pandas·量化·quantdash
ydd1001001 小时前
寻找两个正序数组的中位数 Java 题解,二分分割详解
java·开发语言
半亩码田1 小时前
C#转Python第3.1篇:Python 的 class 没有访问修饰符?面向对象的另一条路
开发语言·python·c#
真上帝的左手2 小时前
10. 软件设计&架构-Spring Security 7 整合CAS SSO 单点登录
java·spring·架构·sso
Wzx1980122 小时前
python沙箱和docker沙箱你选对了吗?
开发语言·python·docker
牧羊人.3332 小时前
Python 办公自动化从入门到入土|09 数据容器之字典
开发语言·python
Zane19942 小时前
单例线程安全、生产者消费者、死锁:并发面试三连问串讲
java·后端
小僧景贤2 小时前
嵌入式C语言 第二篇:基础语法|嵌入式C与标准C的核心差异
c语言·开发语言·嵌入式c语言