Przeglądaj źródła

生成sha1签名时,加入参数判断

master
Binary Wang 7 lat temu
rodzic
commit
0fe431fc78
2 zmienionych plików z 41 dodań i 0 usunięć
  1. +5
    -0
      weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/SHA1.java
  2. +36
    -0
      weixin-java-common/src/test/java/me/chanjar/weixin/common/util/crypto/SHA1Test.java

+ 5
- 0
weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/SHA1.java Wyświetl plik

@@ -1,6 +1,7 @@
package me.chanjar.weixin.common.util.crypto;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

@@ -13,6 +14,10 @@ public class SHA1 {
* 串接arr参数,生成sha1 digest
*/
public static String gen(String... arr) {
if (StringUtils.isAnyEmpty(arr)) {
throw new IllegalArgumentException("非法请求参数,有部分参数为空 : " + Arrays.toString(arr));
}

Arrays.sort(arr);
StringBuilder sb = new StringBuilder();
for (String a : arr) {


+ 36
- 0
weixin-java-common/src/test/java/me/chanjar/weixin/common/util/crypto/SHA1Test.java Wyświetl plik

@@ -0,0 +1,36 @@
package me.chanjar.weixin.common.util.crypto;

import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

/**
* <pre>
* Created by BinaryWang on 2017/6/10.
* </pre>
*
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
*/
public class SHA1Test {
@Test
public void testGen() throws Exception {
final String result = SHA1.gen("123", "345");
assertNotNull(result);
assertEquals(result,"9f537aeb751ec72605f57f94a2f6dc3e3958e1dd");
}

@Test(expectedExceptions = {IllegalArgumentException.class})
public void testGen_illegalArguments() {
final String result = SHA1.gen(null, "", "345");
assertNotNull(result);
}

@Test
public void testGenWithAmple() throws Exception {
final String result = SHA1.genWithAmple("123", "345");
assertNotNull(result);
assertEquals(result,"20b896ccbd5a72dde5dbe0878ff985e4069771c6");
}

}

Ładowanie…
Anuluj
Zapisz