package cn.anicert.secure.access.crypto.util;

import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Hex;

import java.util.Arrays;

public class SM3Utils extends BaseCryptoUtil {

    /**
     * 计算SM3摘要值
     *
     * @param srcData 原文
     * @return 摘要值，对于SM3算法来说是32字节
     */
    public static String sm3Hash(byte[] srcData) {
        SM3Digest digest = new SM3Digest();
        digest.update(srcData, 0, srcData.length);
        byte[] hash = new byte[digest.getDigestSize()];
        digest.doFinal(hash, 0);
        String result = Hex.toHexString(hash);
        return result.toUpperCase();
    }

    /**
     * 计算两项身份信息hash
     *
     * @param idno 身份证号
     * @param name 姓名
     * @return 摘要值，对于SM3算法来说是32字节
     */
    public static String sm3Hash(String idno, String name) {
        String str = idno + name;
        byte[] srcData = str.getBytes();
        return sm3Hash(srcData);
    }
}
