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;
    }
相关推荐
BBB努力学习程序设计几秒前
Java循环:让代码重复工作的"魔法"
java
小尧嵌入式4 分钟前
QT软件开发知识点流程及图片转换工具的开发
开发语言·arm开发·qt
网络点点滴5 分钟前
pinia简介
开发语言·javascript·vue.js
white-persist15 分钟前
【攻防世界】reverse | Mysterious 详细题解 WP
c语言·开发语言·网络·汇编·c++·python·安全
青云交33 分钟前
Java 大视界 -- Java 大数据在智能交通智能停车诱导与车位共享中的应用
java·智能停车·故障预测·极端气候适配·车位共享·民生应用·政企协同
從南走到北35 分钟前
JAVA代驾小程序源码代驾跑腿APP源码
java·开发语言·微信·微信小程序·小程序
lightqjx36 分钟前
【C++】对set和map的使用
开发语言·数据结构·c++·stl
快乐zbc44 分钟前
C++ 中 typedef 指针别名与 const 的坑
开发语言·c++
李贺梖梖1 小时前
day01 Java概述、IDEA安装、Java基础语法
java
urkay-1 小时前
Android getDrawingCache 过时废弃
android·java·开发语言·kotlin·iphone·androidx