c# – .net中Sha256的更新功能在哪里?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – .net中Sha256的更新功能在哪里?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2753字,纯文字阅读大概需要4分钟。
内容图文
![c# – .net中Sha256的更新功能在哪里?](/upload/InfoBanner/zyjiaocheng/800/fa66a7b19830461fbb53a256ecc874d1.jpg)
我在我的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,但更容易.
Sha256 code in C for reference
解决方法:
您可以使用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);
}
}
}
内容总结
以上是互联网集市为您收集整理的c# – .net中Sha256的更新功能在哪里?全部内容,希望文章能够帮你解决c# – .net中Sha256的更新功能在哪里?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。