AuthUtils.java
4.96 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.cnlive.demo.rdupload;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.SignatureException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import com.ksyun.ks3.model.acl.Authorization;
import com.ksyun.ks3.services.request.Ks3HttpRequest;
import com.ksyun.ks3.util.ByteUtil;
import android.text.TextUtils;
import android.util.Base64;
/**
*
* 签名工具类
*
*/
public class AuthUtils {
public static String calcAuthorization(Authorization auth,
Ks3HttpRequest request) throws SignatureException {
String signature = calcSignature(auth.getAccessKeySecret(), request);
String value = "KSS " + auth.getAccessKeyId() + ":" + signature;
return value;
}
private static String calcSignature(String accessKeySecret,
Ks3HttpRequest request) throws SignatureException {
String resource = CanonicalizedKSSResource(request);
String requestMethod = request.getHttpMethod().toString();
String contentMd5 = request.getContentMD5();
String contentType = request.getContentType();
String _signDate = request.getDate();
List<String> signList = new ArrayList<String>();
signList.addAll(Arrays.asList(new String[] { requestMethod, contentMd5,
contentType, _signDate }));
String _headers = CanonicalizedKSSHeaders(request);
if (_headers != null && !_headers.equals("")) {
signList.add(_headers);
}
signList.add(resource);
String signStr = TextUtils.join("\n", signList.toArray());
String serverSignature = calculateRFC2104HMAC(signStr, accessKeySecret);
return serverSignature;
}
// AuthListener方式,签名计算方法
public static String calcAuthToken(String httpMethod, String contentType,
String date, String contentMD5, String resource, String Headers,
String accessKeyId, String accessKeySecret)
throws SignatureException {
List<String> signList = new ArrayList<String>();
signList.addAll(Arrays.asList(new String[] { httpMethod, contentMD5,
contentType, date }));
String _headers = Headers;
if (_headers != null && !_headers.equals("")) {
signList.add(_headers);
}
signList.add(resource);
String signStr = TextUtils.join("\n", signList.toArray());
String serverSignature = calculateRFC2104HMAC(signStr, accessKeySecret);
String value = "KSS " + accessKeyId + ":" + serverSignature;
return value;
}
public static String CanonicalizedKSSResource(Ks3HttpRequest request) {
boolean escapeDoubleSlash = true;
String bucketName = request.getBucketname();
String objectKey = request.getObjectkey();
StringBuffer buffer = new StringBuffer();
buffer.append("/");
if (!TextUtils.isEmpty(bucketName)) {
buffer.append(bucketName).append("/");
}
if (!TextUtils.isEmpty(objectKey)) {
String encodedPath = null;
try {
encodedPath = URLEncoder.encode(objectKey, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (escapeDoubleSlash) {
encodedPath = encodedPath.replace("//", "/%2F");
}
buffer.append(encodedPath);
}
String resource = buffer.toString();
String queryParams = request.getParamsToSign();
if (queryParams != null && !queryParams.equals(""))
resource = resource + "?" + queryParams;
return resource;
}
public static String CanonicalizedKSSHeaders(Ks3HttpRequest request) {
String prefix = "x-kss";
Map<String, String> headers = request.getHeader();
List<String> headList = new ArrayList<String>();
for (String _header : headers.keySet()) {
if (_header.toLowerCase().startsWith(prefix)) {
headList.add(_header);
}
}
Collections.sort(headList, new Comparator<String>() {
public int compare(String o1, String o2) {
return ByteUtil.compareTo(o1.getBytes(), o2.toString()
.getBytes());
}
});
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < headList.size(); i++) {
String _key = headList.get(i);
buffer.append(headList.get(i) + ":" + headers.get(_key));
if (i < (headList.size() - 1))
buffer.append("\n");
}
return buffer.toString();
}
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
public static String calculateRFC2104HMAC(String data, String key)
throws SignatureException {
String result;
try {
// get an hmac_sha1 key from the raw key bytes
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(),
HMAC_SHA1_ALGORITHM);
// get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
// compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(data.getBytes());
// base64-encode the hmac
// result = new String(Base64.encodeBase64(rawHmac), "GBK");
result = Base64.encodeToString(rawHmac, Base64.DEFAULT);
} catch (Exception e) {
throw new SignatureException("Failed to generate HMAC : " + e);
}
return result;
}
}