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;
    }
相关推荐
qq_537562672 分钟前
跨语言调用C++接口
开发语言·c++·算法
wjs202412 分钟前
DOM CDATA
开发语言
一点程序12 分钟前
基于SpringBoot的选课调查系统
java·spring boot·后端·选课调查系统
Tingjct14 分钟前
【初阶数据结构-二叉树】
c语言·开发语言·数据结构·算法
C雨后彩虹14 分钟前
计算疫情扩散时间
java·数据结构·算法·华为·面试
2601_9498095918 分钟前
flutter_for_openharmony家庭相册app实战+我的Tab实现
java·javascript·flutter
猷咪40 分钟前
C++基础
开发语言·c++
IT·小灰灰41 分钟前
30行PHP,利用硅基流动API,网页客服瞬间上线
开发语言·人工智能·aigc·php
快点好好学习吧43 分钟前
phpize 依赖 php-config 获取 PHP 信息的庖丁解牛
android·开发语言·php
秦老师Q44 分钟前
php入门教程(超详细,一篇就够了!!!)
开发语言·mysql·php·db