Administrator
14 hours ago 635199d881d45a8c92abea57e89b69634ab4b366
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
package com.xcong.excoin.modules.newPrice.utils;
 
import lombok.extern.slf4j.Slf4j;
 
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
 
 
@Slf4j
public class SignUtils {
 
    public static String signRest(String secretKey, String timestamp, String method, String path, String body) {
        String str = String.format("%s%s%s%s",
                timestamp, // timestamp
                method,  // method GET/POST
                path, // requestPath
                body // body
        );
        try {
            return Base64.getEncoder().encodeToString(hmacSHA256(secretKey.getBytes(), str.getBytes()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
 
 
    /**
     * HmacSHA256算法,返回的结果始终是32位
     *
     * @param key     加密的键,可以是任何数据
     * @param content 待加密的内容
     * @return 加密后的内容
     * @throws Exception ex
     */
    public static byte[] hmacSHA256(byte[] key, byte[] content) throws Exception {
        Mac hmacSha256 = Mac.getInstance("HmacSHA256");
        hmacSha256.init(new SecretKeySpec(key, 0, key.length, "HmacSHA256"));
        return hmacSha256.doFinal(content);
    }
 
    public static String signWebsocket(String timestamp, String secretKey) {
        String str = String.format("%s%s%s",
                timestamp,
                "GET",
                "/users/self/verify");
        try {
            return Base64.getEncoder().encodeToString(hmacSHA256(secretKey.getBytes(), str.getBytes()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
}