Merge remote-tracking branch 'origin/preview' into preview
commit
a8bf9b7390
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||
* www.hnapay.com
|
||||
*/
|
||||
|
||||
package com.mcwl.common.constant;
|
||||
|
||||
/**
|
||||
* 常量配置
|
||||
*/
|
||||
public class NcountKey {
|
||||
|
||||
public static String NCOUNT_URL = "https://ncount.hnapay.com/api/";
|
||||
|
||||
//新生签名使用的算法
|
||||
public static String ALGORITHM = "RSA";
|
||||
|
||||
//类型
|
||||
public static String SIGN_ALGORITHM = "SHA1withRSA";
|
||||
|
||||
//前置系统公钥
|
||||
/*public static String NCOUNT_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9vGvYjivDF5uPBNDXMtoAtjYQ2YPSsfareduDG6kHL/N3A05rFHA11Dbr+UON82Y4V0RFKAQeZFPWcTLjcy6ntZVI8XoYLpuVQBPsb0Ya+PwbzR8/TmUdUf91ru8APtJgqkULgPVrO1hhzZ1tQMznosNLTOqbknMnnMcwzB5yYwIDAQAB";*/
|
||||
|
||||
public static String NCOUNT_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9vGvYjivDF5uPBNDXMtoAtjYQ" +
|
||||
"2YPSsfareduDG6kHL/N3A05rFHA11Dbr+UON82Y4V0RFKAQeZFPWcTLjcy6ntZVI" +
|
||||
"8XoYLpuVQBPsb0Ya+PwbzR8/TmUdUf91ru8APtJgqkULgPVrO1hhzZ1tQMznosNL" +
|
||||
"TOqbknMnnMcwzB5yYwIDAQAB";
|
||||
|
||||
|
||||
// public static String MER_PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAOMNbm6VJwmOQh74\r\n" +
|
||||
// "DyhrKpX+qLKYeeW3sUoXmmstka5AeAVf3UjHCwuZQHviwwGxUIdi0r320aIUetb1\r\n" +
|
||||
// "gRPm4WDMz9FPsBs54hPhHxIBrt3mXEl9cP4S8YdlivQY7Hbf1WqxNhy5wEf0OpkZ\r\n" +
|
||||
// "JjS7wEU46JfKwbGuzcSVkgWi+iuTAgMBAAECgYBX9OAO3qDcxss+jAHzOnCo4pof\r\n" +
|
||||
// "dYnjP6HiffpIAsMoqbjU/CoRA2p398pPM60vfq0fhVz2UiMrkptzom96cjpkNpQ4\r\n" +
|
||||
// "4uztdoozxKmnHiIV1ttISLqMjfh4xkapxVkFJDcEn8ZTpiMmfjEHX0ao3xk1gprL\r\n" +
|
||||
// "vLMpRr1ar5vCqFEGIQJBAPgrGYpteTtjpZqskP9Pg9O7E4XuAgJ01Kqd7Mp8s3Ij\r\n" +
|
||||
// "1nPUmhG+GDDV6MTNDVUfvLWTTvc4MjU2xTQm+6cu2RcCQQDqN72gqLbaKAMULUCt\r\n" +
|
||||
// "vVST7eT53OBPMjbBi90JRqNPZrXuS+Wr7tw3zVb8j5p0w2qBotK7wEq9Pmjidlma\r\n" +
|
||||
// "JhblAkAT95vJeH9tmNnlNQdBk4s+yOmMOBcvTWkjBSvdKuVhRKLnlO6EiO/3Ve6x\r\n" +
|
||||
// "jvIIrFIJpf2PhJT9lReO16gdznYnAkEApvOCpNF+PXUF9Mzo7K8naabKb9yku28E\r\n" +
|
||||
// "nuPOc/05DbEd/7V0CiFiMhiZZ8AJDi/5bFyesBw6ajjhVIOeDQYXjQJARsJP8PDK\r\n" +
|
||||
// "8sEtHtXwOn2vKbx1lSpT9aX3uK/eB7zJLUMBcAsLsoHZC+YqjKjoSD4sGM6ukH0w\r\n" +
|
||||
// "zzlZdaTU3Ued3g==";
|
||||
|
||||
//测试环境商户私钥
|
||||
/*public static String MER_PRIVATE_KEY = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAM2Ak5XX86FypU3t\n" +
|
||||
"DSXlDQp+eXgowPLjBoEt0ZfEfhUtLIrU8J4YzUiz9tUzI6o6mGafDltUasdyRtt3\n" +
|
||||
"+NdSQuFq7MhZyWwGUqle3ybGswYVkN8+LMADCQEOYphM4p0j/OGw7QYcL/ERHsUW\n" +
|
||||
"2MGPR6M/9xe0KURIDdeJC0ulxQ8TAgMBAAECgYEAqgTfldBl68PbSb5+60Q25uC+\n" +
|
||||
"hJBLGmiDwCp1Rng1zDVkrbQBIvvoE2d5Jui5cCQsZHLhwOP1t+8u+nKmuRaA51vB\n" +
|
||||
"sQ+F01SqYrmS/TV/iD5xjAteSXmTf76doP7FwrHuRQ391KcdIn+W0R7cv1tLBY5B\n" +
|
||||
"oRnAcAwWhQMiRWf+yPECQQD6TtJ7kETd5Rl3YryFMTvfW73oHxss87jz6/tUIPxF\n" +
|
||||
"rXYu+ywD4Ut1AWwtN6Td6kZhWOUhG2d4OUVYP0FtjjprAkEA0izql6EQB7SF8AE5\n" +
|
||||
"j8U7Weg8IqPk8b78neav8y1CpjSuwOaRxcS1Mk+W2/AXRvTRL2VUw+Az4boAz3MS\n" +
|
||||
"WnP3+QJBAJW6vtfhRgDCQciTjJfUCq+irmgC63upUzK+L7p0fNOlXrPd1ceAZmXY\n" +
|
||||
"cwBdb8a7lFw5E98JCIz3HU8bj6TcVn8CQQC3EfLBYsSwwXjNBnQaHAVHmfcPgU0B\n" +
|
||||
"mYc3DuKB4hGk7kytr+iXqHDgQt039FiCMOso6o28FEV8v+TGzgfVjCAZAkA9NWuj\n" +
|
||||
"VEHwFLZLtftLDQxGfJmWJGl4CipOjv6YpQEw0A2NBtUp5tuiJ09DNq5RaKJbfWUY\n" +
|
||||
"fQJkAZ0tsrXBWqgK";*/
|
||||
|
||||
public static String MER_PRIVATE_KEY = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBALAkYQn6emeoRsStWebayuyqMB91j27j+gMJO+YqdK1jVpgp4yh2pja9TxQvoLnd3mJcjyACYvW8imWjgtRDDJK4FOj1pYK+EgbX+32PpD8STDnbC93JertbexKk3NNQi5cur40qGEWRspJEPHZTvobZ+EhsJlH8b/oGG1VKORlXAgMBAAECgYA5qf82tBCEhNt+Ib4vzGroZaLRebwWMn4zpj88z6NY7A96SyQ5p/YsL3eHBTyZ/J+EFgcpFH7vMeUg9xGqibTAexqulCNTvOQBYNGra85yUsnob19sHR/YCx80HI5el8RD2mRL6kbmOnab5yi5O2fc8tl+DRJztKXU3YgNH+OhsQJBAPUENKWJJU/OloKYAvOceAy66ZzEKI/myt5x5jcmZTp7bc0wzsxQQ60TfTqfmpOqEd9cJWxggHvutzCBqPovOZkCQQC4CcaSKs3Z2E9/uPr92Z+DIv/5RHTkq66WstG5iM/5aoz7Hf7mP8xTgcjtKNPTcb+nC5IE6M41IuWnSAz8bCBvAkEAuR/FW3GFFIil5eRPDpuhhRTQn9cCQsABdhMe+3pyL77Elq2TAoJ+r82y59FhrMJqIJZx3DIJwhn6pZbCAs4duQJBAKTMrDIrflY660WWIbu7aqmFZ+6QrJQwYJC15DPeHU5daY1dGOqZ6qBEseYdD0Jis5okNEeNKUpPy5J2ZJqNQKUCQQDdyqJhwxjiQ7BtafYNEf/fmP5bkHSKhzOM6lIN8cLX6h535xHObHdOpmKgUg9mLHgJvlNQy+glh40AF2HdNcLS";
|
||||
|
||||
|
||||
public static String MER_ID = "300050402928";
|
||||
|
||||
/**
|
||||
* 支付宝的收款账户id
|
||||
*/
|
||||
public static String ZFB_MER_ID = "2405101553133642463";
|
||||
|
||||
//测试环境商户ID
|
||||
//public static String MER_ID = "110000030228";
|
||||
|
||||
}
|
|
@ -0,0 +1,114 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.mcwl.common.exception;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* NCountDemoException
|
||||
*/
|
||||
public class NCountDemoException extends Exception {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -7121642511651992156L;
|
||||
|
||||
/**
|
||||
* 模块名,抛出异常的类
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
protected Class errClass = null;
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
*/
|
||||
protected String errCode = null;
|
||||
|
||||
/**
|
||||
* 错误描述信息
|
||||
*/
|
||||
protected String errMsg = null;
|
||||
|
||||
public NCountDemoException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public NCountDemoException(String errMsg) {
|
||||
super("no errMsg");
|
||||
this.errMsg = errMsg;
|
||||
}
|
||||
|
||||
public NCountDemoException(String errMsg, Exception e) {
|
||||
super("no errMsg", e);
|
||||
this.errMsg = errMsg;
|
||||
}
|
||||
|
||||
public NCountDemoException(String errCode, String errMsg) {
|
||||
super(errMsg);
|
||||
this.errCode = errCode;
|
||||
this.errMsg = errMsg;
|
||||
}
|
||||
|
||||
public NCountDemoException(String errCode, String errMsg, Exception e) {
|
||||
super(errMsg, e);
|
||||
this.errCode = errCode;
|
||||
this.errMsg = errMsg;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public NCountDemoException(String errCode, Class errClass) {
|
||||
super("no errMsg");
|
||||
this.errCode = errCode;
|
||||
this.errClass = errClass;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public NCountDemoException(String errCode, String errMsg, Class errClass) {
|
||||
super(errMsg);
|
||||
this.errCode = errCode;
|
||||
this.errMsg = errMsg;
|
||||
this.errClass = errClass;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public NCountDemoException(String errCode, String errMsg, Class errClass, Exception e) {
|
||||
super(errMsg, e);
|
||||
this.errClass = errClass;
|
||||
this.errCode = errCode;
|
||||
this.errMsg = errMsg;
|
||||
}
|
||||
|
||||
public String printErrMsg() {
|
||||
StringBuffer msg = new StringBuffer("");
|
||||
if (StringUtils.isNotBlank(errCode)) {
|
||||
msg.append("Exception errCode is [").append(this.errCode).append("];\n");
|
||||
}
|
||||
|
||||
if (this.errClass != null) {
|
||||
msg.append("Exception Class is [").append(this.errClass.getName()).append("];\n");
|
||||
}
|
||||
|
||||
msg.append("Exception Message is [").append(this.errMsg).append("];\n");
|
||||
|
||||
// 打印错误异常堆栈
|
||||
if (getCause() != null) {
|
||||
getCause().printStackTrace();
|
||||
}
|
||||
|
||||
return msg.toString();
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Class getErrClass() {
|
||||
return errClass;
|
||||
}
|
||||
|
||||
public String getErrCode() {
|
||||
return errCode;
|
||||
}
|
||||
|
||||
public String getErrMsg() {
|
||||
return errMsg;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,197 @@
|
|||
package com.mcwl.common.utils;
|
||||
|
||||
import com.alibaba.fastjson2.util.DateUtils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class ToDateUtils {
|
||||
/** 时间格式:yyyyMMddHHmmss'. */
|
||||
public static String DEFAULT_PATTERN = "yyyyMMddHHmmss";
|
||||
|
||||
/** 时间格式:yyyyMMdd'. */
|
||||
public static String yyyyMMdd = "yyyyMMdd";
|
||||
|
||||
/** 时间格式:yyyy-MM-dd'. */
|
||||
public static String yyyy_MM_dd = "yyyy-MM-dd";
|
||||
|
||||
/** 时间格式:HH:mm:ss'. */
|
||||
public static String HH_mm_ss = "HH:mm:ss";
|
||||
|
||||
/** 时间格式:yyyy-MM-dd' 'HH:mm:ss'. */
|
||||
public static String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd' 'HH:mm:ss";
|
||||
|
||||
/** 时间格式:yyyy-MM-dd' 'HH:mm'. */
|
||||
public static String yyyy_MM_dd_HH_mm = "yyyy-MM-dd' 'HH:mm";
|
||||
|
||||
/**
|
||||
* 获取当前时间(格式:yyyyMMddHHmmss)
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getCurrDate() {
|
||||
return getCurrDate(DEFAULT_PATTERN);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转日期,默认格式为yyyyMMddHHmmss
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static Date strToDate(String str) {
|
||||
return strToDate(str, DEFAULT_PATTERN);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期格式化,默认格式为yyyyMMddHHmmss
|
||||
*
|
||||
* @param date
|
||||
* 日期
|
||||
* @return
|
||||
*/
|
||||
public static String format(Date date) {
|
||||
return DateUtils.format(date, DEFAULT_PATTERN);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转日期
|
||||
*
|
||||
* @param str
|
||||
* 时间
|
||||
* @param pattern
|
||||
* 格式
|
||||
* @return
|
||||
*/
|
||||
public static Date strToDate(String str, String pattern) {
|
||||
SimpleDateFormat format = new SimpleDateFormat(pattern);
|
||||
Date date = null;
|
||||
try {
|
||||
date = format.parse(str);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return date;
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期格式化
|
||||
*
|
||||
* @param date
|
||||
* 日期
|
||||
* @param pattern
|
||||
* 格式
|
||||
* @return
|
||||
*/
|
||||
public static String format(Date date, String pattern) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
||||
|
||||
return sdf.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间
|
||||
*
|
||||
* @param pattern
|
||||
* 格式
|
||||
* @return
|
||||
*/
|
||||
public static String getCurrDate(String pattern) {
|
||||
long d = System.currentTimeMillis();
|
||||
Date date = new Date(d);
|
||||
|
||||
return format(date, pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统当前时间
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Date getSystemDate() {
|
||||
long d = System.currentTimeMillis();
|
||||
Date date = new Date(d);
|
||||
|
||||
return date;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间
|
||||
*
|
||||
* @param strTime
|
||||
* @param pattern
|
||||
* @return
|
||||
*/
|
||||
public static String getTime(String strTime, String pattern) {
|
||||
SimpleDateFormat s = new SimpleDateFormat(pattern);
|
||||
try {
|
||||
return String.valueOf(s.parse(strTime).getTime());// 时间戳
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
return String.valueOf(new Date().getTime());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期格式
|
||||
*
|
||||
* @param date
|
||||
* @param mask
|
||||
* @return
|
||||
*/
|
||||
public static final String date2string(Date date, String style) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(style);
|
||||
return sdf.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本地日期时间.
|
||||
*
|
||||
* @return 本地日期时间yyyyMMddHHmmss
|
||||
*/
|
||||
public static String getLocalFullDateTime14() {
|
||||
return date2string(new Date(), DEFAULT_PATTERN);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回指定格式时间
|
||||
* @param mask
|
||||
* @return
|
||||
*/
|
||||
public static final String now2string(String mask){
|
||||
return date2string(new Date(), mask);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param date
|
||||
* @param field
|
||||
* @param amount
|
||||
* @return 对指定的日期做加减运算;<br>
|
||||
* 减:add(new Date(), Calendar.DATE, -1)返回昨天的日期<br>
|
||||
* 加:add(new Date(), Calendar.YEAR, 1)返回一年后的今天
|
||||
*/
|
||||
public static Date add(Date date, int field, int amount) {
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.setTime(date);
|
||||
c.add(field, amount);
|
||||
return c.getTime();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回需要格式的字符串时间
|
||||
* @param str 字符串时间
|
||||
* @param patternS 字符串格式
|
||||
* @param patternE 格式化后的格式
|
||||
* @return
|
||||
*/
|
||||
public static String strToStr(String str, String patternS,String patternE){
|
||||
|
||||
return format(strToDate(str,patternS),patternE);
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
|
||||
package com.mcwl.common.utils.sign;
|
||||
|
||||
/**
|
||||
* 十六进制转换
|
||||
*/
|
||||
public class HexStringByte {
|
||||
|
||||
/**
|
||||
* 字符串转换成十六进制值
|
||||
* @param bin String 我们看到的要转换成十六进制的字符串
|
||||
* @return
|
||||
*/
|
||||
public static String stringToHex(String bin) {
|
||||
char[] digital = "0123456789ABCDEF".toCharArray();
|
||||
StringBuffer sb = new StringBuffer("");
|
||||
byte[] bs = bin.getBytes();
|
||||
int bit;
|
||||
for (int i = 0; i < bs.length; i++) {
|
||||
bit = (bs[i] & 0x0f0) >> 4;
|
||||
sb.append(digital[bit]);
|
||||
bit = bs[i] & 0x0f;
|
||||
sb.append(digital[bit]);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 十六进制转换字符串
|
||||
* @param hex String 十六进制
|
||||
* @return String 转换后的字符串
|
||||
*/
|
||||
public static String hexToString(String hex) {
|
||||
String digital = "0123456789ABCDEF";
|
||||
char[] hex2char = hex.toCharArray();
|
||||
byte[] bytes = new byte[hex.length() / 2];
|
||||
int temp;
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
temp = digital.indexOf(hex2char[2 * i]) * 16;
|
||||
temp += digital.indexOf(hex2char[2 * i + 1]);
|
||||
bytes[i] = (byte) (temp & 0xff);
|
||||
}
|
||||
return new String(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* java字节码转字符串
|
||||
* @param bts
|
||||
* @return
|
||||
*/
|
||||
|
||||
public static String byteToHex(byte[] bts) {
|
||||
String des = "";
|
||||
String tmp = null;
|
||||
for (int i = 0; i < bts.length; i++) {
|
||||
tmp = (Integer.toHexString(bts[i] & 0xFF));
|
||||
if (tmp.length() == 1) {
|
||||
des += "0";
|
||||
}
|
||||
des += tmp;
|
||||
}
|
||||
return des;
|
||||
}
|
||||
|
||||
/**
|
||||
* hex2byte.
|
||||
*
|
||||
* @param hexStr hexStr
|
||||
* @return byte[]
|
||||
*/
|
||||
public static byte[] hex2byte(String hexStr) {
|
||||
byte[] bts = new byte[hexStr.length() / 2];
|
||||
for (int i = 0, j = 0; j < bts.length; j++) {
|
||||
bts[j] = (byte) Integer.parseInt(hexStr.substring(i, i + 2), 16);
|
||||
i += 2;
|
||||
}
|
||||
return bts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转java字节码
|
||||
* @param b
|
||||
* @return
|
||||
*/
|
||||
public static byte[] hexToByte(byte[] b) {
|
||||
if ((b.length % 2) != 0) {
|
||||
throw new IllegalArgumentException("长度不是偶数");
|
||||
}
|
||||
byte[] b2 = new byte[b.length / 2];
|
||||
for (int n = 0; n < b.length; n += 2) {
|
||||
String item = new String(b, n, 2);
|
||||
// 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个进制字节
|
||||
b2[n / 2] = (byte) Integer.parseInt(item, 16);
|
||||
}
|
||||
b = null;
|
||||
return b2;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,161 @@
|
|||
|
||||
package com.mcwl.common.utils.sign;
|
||||
|
||||
|
||||
import com.baidu.aip.util.Base64Util;
|
||||
|
||||
import com.mcwl.common.constant.NcountKey;
|
||||
import com.mcwl.common.exception.NCountDemoException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.security.Key;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
|
||||
/**
|
||||
* 签名 验签 加解密
|
||||
*/
|
||||
@Slf4j
|
||||
public class HnapaySign {
|
||||
|
||||
/**
|
||||
* RSA最大加密明文大小
|
||||
*/
|
||||
private static final int MAX_ENCRYPT_BLOCK = 117;
|
||||
|
||||
|
||||
/**
|
||||
* 签名
|
||||
*
|
||||
* @param privateKey
|
||||
* @param merData 明文串
|
||||
* @return 签名后的消息
|
||||
*/
|
||||
public static byte[] sign(PrivateKey privateKey, String merData) throws NCountDemoException {
|
||||
|
||||
try {
|
||||
return RSAAlgorithms.sign(privateKey, merData);
|
||||
} catch (Exception e) {
|
||||
throw new NCountDemoException("100F1002", "读取密钥异常", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证签名
|
||||
*
|
||||
* @param merData 明文串
|
||||
* @param signMsg 签名消息
|
||||
* @return 验证签名的结果 true--成功 false--失败
|
||||
* @throws NCountDemoException
|
||||
*/
|
||||
public static boolean verify(String merData, String signMsg)
|
||||
throws NCountDemoException {
|
||||
boolean result = false;
|
||||
String hexPublicKey = HexStringByte.byteToHex(Base64Util.decode(NcountKey.NCOUNT_PUBLIC_KEY));
|
||||
String signVal = HexStringByte.byteToHex(Base64Util.decode(signMsg));
|
||||
result = verifySignatureByRSA(merData, signVal, "UTF-8", hexPublicKey);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param src
|
||||
* @param dit
|
||||
* @param charsetType
|
||||
* @param publicKey
|
||||
* @return
|
||||
* @throws NCountDemoException
|
||||
*/
|
||||
private static boolean verifySignatureByRSA(String src, String dit, String charsetType, String publicKey)
|
||||
throws NCountDemoException {
|
||||
if ((src == null) || ("".equals(src.trim()))) {
|
||||
throw new NCountDemoException("src is empty ,verifySignatureByRSA无法执行");
|
||||
}
|
||||
if ((dit == null) || ("".equals(dit.trim()))) {
|
||||
throw new NCountDemoException("dit is empty ,verifySignatureByRSA无法执行");
|
||||
}
|
||||
try {
|
||||
return RSAAlgorithms.verify(publicKey, src, dit);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new NCountDemoException("验证签名出现异常:请检查输入参数", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 公钥加密
|
||||
* </p>
|
||||
*
|
||||
* @param data 源数据
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] encryptByPublicKey(byte[] data, String key) throws Exception {
|
||||
if (data == null) {
|
||||
throw new Exception("需要加密的数据为空");
|
||||
}
|
||||
PublicKey publicKey = getPublicKey(key);
|
||||
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
|
||||
KeyFactory keyFactory = KeyFactory.getInstance(NcountKey.ALGORITHM);
|
||||
Key publicK = keyFactory.generatePublic(x509KeySpec);
|
||||
// 对数据加密
|
||||
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
|
||||
cipher.init(Cipher.ENCRYPT_MODE, publicK);
|
||||
int inputLen = data.length;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
int offSet = 0;
|
||||
byte[] cache;
|
||||
int i = 0;
|
||||
// 对数据分段加密
|
||||
while (inputLen - offSet > 0) {
|
||||
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
|
||||
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
|
||||
} else {
|
||||
cache = cipher.doFinal(data, offSet, inputLen - offSet);
|
||||
}
|
||||
out.write(cache, 0, cache.length);
|
||||
i++;
|
||||
offSet = i * MAX_ENCRYPT_BLOCK;
|
||||
}
|
||||
byte[] encryptedData = out.toByteArray();
|
||||
out.close();
|
||||
return encryptedData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param publicKey 密钥信息
|
||||
* @return 返回公钥
|
||||
* @throws Exception
|
||||
*/
|
||||
private static PublicKey getPublicKey(String publicKey) throws Exception {
|
||||
PublicKey pubKey = null;
|
||||
try {
|
||||
KeyFactory keyFactory = KeyFactory.getInstance(NcountKey.ALGORITHM);
|
||||
pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(Base64Util.decode(publicKey)));
|
||||
} catch (InvalidKeySpecException e) {
|
||||
throw new Exception("公钥无效!", e);
|
||||
}
|
||||
return pubKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* hex2byte.
|
||||
*
|
||||
* @param hexStr hexStr
|
||||
* @return byte[]
|
||||
*/
|
||||
public static byte[] hex2byte(String hexStr) {
|
||||
byte[] bts = new byte[hexStr.length() / 2];
|
||||
for (int i = 0, j = 0; j < bts.length; j++) {
|
||||
bts[j] = (byte) Integer.parseInt(hexStr.substring(i, i + 2), 16);
|
||||
i += 2;
|
||||
}
|
||||
return bts;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.mcwl.common.utils.sign;
|
||||
|
||||
|
||||
import com.baidu.aip.util.Base64Util;
|
||||
import com.mcwl.common.constant.NcountKey;
|
||||
import com.mcwl.common.exception.NCountDemoException;
|
||||
|
||||
|
||||
import java.security.KeyFactory;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
|
||||
public class KeyUtils {
|
||||
|
||||
/**
|
||||
* 加载私钥
|
||||
*
|
||||
*/
|
||||
public static PrivateKey loadPrivateKey(String privateKey) throws NCountDemoException {
|
||||
// 去除头尾标志
|
||||
// 去除换行符
|
||||
privateKey = privateKey.replace("\r", "").replace("\n", "").replace(" ", "");
|
||||
byte[] bPriKey = Base64Util.decode(privateKey);
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bPriKey);
|
||||
KeyFactory keyFactory;
|
||||
try {
|
||||
keyFactory = KeyFactory.getInstance(NcountKey.ALGORITHM);
|
||||
PrivateKey key = keyFactory.generatePrivate(keySpec);
|
||||
return key;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new NCountDemoException("加载私钥异常", e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* Copyright (c) 2016. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||
* www.hnapay.com
|
||||
*/
|
||||
|
||||
package com.mcwl.common.utils.sign;
|
||||
|
||||
|
||||
|
||||
|
||||
import com.mcwl.common.constant.NcountKey;
|
||||
import com.mcwl.common.exception.NCountDemoException;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.*;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
|
||||
/**
|
||||
* 签名验签
|
||||
*/
|
||||
public class RSAAlgorithms {
|
||||
|
||||
/**
|
||||
* @param publicKey
|
||||
* 公钥HEX字符串
|
||||
* @return 返回公钥
|
||||
* @throws Exception
|
||||
*/
|
||||
public static PublicKey getPublicKey(String publicKey) throws NCountDemoException {
|
||||
PublicKey pubKey = null;
|
||||
try {
|
||||
byte[] encodedKey = HexStringByte.hexToByte(publicKey.getBytes());
|
||||
KeyFactory keyFactory = KeyFactory.getInstance(NcountKey.ALGORITHM);
|
||||
pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
|
||||
} catch (InvalidKeySpecException e) {
|
||||
throw new NCountDemoException("公钥无效!", e);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new NCountDemoException("算法类型无效!", e);
|
||||
}
|
||||
return pubKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param priKey
|
||||
* 私钥
|
||||
* @param data
|
||||
* 要签名的数据
|
||||
* @return 签名消息
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] sign(PrivateKey priKey, String data) throws NCountDemoException {
|
||||
try {
|
||||
Signature signet = Signature.getInstance(NcountKey.SIGN_ALGORITHM);
|
||||
signet.initSign(priKey);
|
||||
signet.update(data.getBytes("UTF-8"));
|
||||
return signet.sign();
|
||||
} catch (Exception e) {
|
||||
throw new NCountDemoException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证签名
|
||||
*
|
||||
* @param publicKey
|
||||
* 公钥HEX字符串
|
||||
* @param merData
|
||||
* 签名数据
|
||||
* @param signMsg
|
||||
* 签名消息
|
||||
* @return 返回验证结果 true 成功 false 失败
|
||||
* @throws Exception
|
||||
*/
|
||||
public static Boolean verify(String publicKey, String merData, String signMsg) throws NCountDemoException {
|
||||
|
||||
boolean bVerify = false;
|
||||
Signature signet = null;
|
||||
try {
|
||||
signet = Signature.getInstance(NcountKey.SIGN_ALGORITHM);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new NCountDemoException("算法类型不能为空!", e);
|
||||
}
|
||||
|
||||
PublicKey pubKey = getPublicKey(publicKey);
|
||||
|
||||
try {
|
||||
signet.initVerify(pubKey);
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new NCountDemoException("公钥无效!", e);
|
||||
}
|
||||
try {
|
||||
signet.update(merData.getBytes("UTF-8"));
|
||||
} catch (SignatureException e) {
|
||||
throw new NCountDemoException("验签时符号异常!", e);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new NCountDemoException("不支持的编码方式", e);
|
||||
}
|
||||
|
||||
try {
|
||||
bVerify = signet.verify(HexStringByte.hex2byte(signMsg));
|
||||
} catch (SignatureException e) {
|
||||
throw new NCountDemoException("验签异常!", e);
|
||||
}
|
||||
return bVerify;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
//package com.mcwl.common.web;
|
||||
//
|
||||
//
|
||||
//import org.springframework.dao.DataAccessException;
|
||||
//
|
||||
//import java.util.List;
|
||||
//
|
||||
///**
|
||||
// * 公共mapper
|
||||
//
|
||||
// */
|
||||
//public interface BaseMapper<K, V> {
|
||||
//
|
||||
// /**
|
||||
// * 根据条件查询数据的数量
|
||||
// *
|
||||
// * @param v 实体类
|
||||
// * @return 数量
|
||||
// */
|
||||
// int searchCount(V v);
|
||||
//
|
||||
// /**
|
||||
// * 根据条件查询数据
|
||||
// *
|
||||
// * @param v 实体类
|
||||
// * @return List<K> 实体类的集合
|
||||
// * @throws DataAccessException 数据访问异常
|
||||
// */
|
||||
// List<K> searchByCondition(V v) throws DataAccessException;
|
||||
//
|
||||
//}
|
|
@ -91,9 +91,9 @@ public class PublishCommissionServiceImpl extends ServiceImpl<PublishCommissionM
|
|||
.orderByDesc(PublishCommission::getCreateTime);
|
||||
// 添加父评论
|
||||
List<PublishCommission> modelCommentList = publishCommentMapper.selectList(lqw);
|
||||
for (PublishCommission modelComment : modelCommentList) {
|
||||
PublicModelCommentVo modelCommentVo = getModelCommentVo(modelComment);
|
||||
CommentVoList.add(modelCommentVo);
|
||||
for (PublishCommission Comment : modelCommentList) {
|
||||
PublicModelCommentVo CommentVo = getModelCommentVo(Comment);
|
||||
CommentVoList.add(CommentVo);
|
||||
}
|
||||
|
||||
return CommentVoList;
|
||||
|
@ -101,17 +101,17 @@ public class PublishCommissionServiceImpl extends ServiceImpl<PublishCommissionM
|
|||
|
||||
|
||||
/**
|
||||
* 构建ModelCommentVo对象
|
||||
* 构建CommentVo对象
|
||||
*
|
||||
* @param Comment 父评论对象
|
||||
* @return ModelCommentVo对象
|
||||
* @return CommentVo对象
|
||||
*/
|
||||
@NotNull
|
||||
private PublicModelCommentVo getModelCommentVo(PublishCommission Comment) {
|
||||
Long userId = Comment.getUserId();
|
||||
SysUser sysUser = sysUserService.selectUserById(userId);
|
||||
|
||||
// 构建ModelCommentVo对象
|
||||
// 构建CommentVo对象
|
||||
PublicModelCommentVo CommentVo = new PublicModelCommentVo();
|
||||
CommentVo.setUserId(userId);
|
||||
CommentVo.setUserName(sysUser.getUserName());
|
||||
|
@ -129,18 +129,18 @@ public class PublishCommissionServiceImpl extends ServiceImpl<PublishCommissionM
|
|||
/**
|
||||
* 递归查询子评论
|
||||
*
|
||||
* @param modelCommentId 父评论id
|
||||
* @param CommentId 父评论id
|
||||
* @return List<ModelCommentVo>
|
||||
*/
|
||||
private List<PublicModelCommentVo> getContentList(Long modelCommentId) {
|
||||
private List<PublicModelCommentVo> getContentList(Long CommentId) {
|
||||
List<PublicModelCommentVo> CommentVoList = new ArrayList<>();
|
||||
if (Objects.isNull(modelCommentId)) {
|
||||
if (Objects.isNull(CommentId)) {
|
||||
return CommentVoList;
|
||||
}
|
||||
|
||||
// 查询子评论
|
||||
LambdaQueryWrapper<PublishCommission> lqw = new LambdaQueryWrapper<PublishCommission>()
|
||||
.eq(PublishCommission::getParentId, modelCommentId)
|
||||
.eq(PublishCommission::getParentId, CommentId)
|
||||
.orderByDesc(PublishCommission::getCreateTime);
|
||||
|
||||
List<PublishCommission> CommentList = publishCommentMapper.selectList(lqw);
|
||||
|
|
Loading…
Reference in New Issue