Skip to content

Commit 31659e0

Browse files
committed
字段 r 为必填;字段 f 虽为可选,但生成签名时也必须包含这个字段
1 parent 106c5f8 commit 31659e0

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

src/main/java/com/qcloud/image/sign/Sign.java

+46-30
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,51 @@
1111
*/
1212
public class Sign {
1313

14-
/**
15-
* 返回图片识别的签名
16-
*
17-
* @param cred
18-
* 包含用户秘钥信息
19-
* @param bucketName
20-
* bucket名
21-
* @param expired
22-
* 超时时间
23-
* @return 返回base64编码的字符串
24-
* @throws AbstractImageException 异常
25-
*/
26-
public static String appSign(Credentials cred, String bucketName, long expired) throws AbstractImageException {
27-
int appId = cred.getAppId();
28-
String secretId = cred.getSecretId();
29-
String secretKey = cred.getSecretKey();
30-
long now = System.currentTimeMillis() / 1000;
31-
int rdm = Math.abs(new Random().nextInt());
32-
String plainText = String.format("a=%d&b=%s&k=%s&t=%d&e=%d", appId, bucketName, secretId, now, now+expired);
33-
byte[] hmacDigest;
34-
try {
35-
hmacDigest = CommonCodecUtils.HmacSha1(plainText, secretKey);
36-
} catch (Exception e) {
37-
throw new UnknownException(e.getMessage());
38-
}
39-
byte[] signContent = new byte[hmacDigest.length + plainText.getBytes().length];
40-
System.arraycopy(hmacDigest, 0, signContent, 0, hmacDigest.length);
41-
System.arraycopy(plainText.getBytes(), 0, signContent, hmacDigest.length, plainText.getBytes().length);
42-
43-
return CommonCodecUtils.Base64Encode(signContent);
14+
/**
15+
* 返回万象图片的多次有效签名
16+
*
17+
* @param cred 包含用户秘钥信息
18+
* @param bucketName bucket名
19+
* @param expired 超时时间
20+
* @return 返回base64编码的字符串
21+
* @throws AbstractImageException 异常
22+
*/
23+
public static String appSign(Credentials cred, String bucketName, long expired) throws AbstractImageException {
24+
return appSign(cred, bucketName, "", expired);
25+
}
26+
27+
/**
28+
* 返回万象图片特定资源的多次有效签名
29+
*
30+
* @param cred 包含用户秘钥信息
31+
* @param bucketName bucket名
32+
* @param fileid 特定资源(会验证与当前操作的文件路径是否一致)
33+
* @param expired 超时时间
34+
* @return 返回base64编码的字符串
35+
* @throws AbstractImageException 异常
36+
*/
37+
public static String appSign(Credentials cred, String bucketName, String fileid, long expired) throws AbstractImageException {
38+
long now = System.currentTimeMillis() / 1000;
39+
return generateSignature(cred, bucketName, fileid, now, now + expired);
40+
}
41+
42+
private static String generateSignature(Credentials cred, String bucketName, String fileid, long currentTime, long expiredTime) throws AbstractImageException {
43+
int appId = cred.getAppId();
44+
String secretId = cred.getSecretId();
45+
String secretKey = cred.getSecretKey();
46+
int rdm = Math.abs(new Random().nextInt());
47+
String plainText = String.format("a=%d&b=%s&k=%s&t=%d&e=%d&r=%d&f=%s", appId, bucketName, secretId, currentTime, expiredTime, rdm, fileid);
48+
49+
byte[] hmacDigest;
50+
try {
51+
hmacDigest = CommonCodecUtils.HmacSha1(plainText, secretKey);
52+
} catch (Exception e) {
53+
throw new UnknownException(e.getMessage());
4454
}
55+
byte[] signContent = new byte[hmacDigest.length + plainText.getBytes().length];
56+
System.arraycopy(hmacDigest, 0, signContent, 0, hmacDigest.length);
57+
System.arraycopy(plainText.getBytes(), 0, signContent, hmacDigest.length, plainText.getBytes().length);
58+
59+
return CommonCodecUtils.Base64Encode(signContent);
60+
}
4561
}

0 commit comments

Comments
 (0)