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;
    }
相关推荐
lly2024064 分钟前
C语言中的循环结构:深入理解与高效应用
开发语言
异步的告白5 分钟前
链接脚本SECTIONS逐行深度解析
linux·开发语言
Aurorar0rua23 分钟前
CS50 x 2024 Notes C - 04
java·开发语言
iCxhust28 分钟前
C#程序,窗体1向窗体2的textbox控件写入字符串“hello”
开发语言·c#
椰羊~王小美32 分钟前
嵌入式 和 单片机
java·单片机·嵌入式硬件
低客的黑调34 分钟前
Redis-不止是缓存
java·开发语言·数据库
花间相见38 分钟前
【大模型微调与部署02】—— ms-swift 自定义数据集完全教程:格式、dataset_info 配置、多格式兼容实战
开发语言·ssh·swift
噢,我明白了41 分钟前
Java 入门,详解List,Map集合使用
java·list·map
Hello--_--World42 分钟前
JS:闭包、函数柯里化、工厂函数、偏函数、立即执行函数 相关知识点与面试题
开发语言·javascript·ecmascript
ZenosDoron1 小时前
函数形参传数组
java·jvm·算法