【发布时间】:2019-03-08 19:19:57
【问题描述】:
早上好, 我需要以哈希模式加密文件。
我想使用这个库
我使用本地 C# 256 方法和 HashLib 方法的测试得到不同的结果,
FileStream fileStream;
SHA256 sha256 = SHA256Managed.Create();
fileStream = new FileStream(localPath, FileMode.Open);
fileStream.Position = 0;
///using System.Security.Cryptography;
byte[] hashValue = sha256.ComputeHash(fileStream);
string hash = ByteArrayToString(hashValue);
#region using HashLib;
//Run Hash
IHash hash256 = HashFactory.Crypto.CreateSHA256();
HashResult result256 = hash256.ComputeStream(fileStream);
byte[] bytearray = result256.GetBytes();
string stringtest = result256.ToString();
stringtest = result256.ToString().Replace("-", "");
#endregion
第一种方法的结果
byte[] hashValue = 94,171,27,169,32,82,120,2,177,84,58,6,216,77,110,239,85,282,75,159,183,85,70,208,22,146,201,22,47,122,153,74
字符串哈希 = 5EAB1BA920527802B1543A06D84D6EEF55FC4B9FB75546D01692C9162F7A994A
HashLib 第二种方法的结果
var bytearray = 227,176,196,66,152,252,28,20,154,251,244,200,153,11,185,36,39,174,65,228,100,155,147,76,164,149,153,27,120,82,184,85
var stringtest = E3B0C442-98FC1C14-9AFBF4C8-996FB924-27AE41E4-649B934C-A495991B-7852B855
有人可以帮助我吗?我不明白是什么问题,为什么结果不同?。
我开始使用这个库,因为我无法使用 System.Security.Cryptography 进行 sha224、sha1
【问题讨论】:
-
为什么在这两种情况下转换为十六进制的方式不同?请注意,想将方法命名为
ByteArrayToString的人应该用线索棒击打头部 - 可能会反复击打。是的,可能取自 StackOverflow,我知道 - 更有理由这样做。
标签: asp.net encryption cryptography