简体   繁体   中英

sha256 : How to use the algorithm as input

Ok here is my problem. I know how to HASH using C#, I made this function to Hash Files and provide progress bar:

   public String SHA384CheckSum(String pstrFilePath)
    {
        const Int32 BUFFER_MAX_SIZE = (4 * 1024 * 1000);  //4Mo
        String strRetValue = "";
        String strBaseCaption = this.Text;
        Int64 dblProgression1;
        Int64 dblProgression2 = 0;

        this.Text = strBaseCaption + " [0%]";

        //Should check if file exist first
        if (AppEx.FileExist64(pstrFilePath) == true)
        {
            //using (SHA384 objSHA = SHA384.Create())
            using (SHA384 objSHA = SHA384.Create()) {
                using (FileStream objFileStream = new FileStream(pstrFilePath, FileMode.Open, FileAccess.Read))
                {
                    Int32 _bufferSize;
                    Byte[] readAheadBuffer;
                    Int32 readAheadBytesRead;
                    Int64 lngBytesRemaining = objFileStream.Length;
                    Double dblTotalBytes = lngBytesRemaining;

                    while ((lngBytesRemaining > 0) && (this.glngState != 2))
                    {
                        if (lngBytesRemaining > BUFFER_MAX_SIZE)
                        {
                            _bufferSize = BUFFER_MAX_SIZE;
                        } else {
                            _bufferSize = (Int32)lngBytesRemaining;
                        }

                        readAheadBuffer = new Byte[_bufferSize];
                        readAheadBytesRead = objFileStream.Read(readAheadBuffer, 0, _bufferSize);

                        lngBytesRemaining = (lngBytesRemaining - _bufferSize);
                        if (lngBytesRemaining != 0)
                        {
                            objSHA.TransformBlock(readAheadBuffer, 0, readAheadBytesRead, readAheadBuffer, 0);
                        } else {
                            objSHA.TransformFinalBlock(readAheadBuffer, 0, readAheadBytesRead);
                            strRetValue = BitConverter.ToString(objSHA.Hash).Replace("-", "").ToLower();
                        }

                        dblProgression1 = (Int64)(((dblTotalBytes - lngBytesRemaining) / dblTotalBytes) * 100);
                        if (dblProgression1 != dblProgression2)
                        {
                            dblProgression2 = dblProgression1;
                            this.Text = strBaseCaption + " [" + dblProgression2.ToString() + "%]";
                            Application.DoEvents();
                        }
                    }
                }
            }
        }
        this.Text = strBaseCaption + " [100%]";

        return strRetValue;
    }

This work perfectly. Now let's suppose I want to hash using Sha256 instead. All I have to do is change this line:

    using (SHA384 objSHA = SHA384.Create())
    to
    using (SHA256 objSHA = SHA256.Create())
    
    How can I pass this as a parameter to the function so I could:
    SHA256 objSHA;
    or
    SHA384 objSHA

and then CALL The Function (..., objSHA)

seem simple enough because they are both abstract class coming from the same type. but I lack the knowledge to do that in C#.

thanks for help

Make your method receive the base class as a parameter:

public String SHA384CheckSum(String pstrFilePath, HashAlgorithm objSHA)

Now your calling code can invoke it like so:

bool useSha256 = false; // Initialize this any way you want
using (HashAlgorithm algorithm = useSha256 ? (HashAlgorithm)SHA256.Create() : SHA384.Create())
{
    string checksum = SHA384CheckSum(pstrFilePath, algorithm);
}

Obviously, SHA384CheckSum is no longer a good name for this function, so the next step is to rename it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM