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;
    }
相关推荐
木易 士心8 小时前
加密与编码算法全解:从原理到精通(Java & JS 实战版)
java·javascript·算法
专注于大数据技术栈8 小时前
java学习--ArrayList
java·学习
郝学胜-神的一滴8 小时前
深入理解Qt中的坐标系统:鼠标、窗口与控件位置详解
开发语言·c++·qt·程序人生
福楠8 小时前
C++ | 继承
c语言·开发语言·数据结构·c++·算法
编程大师哥8 小时前
JavaEE初阶的核心组件
java·java-ee
程序员zgh8 小时前
汽车以太网协议 —— DDS
c语言·开发语言·c++·网络协议·udp·汽车·信息与通信
华如锦8 小时前
MongoDB作为小型 AI智能化系统的数据库
java·前端·人工智能·算法
superman超哥8 小时前
Rust 异步编程的终极考验:Tokio 资源管理与清理
开发语言·rust·编程语言·rust异步编程·tokio资源管理与清理
前天的五花肉8 小时前
D3.js研发交互模型指标柱形图
开发语言·javascript·交互
q***44158 小时前
C++跨平台开发挑战的技术文章大纲编译器与工具链差异
java·后端