|
@@ -3,8 +3,6 @@ package com.qmth.boot.core.fss.utils;
|
|
|
import com.qmth.boot.tools.codec.CodecUtils;
|
|
|
import com.qmth.boot.tools.models.ByteArray;
|
|
|
|
|
|
-import javax.crypto.Mac;
|
|
|
-import javax.crypto.spec.SecretKeySpec;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.time.Duration;
|
|
@@ -37,13 +35,15 @@ public class OssSigner {
|
|
|
//System.out.println("stringToSign:" + stringToSign);
|
|
|
// 步骤3:计算Signature。
|
|
|
// "accesskeysecret"填入用户AK,data参数填入实际日期如"20231203”
|
|
|
- byte[] dateKey = hmacsha256(("aliyun_v4" + config.getAccessSecret()).getBytes(StandardCharsets.UTF_8),
|
|
|
- dateString);
|
|
|
+ byte[] dateKey = CodecUtils
|
|
|
+ .hmacsha256(("aliyun_v4" + config.getAccessSecret()).getBytes(StandardCharsets.UTF_8),
|
|
|
+ dateString.getBytes(StandardCharsets.UTF_8));
|
|
|
// 参数填入所在地区,如所在地域为杭州则填入"cn-hangzhou”
|
|
|
- byte[] dateRegionKey = hmacsha256(dateKey, config.getRegion());
|
|
|
- byte[] dateRegionServiceKey = hmacsha256(dateRegionKey, "oss");
|
|
|
- byte[] signingKey = hmacsha256(dateRegionServiceKey, "aliyun_v4_request");
|
|
|
- byte[] result = hmacsha256(signingKey, stringToSign);
|
|
|
+ byte[] dateRegionKey = CodecUtils.hmacsha256(dateKey, config.getRegion().getBytes(StandardCharsets.UTF_8));
|
|
|
+ byte[] dateRegionServiceKey = CodecUtils.hmacsha256(dateRegionKey, "oss".getBytes(StandardCharsets.UTF_8));
|
|
|
+ byte[] signingKey = CodecUtils
|
|
|
+ .hmacsha256(dateRegionServiceKey, "aliyun_v4_request".getBytes(StandardCharsets.UTF_8));
|
|
|
+ byte[] result = CodecUtils.hmacsha256(signingKey, stringToSign.getBytes(StandardCharsets.UTF_8));
|
|
|
String signature = CodecUtils.toHexString(result).toLowerCase();
|
|
|
//System.out.println("signature:" + signature);
|
|
|
|
|
@@ -73,13 +73,14 @@ public class OssSigner {
|
|
|
+ "/oss/aliyun_v4_request\n" + canonicalDigest;
|
|
|
|
|
|
// 步骤3:计算Signature。
|
|
|
- byte[] dateKey = hmacsha256(("aliyun_v4" + config.getAccessSecret()).getBytes(StandardCharsets.UTF_8),
|
|
|
- dateString);
|
|
|
- byte[] dateRegionKey = hmacsha256(dateKey, config.getRegion());
|
|
|
- byte[] dateRegionServiceKey = hmacsha256(dateRegionKey, "oss");
|
|
|
- byte[] signingKey = hmacsha256(dateRegionServiceKey, "aliyun_v4_request");
|
|
|
-
|
|
|
- byte[] result = hmacsha256(signingKey, stringToSign);
|
|
|
+ byte[] dateKey = CodecUtils
|
|
|
+ .hmacsha256(("aliyun_v4" + config.getAccessSecret()).getBytes(StandardCharsets.UTF_8),
|
|
|
+ dateString.getBytes(StandardCharsets.UTF_8));
|
|
|
+ byte[] dateRegionKey = CodecUtils.hmacsha256(dateKey, config.getRegion().getBytes(StandardCharsets.UTF_8));
|
|
|
+ byte[] dateRegionServiceKey = CodecUtils.hmacsha256(dateRegionKey, "oss".getBytes(StandardCharsets.UTF_8));
|
|
|
+ byte[] signingKey = CodecUtils
|
|
|
+ .hmacsha256(dateRegionServiceKey, "aliyun_v4_request".getBytes(StandardCharsets.UTF_8));
|
|
|
+ byte[] result = CodecUtils.hmacsha256(signingKey, stringToSign.getBytes(StandardCharsets.UTF_8));
|
|
|
String signature = CodecUtils.toHexString(result).toLowerCase();
|
|
|
//System.out.println("signature:" + signature);
|
|
|
|
|
@@ -89,22 +90,8 @@ public class OssSigner {
|
|
|
+ "%2F" + config.getRegion() + "%2Foss%2Faliyun_v4_request&" + "x-oss-date=" + dateTime + "&"
|
|
|
+ "x-oss-expires=" + expire + "&" + "x-oss-signature=" + signature + "&"
|
|
|
+ "x-oss-signature-version=OSS4-HMAC-SHA256";
|
|
|
- return path + "?" + queryString;
|
|
|
- }
|
|
|
-
|
|
|
- private static byte[] hmacsha256(byte[] key, String data) {
|
|
|
- try {
|
|
|
- // 初始化HMAC密钥规格,指定算法为HMAC-SHA256并使用提供的密钥。
|
|
|
- SecretKeySpec secretKeySpec = new SecretKeySpec(key, "HmacSHA256");
|
|
|
- // 获取Mac实例,并通过getInstance方法指定使用HMAC-SHA256算法。
|
|
|
- Mac mac = Mac.getInstance("HmacSHA256");
|
|
|
- // 使用密钥初始化Mac对象。
|
|
|
- mac.init(secretKeySpec);
|
|
|
- // 执行HMAC计算,通过doFinal方法接收需要计算的数据并返回计算结果的数组。
|
|
|
- return mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException("Failed to calculate HMAC-SHA256", e);
|
|
|
- }
|
|
|
+ //System.out.println("queryString:" + queryString);
|
|
|
+ return queryString;
|
|
|
}
|
|
|
|
|
|
public static String getIso8601Date(Date date) {
|