import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.ObjectUtils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class SignUtils {
private static final String HMAC_SHA256 = "HmacSHA256";
private SignUtils() {
}
public static String sign(String plainText, String key) {
Mac mac;
try {
mac = Mac.getInstance(HMAC_SHA256);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(HMAC_SHA256 + "实例化异常");
}
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), HMAC_SHA256);
try {
mac.init(secretKeySpec);
} catch (InvalidKeyException e) {
throw new RuntimeException(HMAC_SHA256 + "初始化异常");
}
byte[] bytes = mac.doFinal(plainText.getBytes());
return Hex.encodeHexString(bytes);
}
public static void main(String[] args) {
long timestamp = System.currentTimeMillis();
System.out.println(timestamp);
/**
* public static final String COLON = ":";
*/
String plainText = "458862758047744" + ParamConstant.COLON + timestamp;
String encryptText = SignUtils.sign(plainText, "573572af-da84-43ef-8bcd-7634cb334b09");
System.out.println(encryptText);
}
签名工具类
最新推荐文章于 2022-12-06 23:41:42 发布