SignatureGenerateUtil.java
5.35 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
package com.genersoft.iot.vmp.vmanager.util;
import com.genersoft.iot.vmp.conf.exception.ServiceException;
import com.genersoft.iot.vmp.vmanager.jt1078.platform.domain.ApiParamReq;
import org.apache.commons.lang3.StringUtils;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* 签名生成工具类
*
* @Author WangXin
* @Data 2025/2/11
* @Version 1.0.0
*/
public class SignatureGenerateUtil {
/**
* 临港调度接口签名生成工具
* @param map 请求入参
* Map<String, String> map = new HashMap<String, String>();
* map.put("timestamp", "1490769820000");//时间戳
* map.put("nonce", "a1503");//随机字符串
* map.put("password", "a097286f0aeab6baf093bfea9afa2c29f6751212");//密码
* @return 签名
*/
public static String getSHA1(Map<String, String> map) throws Exception {
try {
String[] array = new String[map.size()];
map.values().toArray(array);
StringBuilder sb = new StringBuilder();
// 字符串排序
Arrays.sort(array);
for (String s : array) {
sb.append(s);
}
String str = sb.toString();
// SHA1签名生成
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(str.getBytes());
byte[] digest = md.digest();
StringBuilder extra = new StringBuilder();
String shaHex = "";
for (byte b : digest) {
shaHex = Integer.toHexString(b & 0xFF);
if (shaHex.length() < 2) {
extra.append(0);
}
extra.append(shaHex);
}
return extra.toString();
} catch (Exception e) {
throw new ServiceException(e.getMessage());
}
}
public static String getSHA1(String timestamp, String nonce, String password) throws Exception {
HashMap<String, String> map = new HashMap<>();
map.put("timestamp", timestamp);//时间戳
map.put("nonce", nonce);//随机字符串
map.put("password", password);
return getSHA1(map);
}
/**
* 获取SHA1加密字符串
* @param timestamp 时间戳
* @param nonce 随机字符串
* @param password 密码
* @param username 用户名
* @return SHA1加密后的字符串
* @throws Exception 加密过程中可能抛出的异常
*/
public static String getSHA1(String timestamp, String nonce, String password, String username) throws Exception {
HashMap<String, String> map = new HashMap<>();
map.put("timestamp", timestamp);//时间戳
map.put("nonce", nonce);//随机字符串
map.put("password", password);
map.put("username", username);
return getSHA1(map);
}
public static ApiParamReq getApiParamReq(){
String timestamp = String.valueOf(System.currentTimeMillis());
String nonce = "a1503";
// String password = getBean(LgDvrPropertiesConfig.class).getLgApiPassword();
String password = "f8267b7bc5e51994bab57c8e8884f203609d1dc3";
// String password = "bafb2b44a07a02e5e9912f42cd197423884116a8";
// String password = "9dddf2a4f7d94594ec2ea98407a410e1";
try {
return ApiParamReq.builder()
.timestamp(timestamp)
.nonce(nonce)
.password(password)
.sign(getSHA1(timestamp, nonce, password))
.build();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static ApiParamReq getApiParamReq(String password){
String timestamp = String.valueOf(System.currentTimeMillis());
String nonce = "a1503";
String username = "";
// String password = getBean(LgDvrPropertiesConfig.class).getLgApiPassword();
// String password = "bafb2b44a07a02e5e9912f42cd197423884116a8";
// String password = "9dddf2a4f7d94594ec2ea98407a410e1";
try {
return ApiParamReq.builder()
.timestamp(timestamp)
.nonce(nonce)
.password(password)
.sign(StringUtils.isNotBlank(username)?getSHA1(timestamp, nonce, password, username):getSHA1(timestamp, nonce, password))
.build();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws Exception {
String password = "f8267b7bc5e51994bab57c8e8884f203609d1dc3";
ApiParamReq apiParamReq = getApiParamReq(password);//密码
System.out.println(apiParamReq.getSign());
System.out.println(apiParamReq.getTimestamp());
//
System.out.println(StringUtils.join("http://61.169.120.202:40007/getInfo?password=",apiParamReq.getPassword(),"&nonce=",apiParamReq.getNonce(),"&sign=",apiParamReq.getSign(),"×tamp=",apiParamReq.getTimestamp(),"&username=","wangxin","&deviceId=","00000000"));
}
public static String createUrl(String url, String password){
ApiParamReq apiParamReq = getApiParamReq(password);
return StringUtils.join(url,"?password=",apiParamReq.getPassword(),"&nonce=",apiParamReq.getNonce(),"&sign=",apiParamReq.getSign(),"×tamp=",apiParamReq.getTimestamp());
}
}