Skip to content

Commit 0fe431f

Browse files
committed
生成sha1签名时,加入参数判断
1 parent 26272bb commit 0fe431f

File tree

2 files changed

+41
-0
lines changed
  • weixin-java-common/src
    • main/java/me/chanjar/weixin/common/util/crypto
    • test/java/me/chanjar/weixin/common/util/crypto

2 files changed

+41
-0
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/SHA1.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.chanjar.weixin.common.util.crypto;
22

33
import org.apache.commons.codec.digest.DigestUtils;
4+
import org.apache.commons.lang3.StringUtils;
45

56
import java.util.Arrays;
67

@@ -13,6 +14,10 @@ public class SHA1 {
1314
* 串接arr参数,生成sha1 digest
1415
*/
1516
public static String gen(String... arr) {
17+
if (StringUtils.isAnyEmpty(arr)) {
18+
throw new IllegalArgumentException("非法请求参数,有部分参数为空 : " + Arrays.toString(arr));
19+
}
20+
1621
Arrays.sort(arr);
1722
StringBuilder sb = new StringBuilder();
1823
for (String a : arr) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package me.chanjar.weixin.common.util.crypto;
2+
3+
import org.testng.annotations.Test;
4+
5+
import static org.testng.Assert.assertEquals;
6+
import static org.testng.Assert.assertNotNull;
7+
8+
/**
9+
* <pre>
10+
* Created by BinaryWang on 2017/6/10.
11+
* </pre>
12+
*
13+
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
14+
*/
15+
public class SHA1Test {
16+
@Test
17+
public void testGen() throws Exception {
18+
final String result = SHA1.gen("123", "345");
19+
assertNotNull(result);
20+
assertEquals(result,"9f537aeb751ec72605f57f94a2f6dc3e3958e1dd");
21+
}
22+
23+
@Test(expectedExceptions = {IllegalArgumentException.class})
24+
public void testGen_illegalArguments() {
25+
final String result = SHA1.gen(null, "", "345");
26+
assertNotNull(result);
27+
}
28+
29+
@Test
30+
public void testGenWithAmple() throws Exception {
31+
final String result = SHA1.genWithAmple("123", "345");
32+
assertNotNull(result);
33+
assertEquals(result,"20b896ccbd5a72dde5dbe0878ff985e4069771c6");
34+
}
35+
36+
}

0 commit comments

Comments
 (0)