返回顶部
首页 > 资讯 > 后端开发 > JAVA >Java获取文件的hash值(SHA256)
  • 524
分享到

Java获取文件的hash值(SHA256)

哈希算法散列表算法 2023-09-12 07:09:47 524人浏览 八月长安
摘要

目录 简介 获取网络文件的sha256值(方式一) 获取本地文件的sha256值(方式二) 简介         在工作开发当中需求要通过文件的hash值比对文件是否被篡改过,于是通过使用了(sha256)hash值进行比对,因为对于

目录

简介

获取网络文件的sha256值(方式一)

获取本地文件的sha256值(方式二)


简介

        在工作开发当中需求要通过文件的hash值比对文件是否被篡改过,于是通过使用了(sha256)hash值进行比对,因为对于任意长度的消息,SHA256都会产生一个256bit长的哈希值,通常用一个长度为64的十六进制字符串来表示。

获取网络文件的sha256值(方式一)

        首先通过InputStream获取网络URL文件,然后创建临时文件,再通过FileInputStream以字节流的方式逐块读取文件内容,然后通过DigestInputStream将读取的数据传递给MessageDigest来计算SHA256哈希值。这样可以避免将整个文件加载到内存中,而是通过缓冲区逐块处理文件内容。

JAVA代码如下:(文件地址自己改一下)

public static byte[] calculateSHA256(String filePath) throws ioException, NoSuchAlGorithmException {MessageDigest digest = MessageDigest.getInstance("SHA-256");//获取网络URL文件InputStream fis2 = new URL(filePath).openStream();//创建临时文件File file = File.createTempFile(IdWorker.getIdStr(),"");FileUtil.writeFromStream(fis2,file);try (FileInputStream fis = new FileInputStream(file);FileChannel channel = fis.getChannel();DigestInputStream dis = new DigestInputStream(fis, digest)) {ByteBuffer buffer = ByteBuffer.allocate(8192); // 8 KB bufferwhile (channel.read(buffer) != -1) {buffer.flip();digest.update(buffer);buffer.clear();}return digest.digest();}}private static String bytesToHex(byte[] bytes) {StringBuilder result = new StringBuilder();for (byte b : bytes) {result.append(String.fORMat("%02x", b));}return result.toString();}public static void main(String[] args) {String filePath = "https://xxxxx/20230410/bfd71f584d9645b0a5e3D7a465119f0c.pdf";try {byte[] sha256 = calculateSHA256(filePath);String sha256Hex = bytesToHex(sha256);System.out.println("SHA256: " + sha256Hex);} catch (IOException | NoSuchAlgorithmException e) {e.printStackTrace();}}

响应结果:(这里的结果是我自己再测试的时候生成的)

SHA256: cffebd06c485d17b8a93308e1e39cc4c1636444b762c9c153ba8b29022392b98

获取本地文件的sha256值(方式二)

        通过FileInputStream以字节流的方式逐块读取文件内容,然后通过DigestInputStream将读取的数据传递给MessageDigest来计算SHA256哈希值。这样可以避免将整个文件加载到内存中,而是通过缓冲区逐块处理文件内容。

JAVA代码如下:(文件地址自己改一下)

public static byte[] calculateSHA256(String filePath) throws IOException, NoSuchAlgorithmException {MessageDigest digest = MessageDigest.getInstance("SHA-256");try (FileInputStream fis = new FileInputStream(filePath);FileChannel channel = fis.getChannel();DigestInputStream dis = new DigestInputStream(fis, digest)) {ByteBuffer buffer = ByteBuffer.allocate(8192); // 8 KB bufferwhile (channel.read(buffer) != -1) {buffer.flip();digest.update(buffer);buffer.clear();}return digest.digest();}}private static String bytesToHex(byte[] bytes) {StringBuilder result = new StringBuilder();for (byte b : bytes) {result.append(String.format("%02x", b));}return result.toString();}public static void main(String[] args) {String filePath = "D:\\bfd71f584d9645b0a5e3d7a465119f0c.pdf";try {byte[] sha256 = calculateSHA256(filePath);String sha256Hex = bytesToHex(sha256);System.out.println("SHA256: " + sha256Hex);} catch (IOException | NoSuchAlgorithmException e) {e.printStackTrace();}}

响应结果:(这里的结果是我自己再测试的时候生成的)

SHA256: cffebd06c485d17b8a93308e1e39cc4c1636444b762c9c153ba8b29022392b98

作者:筱白爱学习!!

欢迎关注转发评论点赞沟通,您的支持是筱白的动力!

来源地址:https://blog.csdn.net/weixin_43552143/article/details/131397194

--结束END--

本文标题: Java获取文件的hash值(SHA256)

本文链接: https://www.lsjlt.com/news/404120.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作