繁体   English   中英

sha256:如何使用算法作为输入

[英]sha256 : How to use the algorithm as input

好的,这是我的问题。 我知道如何使用 C# 来 HASH,我将这个 function 制作为 ZFAE8A9257E154175DA4193D74Z 文件并提供进度条:

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

这工作完美。 现在假设我想使用 Sha256 代替 hash。 我所要做的就是改变这一行:

    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)

看起来很简单,因为它们都是来自同一类型的抽象 class 。 但我缺乏在 C# 中执行此操作的知识。

感谢帮助

使您的方法接收基本 class 作为参数:

public String SHA384CheckSum(String pstrFilePath, HashAlgorithm objSHA)

现在您的调用代码可以像这样调用它:

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

显然, SHA384CheckSum已经不再是这个 function 的好名字了,所以接下来就是重命名了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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