Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
order-group
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
order-group-application
order-group
Commits
b1ccfa3a
Commit
b1ccfa3a
authored
Jan 13, 2022
by
ping.wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
微信周周惠活动签名
parent
ff09cd60
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
160 additions
and
1 deletions
+160
-1
order-application-service/pom.xml
+11
-0
order-application-service/src/main/java/cn/freemud/utils/HMacSHA256.java
+52
-0
order-application-service/src/main/java/cn/freemud/utils/RSAUtils.java
+97
-1
No files found.
order-application-service/pom.xml
View file @
b1ccfa3a
...
...
@@ -217,6 +217,17 @@
<!-- <artifactId>open-store-open-platform-sdk</artifactId>-->
<!-- <version>1.3.5-SNAPSHOT</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.github.wechatpay-apiv3</groupId>-->
<!-- <artifactId>wechatpay-apache-httpclient</artifactId>-->
<!-- <version>0.4.0</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.squareup.okhttp3</groupId>-->
<!-- <artifactId>okhttp</artifactId>-->
<!-- <version>3.6.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>
io.springfox
</groupId>
<artifactId>
springfox-swagger2
</artifactId>
...
...
order-application-service/src/main/java/cn/freemud/utils/HMacSHA256.java
0 → 100644
View file @
b1ccfa3a
package
cn
.
freemud
.
utils
;
import
javax.crypto.Mac
;
import
javax.crypto.spec.SecretKeySpec
;
public
class
HMacSHA256
{
/**
* HMacSHA256 类型签名
* @param message 签名字符串
* @param secret 私钥
* @return 加密后字符串
*/
private
static
String
encrypt
(
String
message
,
String
secret
)
{
String
hash
=
""
;
try
{
Mac
sha256_HMAC
=
Mac
.
getInstance
(
"HmacSHA256"
);
SecretKeySpec
secret_key
=
new
SecretKeySpec
(
secret
.
getBytes
(),
"HmacSHA256"
);
sha256_HMAC
.
init
(
secret_key
);
byte
[]
bytes
=
sha256_HMAC
.
doFinal
(
message
.
getBytes
());
hash
=
byteArrayToHexString
(
bytes
);
// System.out.println(hash);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Error HmacSHA256 ==========="
+
e
.
getMessage
());
}
return
hash
;
}
/**
* 将加密后的字节数组转化成字符串
* @param b
* @return
*/
private
static
String
byteArrayToHexString
(
byte
[]
b
)
{
StringBuilder
hs
=
new
StringBuilder
();
String
stmp
;
for
(
int
n
=
0
;
b
!=
null
&&
n
<
b
.
length
;
n
++)
{
stmp
=
Integer
.
toHexString
(
b
[
n
]
&
0XFF
);
if
(
stmp
.
length
()
==
1
)
hs
.
append
(
'0'
);
hs
.
append
(
stmp
);
}
return
hs
.
toString
().
toLowerCase
();
}
String
buildMessage
(
String
apply_mchid
,
String
sign_mchid
,
String
store_id
)
{
return
"apply_mchid="
+
apply_mchid
+
"&sign_mchid="
+
sign_mchid
+
"&store_id="
+
store_id
;
}
}
order-application-service/src/main/java/cn/freemud/utils/RSAUtils.java
View file @
b1ccfa3a
package
cn
.
freemud
.
utils
;
//import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
//import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
//import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
//import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
//import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
//import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import
org.apache.commons.codec.binary.Base64
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.tomcat.util.http.fileupload.IOUtils
;
import
org.junit.After
;
import
org.junit.Before
;
import
javax.crypto.Cipher
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.UnsupportedEncodingException
;
import
java.security.*
;
import
java.security.interfaces.RSAPrivateKey
;
...
...
@@ -15,6 +26,9 @@ import java.security.spec.X509EncodedKeySpec;
import
java.util.HashMap
;
import
java.util.Map
;
//import java.security.Signature;
//import java.util.Base64;
public
class
RSAUtils
{
public
static
final
String
CHARSET
=
"UTF-8"
;
...
...
@@ -186,6 +200,87 @@ public class RSAUtils {
return
resultDatas
;
}
// String privateKey = "";
//
// String mchId = "";
//
// String mchSerialNo = "";
//
// String apiV3Key = "";
//
// CloseableHttpClient httpClient;
//
// @Before
// public void setup() throws IOException {
// // 加载商户私钥(privateKey:私钥字符串)
// PrivateKey merchantPrivateKey = PemUtil
// .loadPrivateKey(new ByteArrayInputStream(privateKey.getBytes("utf-8")));
//
// // 加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥)
// AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
// new WechatPay2Credentials(mchId, new PrivateKeySigner(mchSerialNo, merchantPrivateKey)),apiV3Key.getBytes("utf-8"));
//
// // 初始化httpClient
// CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create()
// .withMerchant(mchId, mchSerialNo, merchantPrivateKey)
// .withValidator(new WechatPay2Validator(verifier)).build();
// }
//
// @After
// public void after() throws IOException {
// httpClient.close();
// }
//
//
//
// String url = "https://api.mch.weixin.qq.com/v3/certificates";
//
// // Authorization: <schema> <token>
//// GET - getToken("GET", httpurl, "")
//// POST - getToken("POST", httpurl, json)
// String schema = "WECHATPAY2-SHA256-RSA2048";
// HttpUrl httpurl = HttpUrl.parse(url);
//
// String getToken(String method, HttpUrl url, String body) throws UnsupportedEncodingException, SignatureException, NoSuchAlgorithmException, InvalidKeyException {
// String nonceStr = "your nonce string";
// String yourMerchantId = "your nonce string";
// String yourCertificateSerialNo = "your nonce string";
//
// long timestamp = System.currentTimeMillis() / 1000;
// String message = buildMessage(method, url, timestamp, nonceStr, body);
// String signature = sign(message.getBytes("utf-8"));
//
// return "mchid=\"" + yourMerchantId + "\","
// + "nonce_str=\"" + nonceStr + "\","
// + "timestamp=\"" + timestamp + "\","
// + "serial_no=\"" + yourCertificateSerialNo + "\","
// + "signature=\"" + signature + "\"";
// }
//
// String sign(byte[] message) throws NoSuchAlgorithmException, SignatureException, UnsupportedEncodingException, InvalidKeyException {
// PrivateKey yourPrivateKey = PemUtil
// .loadPrivateKey(new ByteArrayInputStream(privateKey.getBytes("utf-8")));
//
// Signature sign = Signature.getInstance("SHA256withRSA");
// sign.initSign(yourPrivateKey);
// sign.update(message);
//
// return java.util.Base64.getEncoder().encodeToString(sign.sign());
//
//
// }
//
// String buildMessage(String method, HttpUrl url, long timestamp, String nonceStr, String body) {
// String canonicalUrl = url.encodedPath();
// if (url.encodedQuery() != null) {
// canonicalUrl += "?" + url.encodedQuery();
// }
//
// return method + "\n"
// + canonicalUrl + "\n"
// + timestamp + "\n"
// + nonceStr + "\n"
// + body + "\n";
// }
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment