ci
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
# HRO签名DEMO
|
||||
|
||||
|
||||
```java
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* @Author: Alawn
|
||||
* @Description:
|
||||
* @Date: Create in 6:40 下午 2021/1/20
|
||||
*/
|
||||
public class SignDemo {
|
||||
|
||||
private final static String secret = "Giur9quiuD";
|
||||
private final static String charSet = "UTF-8";
|
||||
|
||||
@Test
|
||||
public void sign() throws NoSuchAlgorithmException, UnsupportedEncodingException {
|
||||
String reqBody = "<root><head><version>v1.0</version><serviceName>diskApply</serviceName><requestId>20210104000000000001</requestId><requestTime>20200104235959</requestTime></head><body><applyDate>20201229</applyDate><diskNo>20201229000000000000000000000001</diskNo><filePath>/data/send/20201229000000000000000000000001</filePath><fileVersion>v1</fileVersion><customerId>OHJEMIA1EINGUGAH0AITHOHNUEYAHWOO</customerId><titleId>1000001</titleId><md5>c5950a259435d3e51262539a10a22910</md5></body></root>";
|
||||
|
||||
MessageDigest md5Digest = MessageDigest.getInstance("MD5");
|
||||
|
||||
String md5 = byteArrayToHexString(md5Digest.digest(reqBody.getBytes(charSet)));
|
||||
|
||||
MessageDigest shaDigest = MessageDigest.getInstance("SHA-256");
|
||||
|
||||
byte[] signBuf = new byte[md5.getBytes(charSet).length + secret.getBytes(charSet).length];
|
||||
|
||||
System.arraycopy(md5.getBytes(charSet),0,signBuf,0,md5.getBytes(charSet).length);
|
||||
|
||||
System.arraycopy(secret.getBytes(charSet),0,signBuf,0,secret.getBytes(charSet).length);
|
||||
|
||||
String sign = String.valueOf(Base64.getEncoder().encodeToString(shaDigest.digest(signBuf)));
|
||||
|
||||
System.out.println("Sign:[" + sign + "]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifySign() throws NoSuchAlgorithmException, UnsupportedEncodingException {
|
||||
|
||||
String respBody = "<head><respCode>000000</respCode><respMessage>交易成功</respMessage><respTime>20210104235959</respTime></head>";
|
||||
String respSign = "PcPrQVDIsguzP6ASffQM2zFtznHqi7Bz1RhZhALMnTs=";
|
||||
|
||||
|
||||
MessageDigest md5Digest = MessageDigest.getInstance("MD5");
|
||||
|
||||
String md5 = byteArrayToHexString(md5Digest.digest(respBody.getBytes(charSet)));
|
||||
|
||||
MessageDigest shaDigest = MessageDigest.getInstance("SHA-256");
|
||||
|
||||
byte[] signBuf = new byte[md5.getBytes(charSet).length + secret.getBytes(charSet).length];
|
||||
|
||||
System.arraycopy(md5.getBytes(charSet), 0, signBuf, 0, md5.getBytes(charSet).length);
|
||||
|
||||
System.arraycopy(secret.getBytes(charSet), 0, signBuf, 0, secret.getBytes(charSet).length);
|
||||
|
||||
String sign = Base64.getEncoder().encodeToString(shaDigest.digest(signBuf));
|
||||
|
||||
if (!respSign.equals(sign)) {
|
||||
System.out.println("验签失败;请求签名:[" + respSign + "] 验证签名: [" + sign + "]");
|
||||
}
|
||||
else{
|
||||
System.out.println("验签成功");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static String byteArrayToHexString(byte b[]) {
|
||||
StringBuffer resultSb = new StringBuffer();
|
||||
for (int i = 0; i < b.length; i++) {
|
||||
resultSb.append(byteToHexString(b[i]));
|
||||
}
|
||||
|
||||
return resultSb.toString();
|
||||
}
|
||||
|
||||
private static String byteToHexString(byte b) {
|
||||
int n = b;
|
||||
if (n < 0) {
|
||||
n += 256;
|
||||
}
|
||||
int d1 = n / 16;
|
||||
int d2 = n % 16;
|
||||
return hexDigits[d1] + hexDigits[d2];
|
||||
}
|
||||
|
||||
private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
|
||||
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
|
||||
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,40 @@
|
||||
# 文件压缩/解压
|
||||
|
||||
## Maven配置
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>net.lingala.zip4j</groupId>
|
||||
<artifactId>zip4j</artifactId>
|
||||
<version>2.6.4</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
## 压缩
|
||||
|
||||
```java
|
||||
ZipParameters zipParameters = new ZipParameters();
|
||||
zipParameters.setEncryptFiles(true);
|
||||
zipParameters.setEncryptionMethod(EncryptionMethod.AES);
|
||||
// Below line is optional. AES 256 is used by default. You can override it to use AES 128. AES 192 is supported only for extracting.
|
||||
zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
|
||||
|
||||
List<File> filesToAdd = Arrays.asList(
|
||||
new File("somefile"),
|
||||
new File("someotherfile")
|
||||
);
|
||||
|
||||
ZipFile zipFile = new ZipFile("filename.zip", "password".toCharArray());
|
||||
zipFile.addFiles(filesToAdd, zipParameters);
|
||||
```
|
||||
|
||||
|
||||
## 解压
|
||||
|
||||
```java
|
||||
new ZipFile("filename.zip", "password".toCharArray()).extractAll("/destination_directory");
|
||||
```
|
||||
|
||||
## 参考
|
||||
|
||||
[zip4j](https://github.com/srikanth-lingala/zip4j)
|
||||
Reference in New Issue
Block a user