All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.founder.ecrx.utils.SM3Util Maven / Gradle / Ivy

There is a newer version: 3.6.1.9
Show newest version
package com.founder.ecrx.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.math.BigInteger;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;

public class SM3Util
{
    private static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    private static final String ivHexStr = "7380166f 4914b2b9 172442d7 da8a0600 a96f30bc 163138aa e38dee4d b0fb0e4e";
    private static final BigInteger IV = new BigInteger("7380166f 4914b2b9 172442d7 da8a0600 a96f30bc 163138aa e38dee4d b0fb0e4e".replaceAll(" ", ""), 16);
    private static final Integer Tj15 = Integer.valueOf("79cc4519", 16);
    private static final Integer Tj63 = Integer.valueOf("7a879d8a", 16);
    private static final byte[] FirstPadding = { -128 };
    private static final byte[] ZeroPadding = { 0 };

    private static int T(int j)
    {
        if ((j >= 0) && (j <= 15)) {
            return Tj15.intValue();
        }
        if ((j >= 16) && (j <= 63)) {
            return Tj63.intValue();
        }
        throw new RuntimeException("data invalid");
    }

    private static Integer FF(Integer x, Integer y, Integer z, int j)
    {
        if ((j >= 0) && (j <= 15)) {
            return Integer.valueOf(x.intValue() ^ y.intValue() ^ z.intValue());
        }
        if ((j >= 16) && (j <= 63)) {
            return Integer.valueOf(x.intValue() & y.intValue() | x
                    .intValue() & z.intValue() | y
                    .intValue() & z.intValue());
        }
        throw new RuntimeException("data invalid");
    }

    private static Integer GG(Integer x, Integer y, Integer z, int j)
    {
        if ((j >= 0) && (j <= 15)) {
            return Integer.valueOf(x.intValue() ^ y.intValue() ^ z.intValue());
        }
        if ((j >= 16) && (j <= 63)) {
            return Integer.valueOf(x.intValue() & y.intValue() |
                    (x.intValue() ^ 0xFFFFFFFF) & z.intValue());
        }
        throw new RuntimeException("data invalid");
    }

    private static Integer P0(Integer x)
    {
        return Integer.valueOf(x.intValue() ^
                Integer.rotateLeft(x.intValue(), 9) ^
                Integer.rotateLeft(x.intValue(), 17));
    }

    private static Integer P1(Integer x)
    {
        return Integer.valueOf(x.intValue() ^
                Integer.rotateLeft(x.intValue(), 15) ^
                Integer.rotateLeft(x.intValue(), 23));
    }

    private static byte[] padding(byte[] source)
            throws IOException
    {
        if (source.length >= 2305843009213693952L) {
            throw new RuntimeException("src data invalid.");
        }
        long l = source.length * 8;
        long k = 448L - (l + 1L) % 512L;
        if (k < 0L) {
            k += 512L;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(source);
        baos.write(FirstPadding);
        long i = k - 7L;
        while (i > 0L)
        {
            baos.write(ZeroPadding);
            i -= 8L;
        }
        baos.write(long2bytes(l));
        return baos.toByteArray();
    }

    private static byte[] long2bytes(long l)
    {
        byte[] bytes = new byte[8];
        for (int i = 0; i < 8; i++) {
            bytes[i] = ((byte)(int)(l >>> (7 - i) * 8));
        }
        return bytes;
    }

    public static byte[] hash(byte[] srcData)
    {
        SM3Digest digest = new SM3Digest();
        digest.update(srcData, 0, srcData.length);
        byte[] hash = new byte[digest.getDigestSize()];
        digest.doFinal(hash, 0);
        return hash;
    }

    private static byte[] CF(byte[] vi, byte[] bi)
            throws IOException
    {
        int a = toInteger(vi, 0);
        int b = toInteger(vi, 1);
        int c = toInteger(vi, 2);
        int d = toInteger(vi, 3);
        int e = toInteger(vi, 4);
        int f = toInteger(vi, 5);
        int g = toInteger(vi, 6);
        int h = toInteger(vi, 7);

        int[] w = new int[68];
        int[] w1 = new int[64];
        for (int i = 0; i < 16; i++) {
            w[i] = toInteger(bi, i);
        }
        for (int j = 16; j < 68; j++) {
            w[j] = (P1(Integer.valueOf(w[(j - 16)] ^ w[(j - 9)] ^ Integer.rotateLeft(w[(j - 3)], 15))).intValue() ^ Integer.rotateLeft(w[(j - 13)], 7) ^ w[(j - 6)]);
        }
        for (int j = 0; j < 64; j++) {
            w[j] ^= w[(j + 4)];
        }
        for (int j = 0; j < 64; j++)
        {
            int ss1 = Integer.rotateLeft(
                    Integer.rotateLeft(a, 12) + e +
                            Integer.rotateLeft(T(j), j), 7);
            int ss2 = ss1 ^ Integer.rotateLeft(a, 12);
            int tt1 = FF(Integer.valueOf(a), Integer.valueOf(b), Integer.valueOf(c), j).intValue() + d + ss2 + w1[j];
            int tt2 = GG(Integer.valueOf(e), Integer.valueOf(f), Integer.valueOf(g), j).intValue() + h + ss1 + w[j];
            d = c;
            c = Integer.rotateLeft(b, 9);
            b = a;
            a = tt1;
            h = g;
            g = Integer.rotateLeft(f, 19);
            f = e;
            e = P0(Integer.valueOf(tt2)).intValue();
        }
        byte[] v = toByteArray(a, b, c, d, e, f, g, h);
        for (int i = 0; i < v.length; i++) {
            v[i] = ((byte)(v[i] ^ vi[i]));
        }
        return v;
    }

    private static int toInteger(byte[] source, int index)
    {
        StringBuilder valueStr = new StringBuilder("");
        for (int i = 0; i < 4; i++)
        {
            valueStr.append(hexDigits[((byte)((source[(index * 4 + i)] & 0xF0) >> 4))]);
            valueStr.append(hexDigits[((byte)(source[(index * 4 + i)] & 0xF))]);
        }
        return Long.valueOf(valueStr.toString(), 16).intValue();
    }

    private static byte[] toByteArray(int a, int b, int c, int d, int e, int f, int g, int h)
            throws IOException
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(32);
        baos.write(toByteArray(a));
        baos.write(toByteArray(b));
        baos.write(toByteArray(c));
        baos.write(toByteArray(d));
        baos.write(toByteArray(e));
        baos.write(toByteArray(f));
        baos.write(toByteArray(g));
        baos.write(toByteArray(h));
        return baos.toByteArray();
    }

    public static byte[] toByteArray(int i)
    {
        byte[] byteArray = new byte[4];
        byteArray[0] = ((byte)(i >>> 24));
        byteArray[1] = ((byte)((i & 0xFFFFFF) >>> 16));
        byteArray[2] = ((byte)((i & 0xFFFF) >>> 8));
        byteArray[3] = ((byte)(i & 0xFF));
        return byteArray;
    }

    private static String byteToHexString(byte b)
    {
        int n = b;
        if (n < 0) {
            n = 256 + n;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return "" + hexDigits[d1] + hexDigits[d2];
    }

    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();
    }

    public static void main(String[] args)
            throws IOException
    {
        System.out.println(byteArrayToHexString(hash("test sm3 hash".getBytes())));
        System.out.println(ByteUtils.toHexString(hash("test sm3 hash".getBytes())));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy