是什么问题导致的,想问下如何解决?
签名算法如下:
private static final String bucketName = "";
private static final long expired = 2592000;
/**
* 生成 Authorization 签名字段
*
* @param appId
* @param secretId
* @param secretKey
* @param bucketName
* @param expired
* @return
* @throws Exception
*/
public static String appSign(long appId, String secretId, String secretKey, String bucketName, long expired)
throws Exception {
System.out.println(expired);
long now = System.currentTimeMillis() / 1000;
int rdm = Math.abs(new Random().nextInt());
String plainText = String.format("a=%d&b=%s&k=%s&t=%d&e=%d&r=%d", appId, bucketName, secretId, now,
now + expired, rdm);
byte[] hmacDigest = HmacSha1(plainText, secretKey);
byte[] signContent = new byte[hmacDigest.length + plainText.getBytes().length];
System.arraycopy(hmacDigest, 0, signContent, 0, hmacDigest.length);
System.arraycopy(plainText.getBytes(), 0, signContent, hmacDigest.length, plainText.getBytes().length);
return Base64Encode(signContent);
}
/**
* 生成 base64 编码
*
* @param binaryData
* @return
*/
public static String Base64Encode(byte[] binaryData) {
String encodedstr = null;
try {
System.out.println(binaryData);
encodedstr = Base64.getEncoder().encodeToString(binaryData);
System.out.println(encodedstr);
} catch (Exception e) {
e.printStackTrace();
}
return encodedstr;
}
/**
* 生成 hmacsha1 签名
*
* @param binaryData
* @param key
* @return
* @throws Exception
*/
public static byte[] HmacSha1(byte[] binaryData, String key) throws Exception {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
mac.init(secretKey);
byte[] HmacSha1Digest = mac.doFinal(binaryData);
return HmacSha1Digest;
}
/**
* 生成 hmacsha1 签名
*
* @param plainText
* @param key
* @return
* @throws Exception
*/
public static byte[] HmacSha1(String plainText, String key) throws Exception {
return HmacSha1(plainText.getBytes(), key);
}
相似问题