This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
java1.8.java
75 lines (59 loc) · 2.65 KB
/
java1.8.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
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
class Application {
private static final String ENCODING = "UTF-8";
public static void main(String[] args) throws java.lang.Exception {
String url = "https://example.com/?vk_user_id=494075&vk_app_id=6736218&vk_is_app_user=1&vk_are_notifications_enabled=1&vk_language=ru&vk_access_token_settings=&vk_platform=android&sign=htQFduJpLxz7ribXRZpDFUH-XEUhC9rBPTJkjUFEkRA";
String clientSecret = "wvl68m4dR1UpLrVRli";
Map<String, String> queryParams = getQueryParams(new URL(url));
String checkString = queryParams.entrySet().stream()
.filter(entry -> entry.getKey().startsWith("vk_"))
.sorted(Map.Entry.comparingByKey())
.map(entry -> encode(entry.getKey()) + "=" + encode(entry.getValue()))
.collect(Collectors.joining("&"));
String sign = getHashCode(checkString, clientSecret);
System.out.println(sign.equals(queryParams.getOrDefault("sign", "")) ? "ok" : "fail");
}
private static Map<String, String> getQueryParams(URL url) {
final Map<String, String> result = new LinkedHashMap<>();
final String[] pairs = url.getQuery().split("&");
for (String pair : pairs) {
int idx = pair.indexOf("=");
String key = idx > 0 ? decode(pair.substring(0, idx)) : pair;
String value = idx > 0 && pair.length() > idx + 1 ? decode(pair.substring(idx + 1)) : null;
result.put(key, value);
}
return result;
}
private static String getHashCode(String data, String key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(ENCODING), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(secretKey);
byte[] hmacData = mac.doFinal(data.getBytes(ENCODING));
return new String(Base64.getUrlEncoder().withoutPadding().encode(hmacData));
}
private static String decode(String value) {
try {
return URLDecoder.decode(value, ENCODING);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
}
private static String encode(String value) {
try {
return URLEncoder.encode(value, ENCODING);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
}
}