刘光辉
14 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
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
package jnpf.yozo.utils;
 
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
 
public class SecretSignatureUtils {
    public static final String SHA256 = "HmacSHA256";
 
    public SecretSignatureUtils() {
    }
 
    public static String hmacSHA256(String data, String key) throws NoSuchAlgorithmException, InvalidKeyException {
        Mac mac = Mac.getInstance(SHA256);
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), SHA256);
        mac.init(secretKey);
        byte[] array = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
        StringBuilder sb = new StringBuilder();
 
        for (byte item : array) {
            sb.append(Integer.toHexString(item & 255 | 256), 1, 3);
        }
 
        return sb.toString().toUpperCase();
    }
}