volcengine / volc-openapi-demos

Demos for Volcengine's OpenAPIs
Apache License 2.0
7 stars 7 forks source link

HexFormat.of().formatHex java8 怎么玩? #1

Open dengyongfu opened 10 months ago

dengyongfu commented 10 months ago

*HexFormat.of().formatHex 能不能考虑下现在还在用java8 的,现在有多少公司是直接用java17 ?

chaofanx commented 5 months ago

+1

bwcx-jzy commented 2 weeks ago

hutool 版(jdk8)

package i18n;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.net.URLEncodeUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONUtil;

import java.util.*;

/**
 * @author bwcx_jzy
 * @since 2024/6/11
 */
public class VolcApi {

    private final String region;
    private final String service;
    private final String schema;
    private final String host;
    private final String path;
    private final String ak;
    private final String sk;

    public VolcApi(String region, String service, String schema, String host, String path, String ak, String sk) {
        this.region = region;
        this.service = service;
        this.host = host;
        this.schema = schema;
        this.path = path;
        this.ak = ak;
        this.sk = sk;
    }

    public static void main(String[] args) throws Exception {
        String SecretAccessKey = "xxxx";
        String AccessKeyID = "xxxx;
        // 请求地址
        String endpoint = "translate.volcengineapi.com";
        // 路径,不包含 Query// 请求接口信息
        String path = "/";
        String service = "translate";
        String region = "cn-north-1";
        String schema = "https";
        VolcApi sign = new VolcApi(region, service, schema, endpoint, path, AccessKeyID, SecretAccessKey);

        String action = "TranslateText";
        String version = "2020-06-01";

        HashMap<String, String> queryMap = new HashMap<>(0);

        HashMap<String, Object> query2Map = new HashMap<>(3);
        query2Map.put("SourceLanguage", "zh");
        query2Map.put("TargetLanguage", "en");
        query2Map.put("TextList", new String[]{"你好", "我是一个机器人"});

        String jsonStr = JSONUtil.toJsonStr(query2Map);
        String request = sign.doRequest("POST", queryMap, jsonStr.getBytes(), action, version);
        System.out.println(request);
    }

    public String doRequest(String method, Map<String, String> queryList, byte[] body, String action, String version) throws Exception {
        if (body == null) {
            body = new byte[0];
        }
        String xContentSha256 = SecureUtil.sha256().digestHex(body);

        DateTime dateTime = DateTime.now().setTimeZone(TimeZone.getTimeZone("GMT"));
        String xDate = dateTime.toString("yyyyMMdd'T'HHmmss'Z'");

        String shortXDate = dateTime.toString(DatePattern.PURE_DATE_FORMAT);

        String contentType = "application/json";

        String signHeader = "host;x-date;x-content-sha256;content-type";

        SortedMap<String, String> realQueryList = new TreeMap<>(queryList);
        realQueryList.put("Action", action);
        realQueryList.put("Version", version);
        StringBuilder querySB = new StringBuilder();
        for (String key : realQueryList.keySet()) {
            querySB.append(signStringEncoder(key)).append("=").append(signStringEncoder(realQueryList.get(key))).append("&");
        }
        querySB.deleteCharAt(querySB.length() - 1);

        String canonicalStringBuilder = method + "\n" + path + "\n" + querySB + "\n" +
            "host:" + host + "\n" +
            "x-date:" + xDate + "\n" +
            "x-content-sha256:" + xContentSha256 + "\n" +
            "content-type:" + contentType + "\n" +
            "\n" +
            signHeader + "\n" +
            xContentSha256;

        System.out.println(canonicalStringBuilder);

        String hashcanonicalString = SecureUtil.sha256().digestHex(canonicalStringBuilder.getBytes());
        String credentialScope = shortXDate + "/" + region + "/" + service + "/request";
        String signString = "HMAC-SHA256" + "\n" + xDate + "\n" + credentialScope + "\n" + hashcanonicalString;

        byte[] signKey = genSigningSecretKeyV4(sk, shortXDate, region, service);
        String signature = HexUtil.encodeHexStr(hmacSHA256(signKey, signString));

        Method method1 = Method.valueOf(method);
        HttpRequest request = HttpUtil.createRequest(method1, schema + "://" + host + path + "?" + querySB);

        request.header("Host", host);
        request.header("X-Date", xDate);
        request.header("X-Content-Sha256", xContentSha256);
        request.header("Content-Type", contentType);
        request.header("Authorization", "HMAC-SHA256" +
            " Credential=" + ak + "/" + credentialScope +
            ", SignedHeaders=" + signHeader +
            ", Signature=" + signature);
        if (!Objects.equals(method, "GET")) {
            request.body(body);
        }
        return request.thenFunction(HttpResponse::body);
    }

    private String signStringEncoder(String source) {
        return URLEncodeUtil.encodeQuery(source);
    }

    public static byte[] hmacSHA256(byte[] key, String content) throws Exception {
        return SecureUtil.hmacSha256(key).digest(content);
    }

    private byte[] genSigningSecretKeyV4(String secretKey, String date, String region, String service) throws Exception {
        byte[] kDate = hmacSHA256((secretKey).getBytes(), date);
        byte[] kRegion = hmacSHA256(kDate, region);
        byte[] kService = hmacSHA256(kRegion, service);
        return hmacSHA256(kService, "request");
    }
}