提交 d5c050ff authored 作者: yangqi's avatar yangqi

回调

上级 d30d717d
package cn.lefull.service.ali;
import org.springframework.stereotype.Service;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* @author 杨奇
*/
@Service
public interface CallbackService {
boolean checkSign(String key, String body, String timestamp, String sign) throws NoSuchAlgorithmException, InvalidKeyException;
String hmacSha1(String key, String data, Charset bytesEncode) throws NoSuchAlgorithmException, InvalidKeyException;
}
package cn.lefull.service.ali;
import org.springframework.stereotype.Service;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* @author 杨奇
*/
@Service
public class CallbackServiceImpl implements CallbackService{
@Override
public boolean checkSign(String key, String body, String timestamp, String sign) throws NoSuchAlgorithmException, InvalidKeyException {
return sign.equals(hmacSha1(key, body + timestamp, StandardCharsets.UTF_8));
}
@Override
public String hmacSha1(String key, String data, Charset bytesEncode) throws NoSuchAlgorithmException, InvalidKeyException {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(bytesEncode), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
//通用方法 (由 byte[]转 hex string)
return byteToHexString(mac.doFinal(data.getBytes(bytesEncode)));
}
private String byteToHexString(byte[] bytes){
return new BigInteger(1, bytes).toString(16);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论