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;
    }
相关推荐
S***q1925 分钟前
Rust在系统工具中的内存安全给代码上了三道保险锁。但正是这种“编译期的严苛”,换来了运行时的安心。比如这段代码:
开发语言·后端·rust
M***Z2108 分钟前
SQL 建表语句详解
java·数据库·sql
v***7949 分钟前
Spring Boot 热部署
java·spring boot·后端
执笔论英雄9 分钟前
【RL】python协程
java·网络·人工智能·python·设计模式
打点计时器12 分钟前
matlab 解决wfdb工具使用本地数据集报错
开发语言·matlab
zmzb010317 分钟前
C++课后习题训练记录Day38
开发语言·c++
galaxyffang20 分钟前
认证、会话管理、授权的区别
java
未名编程24 分钟前
Windows 下如何部署 Nacos 并导入配置文件
java·windows
夏霞29 分钟前
c# 使用vs code 创建.net8.0以及.net6.0 webApi项目的教程
开发语言·c#·.net
boonya29 分钟前
Java中Plugin设计模式的规范应用
java·spring·设计模式·插件模式