From cbb68695817f3008c9d965487322c21ec822a42d Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Thu, 13 Jun 2024 14:38:25 +0800
Subject: [PATCH] 哈希值工具

---
 src/main/java/com/dji/sample/territory/utils/jym/SM2Factory.java      |  114 +++++++
 src/main/java/com/dji/sample/territory/utils/jym/SM2Result.java       |   20 +
 src/main/java/com/dji/sample/territory/utils/jym/SM2SignVO.java       |  128 ++++++++
 src/main/java/com/dji/sample/territory/utils/jym/SM2SignVerUtils.java |  104 ++++++
 src/main/java/com/dji/sample/territory/utils/SM3.java                 |   59 +++
 src/main/java/com/dji/sample/territory/utils/jym/Util.java            |  476 +++++++++++++++++++++++++++++
 6 files changed, 901 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/dji/sample/territory/utils/SM3.java b/src/main/java/com/dji/sample/territory/utils/SM3.java
new file mode 100644
index 0000000..1156dbb
--- /dev/null
+++ b/src/main/java/com/dji/sample/territory/utils/SM3.java
@@ -0,0 +1,59 @@
+package com.dji.sample.territory.utils;
+
+import com.dji.sample.territory.utils.jym.SM2SignVO;
+import com.dji.sample.territory.utils.jym.SM2SignVerUtils;
+import com.dji.sample.territory.utils.jym.Util;
+import org.bouncycastle.crypto.digests.SM3Digest;
+import org.bouncycastle.util.encoders.Hex;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+public class SM3 {
+    public static String sm3(byte[] data) {
+        byte[] md = new byte[32];
+        SM3Digest sm3 = new SM3Digest();
+        sm3.update(data, 0, data.length);
+        sm3.doFinal(md, 0);
+        String s = new String(Hex.encode(md));
+        return s.toUpperCase();
+    }
+
+
+    public static void main(String[] args) throws Exception {
+        File file = new File("src/main/resources/FJ_11.jpeg");
+        byte[] bytesArray = Files.readAllBytes(file.toPath());
+        String sm=sm3(bytesArray);
+        StringBuffer buffer=addStringBuffer("0561C8D116010B96128A7470436CDEF2DA1F2BCBD21CA228E427D27AFDA5BF10",
+                "2024-06-11 09:29:44",115.8652884,28.6252874,-83,295,0,"中图智绘无人机",
+                "23E57DA1E4AB865CCBC325B668762207DEF74345B782237808AE0BABDF26734D");
+        System.out.println(addJym(buffer));
+    }
+
+    public static String addJym(StringBuffer buffer) throws Exception {
+        String hash = SM3.sm3(buffer.toString().getBytes());
+        String privateKey = "23E57DA1E4AB865CCBC325B668762207DEF74345B782237808AE0BABDF26734D";
+        SM2SignVO sm2signv0 = SM2SignVerUtils.Sign2SM2(Util.hexStringToBytes(privateKey), hash.getBytes(StandardCharsets.UTF_8));
+        return sm2signv0.getSm2_signForHard().toUpperCase();
+    }
+
+    public static StringBuffer addStringBuffer(String FJHXZ, String PSSJ, Double Longitude, Double Latitude, double PSFYJ, double PSJD, int PSHGJ, String PSRY, String ZSDM) {
+        StringBuffer buffer = new StringBuffer();
+        // 添加参数到 StringBuffer
+          buffer.append(FJHXZ).append(",")
+                .append(PSSJ).append(",")
+                .append(Longitude).append(",")
+                .append(Latitude).append(",")
+                .append(PSFYJ).append(",")
+                .append(PSJD).append(",")
+                .append(PSHGJ).append(",")
+                .append(PSRY).append(",")
+                .append(ZSDM);
+                 return buffer;
+    }
+}
+
diff --git a/src/main/java/com/dji/sample/territory/utils/jym/SM2Factory.java b/src/main/java/com/dji/sample/territory/utils/jym/SM2Factory.java
new file mode 100644
index 0000000..f7b4c93
--- /dev/null
+++ b/src/main/java/com/dji/sample/territory/utils/jym/SM2Factory.java
@@ -0,0 +1,114 @@
+package com.dji.sample.territory.utils.jym;
+
+import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
+import org.bouncycastle.crypto.digests.SM3Digest;
+import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
+import org.bouncycastle.crypto.params.ECDomainParameters;
+import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
+import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
+import org.bouncycastle.crypto.params.ECPublicKeyParameters;
+import org.bouncycastle.math.ec.ECCurve;
+import org.bouncycastle.math.ec.ECFieldElement;
+import org.bouncycastle.math.ec.ECFieldElement.Fp;
+import org.bouncycastle.math.ec.ECPoint;
+
+import java.math.BigInteger;
+import java.security.SecureRandom;
+
+public class SM2Factory {
+    private static final BigInteger a = new BigInteger("fffffffeffffffffffffffffffffffffffffffff00000000fffffffffffffffc", 16);
+    private static final BigInteger b = new BigInteger("28e9fa9e9d9f5e344d5a9e4bcf6509a7f39789f515ab8f92ddbcbd414d940e93", 16);
+    private static final BigInteger gx = new BigInteger("32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7", 16);
+    private static final BigInteger gy = new BigInteger("bc3736a2f4f6779c59bdcee36b692153d0a9877cc62a474002df32e52139f0a0", 16);
+    private static final BigInteger n = new BigInteger("fffffffeffffffffffffffffffffffff7203df6b21c6052b53bbf40939d54123", 16);
+    private static final BigInteger p = new BigInteger("fffffffeffffffffffffffffffffffffffffffff00000000ffffffffffffffff", 16);
+    private static final int h = 1;
+    public final ECFieldElement ecc_gx_fieldelement;
+    public final ECFieldElement ecc_gy_fieldelement;
+    public final ECCurve ecc_curve;
+    public final ECPoint ecc_point_g;
+    public final ECDomainParameters ecc_bc_spec;
+    public final ECKeyPairGenerator ecc_key_pair_generator;
+
+    public static SM2Factory getInstance() {
+        return new SM2Factory();
+    }
+
+    public SM2Factory() {
+        this.ecc_gx_fieldelement = new Fp(p, gx);
+        this.ecc_gy_fieldelement = new Fp(p, gy);
+        this.ecc_curve = new ECCurve.Fp(p, a, b);
+        this.ecc_point_g = new ECPoint.Fp(this.ecc_curve, this.ecc_gx_fieldelement, this.ecc_gy_fieldelement, false);
+        this.ecc_bc_spec = new ECDomainParameters(this.ecc_curve, this.ecc_point_g, n);
+        ECKeyGenerationParameters ecc_ecgenparam = new ECKeyGenerationParameters(this.ecc_bc_spec, new SecureRandom());
+        this.ecc_key_pair_generator = new ECKeyPairGenerator();
+        this.ecc_key_pair_generator.init(ecc_ecgenparam);
+    }
+
+    public byte[] sm2GetZ(byte[] userId, ECPoint userKey) {
+        SM3Digest sm3 = new SM3Digest();
+        int len = userId.length * 8;
+        sm3.update((byte)(len >> 8 & 255));
+        sm3.update((byte)(len & 255));
+        sm3.update(userId, 0, userId.length);
+        byte[] p = Util.byteConvert32Bytes(a);
+        sm3.update(p, 0, p.length);
+        p = Util.byteConvert32Bytes(b);
+        sm3.update(p, 0, p.length);
+        p = Util.byteConvert32Bytes(gx);
+        sm3.update(p, 0, p.length);
+        p = Util.byteConvert32Bytes(gy);
+        sm3.update(p, 0, p.length);
+        p = Util.byteConvert32Bytes(userKey.normalize().getXCoord().toBigInteger());
+        sm3.update(p, 0, p.length);
+        p = Util.byteConvert32Bytes(userKey.normalize().getYCoord().toBigInteger());
+        sm3.update(p, 0, p.length);
+        byte[] md = new byte[sm3.getDigestSize()];
+        sm3.doFinal(md, 0);
+        return md;
+    }
+
+    public void sm2Sign(byte[] md, BigInteger userD, ECPoint userKey,SM2Result sm2Result) {
+        BigInteger e = new BigInteger(1, md);
+        BigInteger k = null;
+        ECPoint kp = null;
+        BigInteger r = null;
+        BigInteger s = null;
+
+        while(true) {
+            do {
+                AsymmetricCipherKeyPair keypair = this.ecc_key_pair_generator.generateKeyPair();
+                ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters)keypair.getPrivate();
+                ECPublicKeyParameters ecpub = (ECPublicKeyParameters)keypair.getPublic();
+                k = ecpriv.getD();
+                kp = ecpub.getQ();
+                r = e.add(kp.getXCoord().toBigInteger());
+                r = r.mod(n);
+            } while(r.equals(BigInteger.ZERO));
+
+            if (!r.add(k).equals(n) && r.toString(16).length() == 64) {
+                BigInteger da_1 = userD.add(BigInteger.ONE);
+                da_1 = da_1.modInverse(n);
+                s = r.multiply(userD);
+                s = k.subtract(s).mod(n);
+                s = da_1.multiply(s).mod(n);
+                if (!s.equals(BigInteger.ZERO) && s.toString(16).length() == 64) {
+                    sm2Result.r = r;
+                    sm2Result.s = s;
+                    return;
+                }
+            }
+        }
+    }
+
+    public void sm2Verify(byte[] md, ECPoint userKey, BigInteger r, BigInteger s, SM2Result sm2Result) {
+        sm2Result.R = null;
+        BigInteger e = new BigInteger(1, md);
+        BigInteger t = r.add(s).mod(n);
+        if (!t.equals(BigInteger.ZERO)) {
+            ECPoint x1y1 = this.ecc_point_g.multiply(sm2Result.s);
+            x1y1 = x1y1.add(userKey.multiply(t));
+            sm2Result.R = e.add(x1y1.normalize().getXCoord().toBigInteger()).mod(n);
+        }
+    }
+}
diff --git a/src/main/java/com/dji/sample/territory/utils/jym/SM2Result.java b/src/main/java/com/dji/sample/territory/utils/jym/SM2Result.java
new file mode 100644
index 0000000..1b4d946
--- /dev/null
+++ b/src/main/java/com/dji/sample/territory/utils/jym/SM2Result.java
@@ -0,0 +1,20 @@
+package com.dji.sample.territory.utils.jym;
+
+import org.bouncycastle.math.ec.ECPoint;
+
+import java.math.BigInteger;
+
+public class SM2Result {
+    public BigInteger r;
+    public BigInteger s;
+    public BigInteger R;
+    public byte[] sa;
+    public byte[] sb;
+    public byte[] s1;
+    public byte[] s2;
+    public ECPoint keyra;
+    public ECPoint keyrb;
+
+    public SM2Result() {
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/com/dji/sample/territory/utils/jym/SM2SignVO.java b/src/main/java/com/dji/sample/territory/utils/jym/SM2SignVO.java
new file mode 100644
index 0000000..94280d5
--- /dev/null
+++ b/src/main/java/com/dji/sample/territory/utils/jym/SM2SignVO.java
@@ -0,0 +1,128 @@
+package com.dji.sample.territory.utils.jym;
+
+public class SM2SignVO {
+    public String sm2_userd;
+    public String x_coord;
+    public String y_coord;
+    public String sm3_z;
+    public String sign_express;
+    public String sm3_digest;
+    public String sign_r;
+    public String sign_s;
+    public String verify_r;
+    public String verify_s;
+    public String sm2_sign;
+    public String sm2_type;
+    public boolean isVerify;
+
+    public SM2SignVO() {
+    }
+
+    public String getX_coord() {
+        return this.x_coord;
+    }
+
+    public void setX_coord(String x_coord) {
+        this.x_coord = x_coord;
+    }
+
+    public String getY_coord() {
+        return this.y_coord;
+    }
+
+    public void setY_coord(String y_coord) {
+        this.y_coord = y_coord;
+    }
+
+    public String getSm3_z() {
+        return this.sm3_z;
+    }
+
+    public void setSm3_z(String sm3_z) {
+        this.sm3_z = sm3_z;
+    }
+
+    public String getSm3_digest() {
+        return this.sm3_digest;
+    }
+
+    public void setSm3_digest(String sm3_digest) {
+        this.sm3_digest = sm3_digest;
+    }
+
+    public String getSm2_signForSoft() {
+        return this.sm2_sign;
+    }
+
+    public String getSm2_signForHard() {
+        return this.getSign_r() + this.getSign_s();
+    }
+
+    public void setSm2_sign(String sm2_sign) {
+        this.sm2_sign = sm2_sign;
+    }
+
+    public String getSign_express() {
+        return this.sign_express;
+    }
+
+    public void setSign_express(String sign_express) {
+        this.sign_express = sign_express;
+    }
+
+    public String getSm2_userd() {
+        return this.sm2_userd;
+    }
+
+    public void setSm2_userd(String sm2_userd) {
+        this.sm2_userd = sm2_userd;
+    }
+
+    public String getSm2_type() {
+        return this.sm2_type;
+    }
+
+    public void setSm2_type(String sm2_type) {
+        this.sm2_type = sm2_type;
+    }
+
+    public boolean isVerify() {
+        return this.isVerify;
+    }
+
+    public void setVerify(boolean isVerify) {
+        this.isVerify = isVerify;
+    }
+
+    public String getSign_r() {
+        return this.sign_r;
+    }
+
+    public void setSign_r(String sign_r) {
+        this.sign_r = sign_r;
+    }
+
+    public String getSign_s() {
+        return this.sign_s;
+    }
+
+    public void setSign_s(String sign_s) {
+        this.sign_s = sign_s;
+    }
+
+    public String getVerify_r() {
+        return this.verify_r;
+    }
+
+    public void setVerify_r(String verify_r) {
+        this.verify_r = verify_r;
+    }
+
+    public String getVerify_s() {
+        return this.verify_s;
+    }
+
+    public void setVerify_s(String verify_s) {
+        this.verify_s = verify_s;
+    }
+}
diff --git a/src/main/java/com/dji/sample/territory/utils/jym/SM2SignVerUtils.java b/src/main/java/com/dji/sample/territory/utils/jym/SM2SignVerUtils.java
new file mode 100644
index 0000000..8328ebb
--- /dev/null
+++ b/src/main/java/com/dji/sample/territory/utils/jym/SM2SignVerUtils.java
@@ -0,0 +1,104 @@
+package com.dji.sample.territory.utils.jym;
+
+import com.dji.sample.territory.utils.HashUtil;
+import com.dji.sample.territory.utils.SM3;
+import org.bouncycastle.asn1.*;
+import org.bouncycastle.crypto.digests.SM3Digest;
+import org.bouncycastle.math.ec.ECPoint;
+
+import java.io.ByteArrayInputStream;
+import java.math.BigInteger;
+import java.nio.charset.StandardCharsets;
+import java.util.Enumeration;
+
+public class SM2SignVerUtils {
+    public static void main(String[] args) throws Exception {
+        String data = "0561C8D116010B96128A7470436CDEF2DA1F2BCBD21CA228E427D27AFDA5BF10,2024-06-11 09:29:44,115.8652884,28.6252874,-83,295,0,中图智绘无人机,23C8CCC61E3042FBA6A658F319337B1A"; // 替换为需要哈希的数据
+        String HXZ=SM3.sm3(data.getBytes());
+        SM2SignVO sm2signv0 = SM2SignVerUtils.Sign2SM2(Util.hexStringToBytes("23E57DA1E4AB865CCBC325B668762207DEF74345B782237808AE0BABDF26734D"), HXZ.getBytes(StandardCharsets.UTF_8));
+        String jym= sm2signv0.getSm2_signForHard().toUpperCase();
+        System.out.println(jym);
+    }
+    public static String USER_ID = "1234567812345678";
+
+    public SM2SignVerUtils() {
+    }
+
+    public static SM2SignVO Sign2SM2(byte[] privatekey, byte[] sourceData) throws Exception {
+        SM2SignVO sm2SignVO = new SM2SignVO();
+        sm2SignVO.setSm2_type("sign");
+        SM2Factory factory = SM2Factory.getInstance();
+        BigInteger userD = new BigInteger(1, privatekey);
+        sm2SignVO.setSm2_userd(userD.toString(16));
+        ECPoint userKey = factory.ecc_point_g.multiply(userD);
+        SM3Digest sm3Digest = new SM3Digest();
+        byte[] z = factory.sm2GetZ(USER_ID.getBytes(), userKey);
+        sm2SignVO.setSm3_z(Util.getHexString(z));
+        sm2SignVO.setSign_express(Util.getHexString(sourceData));
+        sm3Digest.update(z, 0, z.length);
+        sm3Digest.update(sourceData, 0, sourceData.length);
+        byte[] md = new byte[32];
+        sm3Digest.doFinal(md, 0);
+        sm2SignVO.setSm3_digest(Util.getHexString(md));
+        SM2Result sm2Result = new SM2Result();
+        factory.sm2Sign(md, userD, userKey, sm2Result);
+        sm2SignVO.setSign_r(sm2Result.r.toString(16));
+        sm2SignVO.setSign_s(sm2Result.s.toString(16));
+        ASN1Integer d_r = new ASN1Integer(sm2Result.r);
+        ASN1Integer d_s = new ASN1Integer(sm2Result.s);
+        ASN1EncodableVector v2 = new ASN1EncodableVector();
+        v2.add(d_r);
+        v2.add(d_s);
+        DERSequence sign = new DERSequence(v2);
+        String result = Util.byteToHex(sign.getEncoded());
+        sm2SignVO.setSm2_sign(result);
+        return sm2SignVO;
+    }
+
+    public static SM2SignVO VerifySignSM2(byte[] publicKey, byte[] sourceData, byte[] signData) {
+        try {
+            SM2SignVO verifyVo = new SM2SignVO();
+            verifyVo.setSm2_type("verify");
+            byte[] formatedPubKey;
+            if (publicKey.length == 64) {
+                formatedPubKey = new byte[65];
+                formatedPubKey[0] = 4;
+                System.arraycopy(publicKey, 0, formatedPubKey, 1, publicKey.length);
+            } else {
+                formatedPubKey = publicKey;
+            }
+
+            SM2Factory factory = SM2Factory.getInstance();
+            ECPoint userKey = factory.ecc_curve.decodePoint(formatedPubKey);
+            SM3Digest sm3Digest = new SM3Digest();
+            byte[] z = factory.sm2GetZ(USER_ID.getBytes(), userKey);
+            verifyVo.setSm3_z(Util.getHexString(z));
+            sm3Digest.update(z, 0, z.length);
+            sm3Digest.update(sourceData, 0, sourceData.length);
+            byte[] md = new byte[32];
+            sm3Digest.doFinal(md, 0);
+            verifyVo.setSm3_digest(Util.getHexString(md));
+            ByteArrayInputStream bis = new ByteArrayInputStream(signData);
+            ASN1InputStream dis = new ASN1InputStream(bis);
+            SM2Result sm2Result = null;
+            ASN1Primitive derObj = dis.readObject();
+            Enumeration<ASN1Integer> e = ((ASN1Sequence)derObj).getObjects();
+            BigInteger r = ((ASN1Integer)e.nextElement()).getValue();
+            BigInteger s = ((ASN1Integer)e.nextElement()).getValue();
+            sm2Result = new SM2Result();
+            sm2Result.r = r;
+            sm2Result.s = s;
+            verifyVo.setVerify_r(sm2Result.r.toString(16));
+            verifyVo.setVerify_s(sm2Result.s.toString(16));
+            factory.sm2Verify(md, userKey, sm2Result.r, sm2Result.s, sm2Result);
+            boolean verifyFlag = sm2Result.r.equals(sm2Result.R);
+            verifyVo.setVerify(verifyFlag);
+            return verifyVo;
+        } catch (IllegalArgumentException var18) {
+            return null;
+        } catch (Exception var19) {
+            var19.printStackTrace();
+            return null;
+        }
+    }
+}
diff --git a/src/main/java/com/dji/sample/territory/utils/jym/Util.java b/src/main/java/com/dji/sample/territory/utils/jym/Util.java
new file mode 100644
index 0000000..ddf60f3
--- /dev/null
+++ b/src/main/java/com/dji/sample/territory/utils/jym/Util.java
@@ -0,0 +1,476 @@
+package com.dji.sample.territory.utils.jym;
+
+import org.bouncycastle.asn1.ASN1EncodableVector;
+import org.bouncycastle.asn1.ASN1Integer;
+import org.bouncycastle.asn1.DERSequence;
+
+import java.io.IOException;
+import java.math.BigInteger;
+
+public class Util {
+    private static final char[] DIGITS_LOWER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+    private static final char[] DIGITS_UPPER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
+
+    public Util() {
+    }
+
+    public static byte[] intToBytes(int num) {
+        byte[] bytes = new byte[]{(byte)(255 & num >> 0), (byte)(255 & num >> 8), (byte)(255 & num >> 16), (byte)(255 & num >> 24)};
+        return bytes;
+    }
+
+//    public static int byteToInt(byte[] bytes) {
+//        int num = 0;
+//        int temp = (255 & bytes[0]) << 0;
+//        int num = num | temp;
+//        temp = (255 & bytes[1]) << 8;
+//        num |= temp;
+//        temp = (255 & bytes[2]) << 16;
+//        num |= temp;
+//        temp = (255 & bytes[3]) << 24;
+//        num |= temp;
+//        return num;
+//    }
+
+    public static byte[] longToBytes(long num) {
+        byte[] bytes = new byte[8];
+
+        for(int i = 0; i < 8; ++i) {
+            bytes[i] = (byte)((int)(255L & num >> i * 8));
+        }
+
+        return bytes;
+    }
+
+    public static byte[] byteConvert32Bytes(BigInteger n) {
+        byte[] tmpd = (byte[])null;
+        if (n == null) {
+            return null;
+        } else {
+            if (n.toByteArray().length == 33) {
+                tmpd = new byte[32];
+                System.arraycopy(n.toByteArray(), 1, tmpd, 0, 32);
+            } else if (n.toByteArray().length == 32) {
+                tmpd = n.toByteArray();
+            } else {
+                tmpd = new byte[32];
+
+                for(int i = 0; i < 32 - n.toByteArray().length; ++i) {
+                    tmpd[i] = 0;
+                }
+
+                System.arraycopy(n.toByteArray(), 0, tmpd, 32 - n.toByteArray().length, n.toByteArray().length);
+            }
+
+            return tmpd;
+        }
+    }
+
+    public static BigInteger byteConvertInteger(byte[] b) {
+        if (b[0] < 0) {
+            byte[] temp = new byte[b.length + 1];
+            temp[0] = 0;
+            System.arraycopy(b, 0, temp, 1, b.length);
+            return new BigInteger(temp);
+        } else {
+            return new BigInteger(b);
+        }
+    }
+
+    public static String getHexString(byte[] bytes) {
+        return getHexString(bytes, true);
+    }
+
+    public static String getHexString(byte[] bytes, boolean upperCase) {
+        String ret = "";
+
+        for(int i = 0; i < bytes.length; ++i) {
+            ret = ret + Integer.toString((bytes[i] & 255) + 256, 16).substring(1);
+        }
+
+        return upperCase ? ret.toUpperCase() : ret;
+    }
+
+    public static void printHexString(byte[] bytes) {
+        for(int i = 0; i < bytes.length; ++i) {
+            String hex = Integer.toHexString(bytes[i] & 255);
+            if (hex.length() == 1) {
+                hex = '0' + hex;
+            }
+
+            System.out.print("0x" + hex.toUpperCase() + ",");
+        }
+
+        System.out.println("");
+    }
+
+    public static byte[] hexStringToBytes(String hexString) {
+        if (hexString != null && !hexString.equals("")) {
+            hexString = hexString.toUpperCase();
+            int length = hexString.length() / 2;
+            char[] hexChars = hexString.toCharArray();
+            byte[] d = new byte[length];
+
+            for(int i = 0; i < length; ++i) {
+                int pos = i * 2;
+                d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
+            }
+
+            return d;
+        } else {
+            return null;
+        }
+    }
+
+    public static byte charToByte(char c) {
+        return (byte)"0123456789ABCDEF".indexOf(c);
+    }
+
+    public static char[] encodeHex(byte[] data) {
+        return encodeHex(data, true);
+    }
+
+    public static char[] encodeHex(byte[] data, boolean toLowerCase) {
+        return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
+    }
+
+    protected static char[] encodeHex(byte[] data, char[] toDigits) {
+        int l = data.length;
+        char[] out = new char[l << 1];
+        int i = 0;
+
+        for(int var5 = 0; i < l; ++i) {
+            out[var5++] = toDigits[(240 & data[i]) >>> 4];
+            out[var5++] = toDigits[15 & data[i]];
+        }
+
+        return out;
+    }
+
+    public static String encodeHexString(byte[] data) {
+        return encodeHexString(data, true);
+    }
+
+    public static String encodeHexString(byte[] data, boolean toLowerCase) {
+        return encodeHexString(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
+    }
+
+    protected static String encodeHexString(byte[] data, char[] toDigits) {
+        return new String(encodeHex(data, toDigits));
+    }
+
+    public static byte[] decodeHex(char[] data) {
+        int len = data.length;
+        if ((len & 1) != 0) {
+            throw new RuntimeException("Odd number of characters.");
+        } else {
+            byte[] out = new byte[len >> 1];
+            int i = 0;
+
+            for(int j = 0; j < len; ++i) {
+                int f = toDigit(data[j], j) << 4;
+                ++j;
+                f |= toDigit(data[j], j);
+                ++j;
+                out[i] = (byte)(f & 255);
+            }
+
+            return out;
+        }
+    }
+
+    protected static int toDigit(char ch, int index) {
+        int digit = Character.digit(ch, 16);
+        if (digit == -1) {
+            throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index);
+        } else {
+            return digit;
+        }
+    }
+
+    public static String StringToAsciiString(String content) {
+        String result = "";
+        int max = content.length();
+
+        for(int i = 0; i < max; ++i) {
+            char c = content.charAt(i);
+            String b = Integer.toHexString(c);
+            result = result + b;
+        }
+
+        return result;
+    }
+
+    public static String hexStringToString(String hexString, int encodeType) {
+        String result = "";
+        int max = hexString.length() / encodeType;
+
+        for(int i = 0; i < max; ++i) {
+            char c = (char)hexStringToAlgorism(hexString.substring(i * encodeType, (i + 1) * encodeType));
+            result = result + c;
+        }
+
+        return result;
+    }
+
+    public static int hexStringToAlgorism(String hex) {
+        hex = hex.toUpperCase();
+        int max = hex.length();
+        int result = 0;
+
+        for(int i = max; i > 0; --i) {
+            char c = hex.charAt(i - 1);
+
+            int algorism;
+            if (c >= '0' && c <= '9') {
+                algorism = c - 48;
+            } else {
+                algorism = c - 55;
+            }
+
+            result = (int)((double)result + Math.pow(16.0D, (double)(max - i)) * (double)algorism);
+        }
+
+        return result;
+    }
+
+    public static String hexStringToBinary(String hex) {
+        hex = hex.toUpperCase();
+        String result = "";
+        int max = hex.length();
+
+        for(int i = 0; i < max; ++i) {
+            char c = hex.charAt(i);
+            switch(c) {
+                case '0':
+                    result = result + "0000";
+                    break;
+                case '1':
+                    result = result + "0001";
+                    break;
+                case '2':
+                    result = result + "0010";
+                    break;
+                case '3':
+                    result = result + "0011";
+                    break;
+                case '4':
+                    result = result + "0100";
+                    break;
+                case '5':
+                    result = result + "0101";
+                    break;
+                case '6':
+                    result = result + "0110";
+                    break;
+                case '7':
+                    result = result + "0111";
+                    break;
+                case '8':
+                    result = result + "1000";
+                    break;
+                case '9':
+                    result = result + "1001";
+                case ':':
+                case ';':
+                case '<':
+                case '=':
+                case '>':
+                case '?':
+                case '@':
+                default:
+                    break;
+                case 'A':
+                    result = result + "1010";
+                    break;
+                case 'B':
+                    result = result + "1011";
+                    break;
+                case 'C':
+                    result = result + "1100";
+                    break;
+                case 'D':
+                    result = result + "1101";
+                    break;
+                case 'E':
+                    result = result + "1110";
+                    break;
+                case 'F':
+                    result = result + "1111";
+            }
+        }
+
+        return result;
+    }
+
+    public static String AsciiStringToString(String content) {
+        String result = "";
+        int length = content.length() / 2;
+
+        for(int i = 0; i < length; ++i) {
+            String c = content.substring(i * 2, i * 2 + 2);
+            int a = hexStringToAlgorism(c);
+            char b = (char)a;
+            String d = String.valueOf(b);
+            result = result + d;
+        }
+
+        return result;
+    }
+
+    public static String algorismToHexString(int algorism, int maxLength) {
+        String result = "";
+        result = Integer.toHexString(algorism);
+        if (result.length() % 2 == 1) {
+            result = "0" + result;
+        }
+
+        return patchHexString(result.toUpperCase(), maxLength);
+    }
+
+    public static String byteToString(byte[] bytearray) {
+        String result = "";
+        int length = bytearray.length;
+
+        for(int i = 0; i < length; ++i) {
+            char temp = (char)bytearray[i];
+            result = result + temp;
+        }
+
+        return result;
+    }
+
+    public static int binaryToAlgorism(String binary) {
+        int max = binary.length();
+        int result = 0;
+
+        for(int i = max; i > 0; --i) {
+            char c = binary.charAt(i - 1);
+            int algorism = c - 48;
+            result = (int)((double)result + Math.pow(2.0D, (double)(max - i)) * (double)algorism);
+        }
+
+        return result;
+    }
+
+    public static String algorismToHEXString(int algorism) {
+        String result = "";
+        result = Integer.toHexString(algorism);
+        if (result.length() % 2 == 1) {
+            result = "0" + result;
+        }
+
+        result = result.toUpperCase();
+        return result;
+    }
+
+    public static String patchHexString(String str, int maxLength) {
+        String temp = "";
+
+        for(int i = 0; i < maxLength - str.length(); ++i) {
+            temp = "0" + temp;
+        }
+
+        str = (temp + str).substring(0, maxLength);
+        return str;
+    }
+
+    public static int parseToInt(String s, int defaultInt, int radix) {
+        boolean var3 = false;
+
+        int i;
+        try {
+            i = Integer.parseInt(s, radix);
+        } catch (NumberFormatException var5) {
+            i = defaultInt;
+        }
+
+        return i;
+    }
+
+    public static int parseToInt(String s, int defaultInt) {
+        boolean var2 = false;
+
+        int i;
+        try {
+            i = Integer.parseInt(s);
+        } catch (NumberFormatException var4) {
+            i = defaultInt;
+        }
+
+        return i;
+    }
+
+    public static byte[] hexToByte(String hex) throws IllegalArgumentException {
+        if (hex.length() % 2 != 0) {
+            throw new IllegalArgumentException();
+        } else {
+            char[] arr = hex.toCharArray();
+            byte[] b = new byte[hex.length() / 2];
+            int i = 0;
+            int j = 0;
+
+            for(int l = hex.length(); i < l; ++j) {
+                String swap = "" + arr[i++] + arr[i];
+                int byteint = Integer.parseInt(swap, 16) & 255;
+                b[j] = (new Integer(byteint)).byteValue();
+                ++i;
+            }
+
+            return b;
+        }
+    }
+
+    public static String byteToHex(byte[] b) {
+        if (b == null) {
+            throw new IllegalArgumentException("Argument b ( byte array ) is null! ");
+        } else {
+            String hs = "";
+            String stmp = "";
+
+            for(int n = 0; n < b.length; ++n) {
+                stmp = Integer.toHexString(b[n] & 255);
+                if (stmp.length() == 1) {
+                    hs = hs + "0" + stmp;
+                } else {
+                    hs = hs + stmp;
+                }
+            }
+
+            return hs.toUpperCase();
+        }
+    }
+
+    public static byte[] subByte(byte[] input, int startIndex, int length) {
+        byte[] bt = new byte[length];
+
+        for(int i = 0; i < length; ++i) {
+            bt[i] = input[i + startIndex];
+        }
+
+        return bt;
+    }
+
+    public static String SM2SignHardToSoft(String hardSign) {
+        byte[] bytes = hexToByte(hardSign);
+        byte[] r = new byte[bytes.length / 2];
+        byte[] s = new byte[bytes.length / 2];
+        System.arraycopy(bytes, 0, r, 0, bytes.length / 2);
+        System.arraycopy(bytes, bytes.length / 2, s, 0, bytes.length / 2);
+        ASN1Integer d_r = new ASN1Integer(byteConvertInteger(r));
+        ASN1Integer d_s = new ASN1Integer(byteConvertInteger(s));
+        ASN1EncodableVector v2 = new ASN1EncodableVector();
+        v2.add(d_r);
+        v2.add(d_s);
+        DERSequence sign = new DERSequence(v2);
+        String result = null;
+
+        try {
+            result = byteToHex(sign.getEncoded());
+            return result;
+        } catch (IOException var10) {
+            var10.printStackTrace();
+            throw new RuntimeException(var10);
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.3