Skip to content

Commit

Permalink
支持HmacSHA256加密方式
Browse files Browse the repository at this point in the history
  • Loading branch information
xibeiwangwei committed Sep 16, 2022
1 parent eea38dd commit 9dacff4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.zto.zop</groupId>
<artifactId>zopsdk</artifactId>
<version>0.8</version>
<version>0.9</version>
<packaging>jar</packaging>

<properties>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/zto/zop/EncryptionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

public enum EncryptionType {
MD5,
SHA256
SHA256,
HmacSHA256

}
6 changes: 3 additions & 3 deletions src/main/java/com/zto/zop/ZopClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ public String execute(ZopPublicRequest request) throws IOException {
strToDigest = strToDigest + properties.getKey();
Map<String, String> headers = new HashMap<String, String>();
headers.put("x-companyid", properties.getCompanyId());
headers.put("x-datadigest", ZopDigestUtil.digest(strToDigest, request.getBase64(), request.getEncryptionType(), request.getTimestamp()));
headers.put("x-datadigest", ZopDigestUtil.digest(strToDigest, request.getBase64(), request.getEncryptionType(), request.getTimestamp(), request.getSecretKey()));
if (request.getTimestamp() != null) {
headers.put("x-timestamp", String.valueOf(request.getTimestamp()));
}
return HttpUtil.post(request.getUrl(), headers, queryString);
} else {
Map<String, String> headers = new HashMap<String, String>();
Map<String, String> headers = new HashMap<>();
String strToDigest = jsonBody + properties.getKey();
headers.put("x-companyid", properties.getCompanyId());
headers.put("x-datadigest", ZopDigestUtil.digest(strToDigest, request.getBase64(), request.getEncryptionType(), request.getTimestamp()));
headers.put("x-datadigest", ZopDigestUtil.digest(strToDigest, request.getBase64(), request.getEncryptionType(), request.getTimestamp(), request.getSecretKey()));
if (request.getTimestamp() != null) {
headers.put("x-timestamp", String.valueOf(request.getTimestamp()));
}
Expand Down
51 changes: 48 additions & 3 deletions src/main/java/com/zto/zop/ZopDigestUtil.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,61 @@
package com.zto.zop;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class ZopDigestUtil {

public static String digest(String str, Boolean isBase64, EncryptionType encryptionType, Long timestamp) {
private static final String HMAC_SHA_256 = "HmacSHA256";

private static final Map<String, Mac> MAC_MAP = new ConcurrentHashMap<>();

public static String digest(String str, Boolean isBase64, EncryptionType encryptionType, Long timestamp, String secretKey) {
if (timestamp != null) {
str = timestamp + str;
}
boolean base64 = isBase64 == null || isBase64;
return base64 ? EncryptionType.SHA256.equals(encryptionType) ? Base64.encodeBase64String(DigestUtils.sha256(str)) : Base64.encodeBase64String(DigestUtils.md5(str))
: EncryptionType.SHA256.equals(encryptionType) ? DigestUtils.sha256Hex(str) : DigestUtils.md5Hex(str);
switch (encryptionType) {
case SHA256:
return base64 ? Base64.encodeBase64String(DigestUtils.sha256(str)) : DigestUtils.sha256Hex(str);
case HmacSHA256:
return base64 ? Base64.encodeBase64String(hmacSha256(secretKey, str)) : hmacSha256Str(secretKey, str);
default:
return base64 ? Base64.encodeBase64String(DigestUtils.md5(str)) : DigestUtils.md5Hex(str);
}
}

public static String hmacSha256Str(String key, String body) {
return Hex.encodeHexString(hmacSha256(key, body));
}

public static byte[] hmacSha256(String key, String body) {
if (Objects.isNull(key)) {
key = "";
}
Mac mac = MAC_MAP.get(key);
if (mac == null) {
try {
mac = Mac.getInstance(HMAC_SHA_256);
SecretKey secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), HMAC_SHA_256);
mac.init(secretKey);
MAC_MAP.put(key, mac);
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
throw new RuntimeException(e);
}
}
return mac.doFinal(body.getBytes(StandardCharsets.UTF_8));
}

}
13 changes: 13 additions & 0 deletions src/main/java/com/zto/zop/ZopPublicRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public class ZopPublicRequest {
*/
private EncryptionType encryptionType;

/**
* HmacSHA256加密key
*/
private String secretKey;

/**
* 时间戳,如果接口文档未标识使用时间戳请不要传值,否则会导致签名错误
*/
Expand Down Expand Up @@ -99,6 +104,14 @@ public void setEncryptionType(EncryptionType encryptionType) {
this.encryptionType = encryptionType;
}

public String getSecretKey() {
return secretKey;
}

public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}

public Long getTimestamp() {
return timestamp;
}
Expand Down

0 comments on commit 9dacff4

Please sign in to comment.