Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Aug 31, 2020. It is now read-only.

Latest commit

 

History

History
History
230 lines (190 loc) · 6.36 KB

File metadata and controls

230 lines (190 loc) · 6.36 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.security.SecureRandom;
import java.net.HttpURLConnection;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.RandomStringUtils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.TrustManager;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.HttpsURLConnection;
public class SMSGlobalUtil {
/** Constants */
public static final String HASH_ALGORITHM = "HmacSHA256";
private Mac mac;
public SMSGlobalUtil(String secret) {
try {
mac = Mac.getInstance(HASH_ALGORITHM);
mac.init(new SecretKeySpec(secret.getBytes(), mac.getAlgorithm()));
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
/**
* This method constructs the complete URL.
*
* @param protocol the protocol
* @param host the host
* @param port the port
* @param version the version
* @param uri the uri
* @return the string
*/
public String constructURL(String protocol, String host, int port, String version, String uri) {
StringBuilder url = new StringBuilder();
url.append(protocol);
url.append("://");
url.append(host);
url.append(":");
url.append(port);
url.append(version);
url.append(uri);
return url.toString();
}
/**
* returns authorization header.
*
* @param apiKey the api key
* @param method the method
* @param requestUri the request uri
* @param host the host
* @param port the port
* @param extraData the extra data
* @return the authorization header
* @throws SMSGlobalRestClientException the rest client exception
*/
public String getAuthorizationHeader(String apiKey,
String method, String requestUri, String host, int port,
String extraData) throws SMSGlobalRestClientException {
StringBuilder header = new StringBuilder();
String nonce = "";
String randomString = "";
String hash = "";
String data = "";
//create a random string to be part of the HMAC
randomString = RandomStringUtils.random(15);
//get timestamp in seconds
long timestamp = System.currentTimeMillis() / 1000;
try {
nonce = getMD5Value(randomString);
data = getData(timestamp, nonce,method, requestUri, host, port, extraData);
hash = getHash(data);
header.append("MAC ");
header.append("id=\"" + apiKey);
header.append("\",ts=\"" + timestamp);
header.append("\",nonce=\"" + nonce);
header.append("\",mac=\"" + hash);
header.append("\"");
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new SMSGlobalRestClientException(e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new SMSGlobalRestClientException(e);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new SMSGlobalRestClientException(e);
}
return header.toString();
}
/**
* Method concatenate the string in the required format.
*
* @param timestamp the timestamp
* @param nonce the nonce
* @param method the method
* @param requestUri the request uri
* @param host the host
* @param port the port
* @param extraData the extra data
* @return the data
*/
private String getData(long timestamp, String nonce, String method,
String requestUri, String host, int port, String extraData) {
StringBuilder sb = new StringBuilder();
sb.append(timestamp);
sb.append("\n");
sb.append(nonce);
sb.append("\n");
sb.append(method);
sb.append("\n");
sb.append(requestUri);
sb.append("\n");
sb.append(host);
sb.append("\n");
sb.append(port);
sb.append("\n");
sb.append(extraData);
sb.append("\n");
return sb.toString();
}
/**
* Returns MD5 value.
*
* @param data the data
* @return the m d5 value
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws UnsupportedEncodingException the unsupported encoding exception
*/
private String getMD5Value(String data) throws NoSuchAlgorithmException,
UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(data.getBytes());
byte byteData[] = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16)
.substring(1));
}
return sb.toString();
}
/**
* returns Base64 encoded value of computed HMAC of data.
*
* @param data the data
* @return the hash
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws InvalidKeyException the invalid key exception
* @throws UnsupportedEncodingException the unsupported encoding exception
*/
private String getHash(String data)
throws NoSuchAlgorithmException, InvalidKeyException,
UnsupportedEncodingException {
return Base64.encodeBase64String(mac.doFinal(data.getBytes()));
}
/**
* While not recommended, you can also disable SSL cert validation alltogether:
*/
public static void disableCertificateValidation() {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {}
public void checkServerTrusted(X509Certificate[] certs, String authType) {}
}};
// Ignore differences between given hostname and certificate hostname
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) { return true; }
};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv);
} catch (Exception e) {}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.