package jj.tech.paolu.biz.webadmin.service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.util.encoders.Hex;

public class SM3Test {
	
	/**
     * 使用SM3算法计算给定输入的摘要值
     * @param inputBytes 要进行摘要计算的输入字符串
     * @return 给定输入的摘要值
     */
    private static byte[] calculateSM3Digest( byte [] inputBytes) {
        //创建 SM3Digest 对象
        SM3Digest digest = new SM3Digest();
        //将输入字符串转为字节数组，并使用该字节数组更新摘要对象的内部状态，以便进行计算
       // byte[] inputBytes = input.getBytes();
        digest.update(inputBytes, 0, inputBytes.length);
        //创建一个输出字节数组，调用 doFinal 方法完成哈希计算，并将结果存入输出数组
        byte[] output = new byte[digest.getDigestSize()];
        digest.doFinal(output, 0);
        //返回摘要字节数组
        return output;
    }
    public static void main(String[] args) {
        String filePath = "C:\\Users\\lenovo\\git\\ailt\\src\\test\\resources\\logback.xml";
        try {
           
            byte [] inputBytes = Files.readAllBytes(Paths.get(filePath));
            byte[] digest = calculateSM3Digest(inputBytes);
            String hexDigest = Hex.toHexString(digest);
            System.out.println("SM3 Digest: " + hexDigest);
        } catch (IOException e) {
            System.out.println("An error occurred while reading the file: " + e.getMessage());
        }
    }

}
