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;
    }
相关推荐
꧁Q༒ོγ꧂19 分钟前
C++ 入门完全指南(四)--函数与模块化编程
开发语言·c++
花哥码天下35 分钟前
apifox登录后设置token到环境变量
java·后端
listhi52037 分钟前
对LeNet-5的matlab实现,识别MINST手写数字集
开发语言·matlab
qq_4335545441 分钟前
C++ manacher(求解回文串问题)
开发语言·c++·算法
csbysj20201 小时前
Chart.js 饼图:全面解析与实例教程
开发语言
浩瀚地学1 小时前
【Java】常用API(二)
java·开发语言·经验分享·笔记·学习
程序员小寒1 小时前
从一道前端面试题,谈 JS 对象存储特点和运算符执行顺序
开发语言·前端·javascript·面试
七夜zippoe1 小时前
事件驱动架构:构建高并发松耦合系统的Python实战
开发语言·python·架构·eda·事件驱动
古城小栈1 小时前
Rust Trait 敲黑板
开发语言·rust