From 3dbc7b5700bfa033d216307e6c45fcd24ddd872f Mon Sep 17 00:00:00 2001 From: Alawn Date: Mon, 25 Jan 2021 13:58:23 +0800 Subject: [PATCH] ci --- content/docs/API/HRO签名DEMO.md | 96 ++++++++++++++++++++++++++++++++ content/docs/API/文件压缩解压.md | 40 +++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 content/docs/API/HRO签名DEMO.md create mode 100644 content/docs/API/文件压缩解压.md diff --git a/content/docs/API/HRO签名DEMO.md b/content/docs/API/HRO签名DEMO.md new file mode 100644 index 0000000..91a1d1c --- /dev/null +++ b/content/docs/API/HRO签名DEMO.md @@ -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 = "v1.0diskApply20210104000000000001202001042359592020122920201229000000000000000000000001/data/send/20201229000000000000000000000001v1OHJEMIA1EINGUGAH0AITHOHNUEYAHWOO1000001c5950a259435d3e51262539a10a22910"; + + 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 = "000000交易成功20210104235959"; + 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" }; + +} +``` diff --git a/content/docs/API/文件压缩解压.md b/content/docs/API/文件压缩解压.md new file mode 100644 index 0000000..97fc0e5 --- /dev/null +++ b/content/docs/API/文件压缩解压.md @@ -0,0 +1,40 @@ +# 文件压缩/解压 + +## Maven配置 + +```xml + + net.lingala.zip4j + zip4j + 2.6.4 + +``` + +## 压缩 + +```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 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)