/
QcloudSign.java
116 lines (92 loc) · 3.43 KB
/
QcloudSign.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*=============================================================================
#
# FileName: QcloudSign.java
# Desc: Qcloud 签名调试工具
#
# Author: gavinyao
# Email: gavinyao@tencent.com
#
# Created: 2014-12-19 11:30:04
# Version: 0.0.1
# History:
# 0.0.1 | gavinyao | 2014-12-19 11:30:04 | initialization
#
=============================================================================*/
import javax.crypto.Mac;
import java.util.TreeMap;
import java.io.IOException;
import sun.misc.BASE64Encoder;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.spec.SecretKeySpec;
/**
* @brief 腾讯云API 签名工具
* @author gavinyao@tencent.com
* @date 2014-08-13 20:48:11
*/
public class QcloudSign {
// 编码方式
private static final String CONTENT_CHARSET = "UTF-8";
// HMAC算法
private static final String HMAC_ALGORITHM = "HmacSHA1";
/**
* @brief 签名
* @author gavinyao@tencent.com
* @date 2014-08-13 21:07:27
*
* @param signStr 被加密串
* @param secret 加密密钥
*
* @return
*/
public static String sign(String signStr, String secret) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
String sig = null;
Mac mac = Mac.getInstance(HMAC_ALGORITHM);
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(CONTENT_CHARSET), mac.getAlgorithm());
mac.init(secretKey);
byte[] hash = mac.doFinal(signStr.getBytes(CONTENT_CHARSET));
// base64
sig = new String(new BASE64Encoder().encode(hash).getBytes());
return sig;
}
public static String makeSignPlainText(TreeMap<String, Object> requestParams) {
return makeSignPlainText(requestParams, "GET", "cvm.api.qcloud.com", "/v2/index.php");
}
public static String makeSignPlainText(TreeMap<String, Object> requestParams,
String requestMethod) {
return makeSignPlainText(requestParams, requestMethod, "cvm.api.qcloud.com", "/v2/index.php");
}
public static String makeSignPlainText(TreeMap<String, Object> requestParams,
String requestMethod, String requestHost) {
return makeSignPlainText(requestParams, requestMethod, requestHost, "/v2/index.php");
}
public static String makeSignPlainText(TreeMap<String, Object> requestParams,
String requestMethod, String requestHost, String requestPath) {
String retStr = "";
retStr += requestMethod;
retStr += requestHost;
retStr += requestPath;
retStr += _buildParamStr(requestParams);
return retStr;
}
protected static String _buildParamStr(TreeMap<String, Object> requestParams) {
return _buildParamStr(requestParams, "GET");
}
protected static String _buildParamStr(TreeMap<String, Object> requestParams, String requestMethod) {
String retStr = "";
for(String key: requestParams.keySet()) {
if (key.equals("Signature")) {
continue;
}
if (retStr.isEmpty()) {
retStr += '?';
} else {
retStr += '&';
}
retStr += key.replace("_", ".") + '=' + requestParams.get(key).toString();
}
return retStr;
}
}