简体   繁体   English

.NET SHA256与Object.GetHashCode()

[英].NET SHA256 Vs Object.GetHashCode()

What are the pros/cons when it comes to using SHA256 Vs Object.GetHashCode()? 使用SHA256和Object.GetHashCode()有什么优缺点?

In the code below, the output is identical for both methods, however the GetHashCode() seems a lot simpler, requires fewer objects/code. 在下面的代码中,这两种方法的输出相同,但是GetHashCode()看起来简单得多,所需的对象/代码更少。

class Program
    {
        static void Main(string[] args)
        {
            TestGetHashCode();
            TestSha256();
            Console.Read();
        }

        static void TestSha256()
        {
            Console.WriteLine("Testing SHA256");
            UnicodeEncoding byteConverter = new UnicodeEncoding();
            SHA256 sha256 = SHA256.Create();
            string data = "A paragraph of text";
            byte[] hashA = sha256.ComputeHash(byteConverter.GetBytes(data));
            data = "A paragraph of changed text";
            byte[] hashB = sha256.ComputeHash(byteConverter.GetBytes(data));
            data = "A paragraph of text";
            byte[] hashC = sha256.ComputeHash(byteConverter.GetBytes(data));
            Console.WriteLine(hashA.SequenceEqual(hashB)); // Displays: false
            Console.WriteLine(hashA.SequenceEqual(hashC)); // Displays: true
        }

        static void TestGetHashCode()
        {
            Console.WriteLine("Testing Object.GetHashCode()");
            string data = "A paragraph of text";
            int hashA = data.GetHashCode();
            data = "A paragraph of changed text";
            int hashB = data.GetHashCode();
            data = "A paragraph of text";
            int hashC = data.GetHashCode();
            Console.WriteLine(hashA.Equals(hashB)); // Displays: false
            Console.WriteLine(hashA.Equals(hashC)); // Displays: true
        }
    }

You cannot compare the two as they are built for two entirely different purposes. 您不能将两者进行比较,因为它们是为两个完全不同的目的而构建的。 What is your goal? 你的目标是什么? - Encryption or simple object lookup? -加密还是简单的对象查找?

For object lookup (in a hashtable): GetHashCode() 对于对象查找(在哈希表中): GetHashCode()

For encryption: SHA256 combined with eg AES. 对于加密:SHA256与AES结合使用。

GetHashCode() should be overridden for your type and ideally only use immutable fields, ie. GetHashCode()应该为您的类型覆盖,并且理想情况下仅使用不可变字段,即。 fields not changing over the lifetime of the object, read-only fields is a good example of this ;-) 字段在对象的整个生命周期中都没有变化,只读字段是一个很好的例子;-)

SHA256 is used for example to hash a password for usage in an encryption algorithm that takes 256-bit keys. SHA256例如用于对密码进行哈希处理,以用于采用256位密钥的加密算法。 The point of a hashing algorithm used for encryption is that it must be slow (the opposite of the object lookup scenario) to make it more difficult to bruteforce attack passwords. 用于加密的哈希算法的要点是,它必须很慢(与对象查找方案相反),以使蛮力攻击密码更加困难。

So no pros/cons as such, but really depends on your goal. 因此,没有任何利弊,但实际上取决于您的目标。 Use the right tool for the purpose :-) 为此使用正确的工具:-)

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

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