TuyaUtils.java
1.85 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
package cn.com.glsx.modules.utils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
/**
* 获取涂鸦签名
*
* @author zhouhaibao
* @date 2021/3/3 14:23
*/
public class TuyaUtils {
/**
* 获取令牌签名
*
* @param time 时间戳
* @param client_id client_id
* @param secret secret
* @return
*/
public static String getLoginSign(long time, String client_id, String secret) {
String hash=null;
try {
String message = client_id + time;
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
hash = new HexBinaryAdapter().marshal(bytes).toUpperCase();
} catch (Exception e) {
e.printStackTrace();
}
return hash;
}
/**
* 获取业务的签名
*
* @param time 时间戳
* @param client_id client_id
* @param secret secret
* @param accessToken 令牌
* @return
*/
public static String getBussinessSign(long time, String client_id, String secret, String accessToken) {
String hash=null;
try {
String message = client_id + accessToken + time;
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
hash = new HexBinaryAdapter().marshal(bytes).toUpperCase();
} catch (Exception e) {
e.printStackTrace();
}
return hash;
}
}