using System;
 using System.IO;
 using System.Text;
 using Org.BouncyCastle.Utilities.Encoders;
 
 namespace Com.Bocom.OpenApi
 {
     namespace Security
     {
         namespace Digest
         {
             public class SM3Digest
             {
                 private const int SM3_DIGEST_SIZE = 32;
                 private const int DEFAULT_BUF_SIZE = 65536;
 
                 public static string Digest(string fileName)
                 {
                     Org.BouncyCastle.Crypto.Digests.SM3Digest sm3Digest = new Org.BouncyCastle.Crypto.Digests.SM3Digest();
                     FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                     byte[] buf = new byte[DEFAULT_BUF_SIZE];
                     int num;
 
                     while ((num = fileStream.Read(buf, 0, buf.Length)) > 0)
                     {
                         sm3Digest.BlockUpdate(buf, 0, num);
                     }
 
                     byte[] dig = new byte[SM3_DIGEST_SIZE];
                     sm3Digest.DoFinal(dig, 0);
 
                     return Encoding.UTF8.GetString(Hex.Encode(dig));
                 }
 
                 public static void Main(string[] args)
                 {
                     try
                     {
                         string digStr = SM3Digest.Digest("C:/Users/Administrator/Desktop/test.txt");
                         Console.WriteLine(digStr);
                     }
                     catch (Exception e)
                     {
                         Console.WriteLine(e.Message);
                         Console.WriteLine(e.StackTrace);
                     }
                 }
             }
         }
     }
 }