Java 远程url文件sha256加密

Java 远程url文件sha256加密

java 复制代码
public static String getSHA256(String filePath) throws Exception {
        InputStream fis = null;
        URL url = new URL(filePath);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] buffer = new byte[1024];

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            fis = connection.getInputStream();
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                digest.update(buffer, 0, bytesRead);
            }
            fis.close();
        } else {
            throw new IOException("HTTP request failed with response code: " + responseCode);
        }

        byte[] sha256Bytes = digest.digest();

        StringBuilder sb = new StringBuilder();
        for (byte b : sha256Bytes) {
            sb.append(String.format("%02x", b));
        }

        String sha256 = sb.toString();
        return sha256;
    }
相关推荐
栗子叶7 小时前
Java对象创建的过程
java·开发语言·jvm
Amumu121388 小时前
React面向组件编程
开发语言·前端·javascript
有一个好名字8 小时前
力扣-从字符串中移除星号
java·算法·leetcode
IT=>小脑虎8 小时前
Python零基础衔接进阶知识点【详解版】
开发语言·人工智能·python
wjs20248 小时前
C 标准库 - `<float.h>》详解
开发语言
zfj3218 小时前
CyclicBarrier、CountDownLatch、Semaphore 各自的作用和用法区别
java·开发语言·countdownlatch·semaphore·cyclicbarrier
2501_916766548 小时前
【JVM】类的加载机制
java·jvm
Sag_ever8 小时前
Java数组详解
java
张np8 小时前
java基础-ConcurrentHashMap
java·开发语言
早日退休!!!8 小时前
进程与线程的上下文加载_保存及内存映射
开发语言