Java实现计算指定文件的SHA256

 SHA-256

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

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

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实现

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

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实现

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 ""
    }
}

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值