【问题标题】:Difference between C# cryptoSha256 and HashLib Sha256C# cryptoSha256 和 HashLib Sha256 的区别
【发布时间】:2019-03-08 19:19:57
【问题描述】:

早上好, 我需要以哈希模式加密文件。

我想使用这个库

HashLibrary

我使用本地 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


【解决方案1】:

您已经从流中读取了所有内容,因此您处于流的末尾。重新创建它以执行测试。第二个字符串是众所周知的空数组散列(又名 nuthin')。

有时很容易对照众所周知的工具(例如 sha256sum)检查您的输出。例如,可以像这样测试空数组,给定一个普通的 *nix shell(例如 Cygwin 或我想的 Linux 的 Windows 子系统):

$ dd count=0 status=none | sha256sum -b | awk '{print $1}'
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

【讨论】:

  • 最好,这个解决方案对我来说是成功的,使用 FileStream fs = new FileStream,我在两者中都进行了测试,结果是一样的。非常感谢你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-17
  • 2020-07-24
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多