HmacUtil.java
1.42 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
package com.bckefu.uccc.hmac;
import com.bckefu.uccc.CipherType;
import org.apache.commons.codec.binary.Hex;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.SignatureException;
/**
* Created by caoliang on 2017/8/4.
*/
public class HmacUtil {
/**
*
* @param data
* @param key
* @return
* @throws java.security.SignatureException
*/
public static String hmacSha256Hex(String data, String key , String algorithm) {
try {
// get an hmac_sha1 key from the raw key bytes
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(),algorithm);
// get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance(algorithm);
mac.init(signingKey);
// compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(data.getBytes());
// base64-encode the hmac
byte[] hexB = (new Hex()).encode(rawHmac);
return new String(hexB);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param message
* @param key
* @return
* @throws SignatureException
*/
public static String hmacSha256Hex(String message , String key) throws SignatureException {
return hmacSha256Hex(message , key , CipherType.HMAC_SHA_256);
}
}