繁体   English   中英

Shanet6在.net中的更新功能在哪里?

[英]Where is update function in Sha256 in .net?

我在我的c ++代码中使用了Sha256类的更新函数,将一些字符串包含在一个哈希值中,但我在.net类Sha256中找不到这个函数。 这个函数用C ++实现,Sha的Java实现,但不是.net?

C ++中的示例代码:

l_ceSHA2.Init();
for ( l_dwordCnt = 0; l_dwordCnt < l_dwordHashRounds; l_dwordCnt++)
{
    l_ceSHA2.Update( mp_strPassword, strlen( mp_strPassword )));
    l_ceSHA2.Update( mp_byteSalt, 32 );
}
l_ceSHA2.Final( mp_byteCryptoKey);

所以,它就像PBKDF,但更容易。

C中的Sha256代码供参考

您可以使用TransformBlock 这是一个示例,说明如何:

using System;
using System.Text;
using System.Security.Cryptography;

// Example code for using TransformBlock to hash data in chunks

namespace HashTest
{
    class HashTest
    {
        static void Main(string[] args)
        {
            SHA256 hash = SHA256.Create();
            ASCIIEncoding encoding = new ASCIIEncoding();
            string password = "password";

            // Hash a string using ComputeHash
            string sourcetext = password;
            Console.WriteLine(sourcetext);
            byte[] sourcebytes = encoding.GetBytes(sourcetext);
            byte[] hashBytes = hash.ComputeHash(sourcebytes);
            string hashStr = BitConverter.ToString(hashBytes).Replace("-", "");
            Console.WriteLine(hashStr);

            // Hash exactly two copies of a string
            // (used to cross verify other methods below).
            Console.WriteLine();
            sourcetext = password + password;
            Console.WriteLine(sourcetext);
            sourcebytes = encoding.GetBytes(sourcetext);
            hashBytes = hash.ComputeHash(sourcebytes);
            hashStr = BitConverter.ToString(hashBytes).Replace("-", "");
            Console.WriteLine(hashStr);

            // Hash a string using TransformFinalBlock
            Console.WriteLine();
            sourcetext = password;
            sourcebytes = encoding.GetBytes(sourcetext);
            Console.WriteLine(sourcetext);
            hash.TransformFinalBlock(sourcebytes, 0, sourcebytes.Length);
            hashBytes = hash.Hash;
            hashStr = BitConverter.ToString(hashBytes).Replace("-", "");
            Console.WriteLine(hashStr);

            // At this point we've finalized the hash. To
            // reuse it we must first call Initialize().

            // Hash string twice using TransformBlock / TransformFinalBlock
            Console.WriteLine();
            hash.Initialize();
            sourcetext = password;
            sourcebytes = encoding.GetBytes(sourcetext);
            Console.Write(sourcetext);
            hash.TransformBlock(sourcebytes, 0, sourcebytes.Length, null, 0);
            Console.WriteLine(sourcetext);
            hash.TransformFinalBlock(sourcebytes, 0, sourcebytes.Length);
            hashBytes = hash.Hash;
            hashStr = BitConverter.ToString(hashBytes).Replace("-", "");
            Console.WriteLine(hashStr);

            // Hash string twice using TransformBlock in a loop

            Console.WriteLine();
            hash.Initialize();
            sourcetext = password;
            sourcebytes = encoding.GetBytes(sourcetext);
            for (int i = 0; i < 2; ++i)
            {
                Console.Write(sourcetext);
                hash.TransformBlock(sourcebytes, 0, sourcebytes.Length, null, 0);
            }
            Console.WriteLine();
            hash.TransformFinalBlock(sourcebytes, 0, 0);
            hashBytes = hash.Hash;
            hashStr = BitConverter.ToString(hashBytes).Replace("-", "");
            Console.WriteLine(hashStr);
        }
    }
}

暂无
暂无

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

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