Repository navigation
Expand file tree
/
Copy pathPubNubUtil.java
More file actions
119 lines (96 loc) · 3.38 KB
/
Copy pathPubNubUtil.java
File metadata and controls
119 lines (96 loc) · 3.38 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
package com.pubnub.api;
import com.pubnub.api.vendor.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/**
* Created by Frederick on 3/30/16.
*/
public final class PubNubUtil {
private PubNubUtil() {
}
public static String joinString(List<String> val, String delim) {
StringBuilder builder = new StringBuilder();
for (String l : val) {
builder.append(l);
builder.append(",");
}
return builder.toString().substring(0, builder.toString().length() - 1);
}
/**
* Returns encoded String
*
* @param sUrl , input string
* @return , encoded string
*/
public static String pamEncode(String sUrl) {
/* !'()*~ */
String encoded = urlEncode(sUrl);
if (encoded != null) {
encoded = encoded.
replace("*", "%2A")
.replace("!", "%21")
.replace("'", "%27")
.replace("(", "%28")
.replace(")", "%29")
.replace("[", "%5B")
.replace("]", "%5D")
.replace("~", "%7E");
}
return encoded;
}
/**
* Returns encoded String
*
* @param sUrl , input string
* @return , encoded string
*/
public static String urlEncode(String sUrl) {
try {
return URLEncoder.encode(sUrl, "UTF-8").replace("+", "%20");
} catch (UnsupportedEncodingException e) {
return null;
}
}
public static String preparePamArguments(Map<String, String> pamArgs) {
Set<String> pamKeys = new TreeSet(pamArgs.keySet());
String stringifiedArguments = "";
int i = 0;
for (String pamKey : pamKeys) {
if (i != 0) {
stringifiedArguments = stringifiedArguments.concat("&");
}
stringifiedArguments = stringifiedArguments.concat(pamKey).concat("=").concat(pamEncode(pamArgs.get(pamKey)));
i += 1;
}
return stringifiedArguments;
}
public static String signSHA256(final String key, final String data) throws PubNubException {
Mac sha256HMAC;
byte[] hmacData;
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "HmacSHA256");
try {
sha256HMAC = Mac.getInstance("HmacSHA256");
} catch (NoSuchAlgorithmException e) {
throw PubNubException.builder().pubnubError(PubNubError.PNERROBJ_CRYPTO_ERROR).errormsg(e.getMessage()).build();
}
try {
sha256HMAC.init(secretKey);
} catch (InvalidKeyException e) {
throw PubNubException.builder().pubnubError(PubNubError.PNERROBJ_CRYPTO_ERROR).errormsg(e.getMessage()).build();
}
try {
hmacData = sha256HMAC.doFinal(data.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw PubNubException.builder().pubnubError(PubNubError.PNERROBJ_CRYPTO_ERROR).errormsg(e.getMessage()).build();
}
return new String(Base64.encode(hmacData, 0)).replace('+', '-').replace('/', '_');
}
}