cn.net.wanmo.ocr.util.Signature Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ocr-springboot2 Show documentation
Show all versions of ocr-springboot2 Show documentation
文字识别(Optical Character Recognition,OCR )基于行业前沿的深度学习技术,将图片上的文字内容智能识别成为可编辑的文本,可以有效地代替人工录入信息。
The newest version!
package cn.net.wanmo.ocr.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class Signature {
private static Logger logger = LoggerFactory.getLogger(Signature.class);
/**
* 获取签名
*
* @param secretKey 密钥
* @param data 需要签名的数据
* @return 签名
*/
public static String signWithHmacSha256(String secretKey, String data) {
try {
SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
return Base64.getEncoder().encodeToString(mac.doFinal(data.getBytes("UTF-8")));
} catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) {
logger.error("生成接口签名值", e);
}
return null;
}
/**
* 验证签名
*
* @param secretKey 密钥
* @param data 需要签名的数据
* @param hmac 已经签名的数据
* @return true:签名一致
*/
public static boolean verify(String secretKey, String data, String hmac) {
String calculatedHmac = signWithHmacSha256(secretKey, data);
return calculatedHmac.equals(hmac);
}
}