优化签名

This commit is contained in:
egan
2017-12-05 23:51:10 +08:00
parent 7336933100
commit 27630651aa
18 changed files with 171 additions and 177 deletions

View File

@@ -1,6 +1,6 @@
package com.egzosn.pay.common.util.sign;
import com.egzosn.pay.common.util.sign.SM3.SM3Digest;
import com.egzosn.pay.common.util.sign.sm3.SM3Digest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -150,6 +150,7 @@ public class SecureUtil {
}
public static boolean validateSignBySoft256(PublicKey publicKey, byte[] signData, byte[] srcData) throws Exception {
Signature st = Signature.getInstance(BC_PROV_ALGORITHM_SHA256RSA, "BC");
st.initVerify(publicKey);
st.update(srcData);

View File

@@ -4,8 +4,8 @@ package com.egzosn.pay.common.util.sign.encrypt;
import com.egzosn.pay.common.util.str.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;
import java.io.UnsupportedEncodingException;
import java.security.SignatureException;
import static com.egzosn.pay.common.util.str.StringUtils.getContentBytes;
/**
* MD5签名工具
@@ -42,20 +42,5 @@ public class MD5 {
/**
* @param content 需要加密串
* @param charset 字符集
* @return 加密后的字节数组
*/
public static byte[] getContentBytes(String content, String charset) {
if (StringUtils.isEmpty(charset)) {
return content.getBytes();
}
try {
return content.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
}
}
}

View File

@@ -0,0 +1,46 @@
package com.egzosn.pay.common.util.sign.encrypt;
import com.egzosn.pay.common.util.str.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;
/**
* SHA1签名工具
* @author Actinia
* @email hayesfu@qq.com
* @create 2017 2017/11/27 0027
*/
public class SHA1 {
/**
* 签名字符串
*
* @param text 需要签名的字符串
* @param key 密钥
* @param input_charset 编码格式
* @return 签名结果
*/
public static String sign(String text, String key, String input_charset) {
//拼接key
text = text + key;
return DigestUtils.sha1Hex( StringUtils.getContentBytes(text, input_charset));
}
/**
* 签名字符串
*
* @param text 需要签名的字符串
* @param sign 签名结果
* @param key 密钥
* @param input_charset 编码格式
* @return 签名结果
*/
public static boolean verify(String text, String sign, String key, String input_charset) {
//判断是否一样
return StringUtils.equals(sign(text, key, input_charset).toUpperCase(), sign.toUpperCase());
}
}

View File

@@ -1,14 +1,11 @@
package com.egzosn.pay.common.util.sign.encrypt;/**
* Description
* author: Fuzx
* date: 2017/11/27 0027
*/
package com.egzosn.pay.common.util.sign.encrypt;
import com.egzosn.pay.common.util.str.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;
/**
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
/**
* @author Actinia
@@ -16,74 +13,34 @@ import java.security.MessageDigest;
* @create 2017 2017/11/27 0027
*/
public class SHA256 {
//日志
protected static final Log log = LogFactory.getLog(SHA256.class);
/**
* 算法常量: SHA256
*/
private static final String ALGORITHM_SHA256 = "SHA-256";
/**
* sha256计算后进行16进制转换
* 签名字符串
*
* @param data
* 待计算的数据
* @param encoding
* 编码
* @return 计算结果
* @param text 需要签名的字符串
* @param key 密钥
* @param input_charset 编码格式
* @return 签名结果
*/
public static byte[] sha256X16(String data, String encoding) {
byte[] bytes = sha256(data, encoding);
StringBuilder sha256StrBuff = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
if (Integer.toHexString(0xFF & bytes[i]).length() == 1) {
sha256StrBuff.append("0").append(
Integer.toHexString(0xFF & bytes[i]));
} else {
sha256StrBuff.append(Integer.toHexString(0xFF & bytes[i]));
}
}
try {
return sha256StrBuff.toString().getBytes(encoding);
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e);
return null;
}
public static String sign(String text, String key, String input_charset) {
//拼接key
text = text + key;
return DigestUtils.sha512Hex( StringUtils.getContentBytes(text, input_charset));
}
/**
* sha256计算
* 签名字符串
*
* @param datas
* 待计算的数据
* @param encoding
* 字符集编码
* @return
* @param text 需要签名的字符串
* @param sign 签名结果
* @param key 密钥
* @param input_charset 编码格式
* @return 签名结果
*/
private static byte[] sha256(String datas, String encoding) {
try {
return sha256(datas.getBytes(encoding));
} catch (UnsupportedEncodingException e) {
log.error("SHA256计算失败", e);
return null;
}
}
/**
* sha256计算.
*
* @param data
* 待计算的数据
* @return 计算结果
*/
private static byte[] sha256(byte[] data) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance(ALGORITHM_SHA256);
md.reset();
md.update(data);
return md.digest();
} catch (Exception e) {
log.error("SHA256计算失败", e);
return null;
}
public static boolean verify(String text, String sign, String key, String input_charset) {
//判断是否一样
return StringUtils.equals(sign(text, key, input_charset).toUpperCase(), sign.toUpperCase());
}
}

View File

@@ -1,4 +1,4 @@
package com.egzosn.pay.common.util.sign.SM3;
package com.egzosn.pay.common.util.sign.sm3;
public class SM3
{

View File

@@ -1,4 +1,4 @@
package com.egzosn.pay.common.util.sign.SM3;
package com.egzosn.pay.common.util.sign.sm3;
public class SM3Digest
{

View File

@@ -1,4 +1,4 @@
package com.egzosn.pay.common.util.sign.SM3;
package com.egzosn.pay.common.util.sign.sm3;
import java.math.BigInteger;

View File

@@ -1,5 +1,7 @@
package com.egzosn.pay.common.util.str;
import java.io.UnsupportedEncodingException;
/**
* Created by ZaoSheng on 2016/6/4.
*/
@@ -108,4 +110,20 @@ public class StringUtils {
return !StringUtils.isBlank(cs);
}
/**
* @param content 需要加密串
* @param charset 字符集
* @return 加密后的字节数组
*/
public static byte[] getContentBytes(String content, String charset) {
if (StringUtils.isEmpty(charset)) {
return content.getBytes();
}
try {
return content.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("转码过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
}
}
}