Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么SHA256的所有变体都显示为SHA256Managed?_C#_Hash_Md5_Checksum_Sha256 - Fatal编程技术网

C# 为什么SHA256的所有变体都显示为SHA256Managed?

C# 为什么SHA256的所有变体都显示为SHA256Managed?,c#,hash,md5,checksum,sha256,C#,Hash,Md5,Checksum,Sha256,我正在编写一个扩展方法,通过删除大量的样板文件来简化散列的创建,但是我的问题是,每当我一步一步地浏览代码时,我都能看到它总是挑选SHA256Managed,而不管我是否调用SHA256.Create(),SHA256Cng.Create(),SHA256Managed.Create()或SHA256CryptoServiceProvider.Create() 当我选择不同的散列算法(如MD5)时,情况也是如此,但在MD5的情况下,它总是选择MD5CryptoServiceProvider,而不管

我正在编写一个扩展方法,通过删除大量的样板文件来简化散列的创建,但是我的问题是,每当我一步一步地浏览代码时,我都能看到它总是挑选
SHA256Managed
,而不管我是否调用
SHA256.Create()
SHA256Cng.Create()
SHA256Managed.Create()
SHA256CryptoServiceProvider.Create()

当我选择不同的散列算法(如MD5)时,情况也是如此,但在MD5的情况下,它总是选择
MD5CryptoServiceProvider
,而不管我实际使用的类是什么

为什么呢

这是我的密码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Utility.Methods
{
    public enum HashType { MD5, SHA512, SHA256, SHA384, SHA1 }
    public enum HashSubType {Normal, Cng, Managed, CryptoServiceProvider}

    public static class TextHasher
    {
        public static string Hash(this string input, HashType hash, HashSubType subType = HashSubType.Normal)
        {
            Func<HashAlgorithm, string> hashFunction = alg => HashingHelper(input, alg);

            switch (subType)
            {
                case HashSubType.Normal:
                    return hashFunction(NormalHashes(hash));
                case HashSubType.Cng:
                    return hashFunction(CngHashes(hash));
                case HashSubType.Managed:
                    return hashFunction(ManagedHashes(hash));
                case HashSubType.CryptoServiceProvider:
                    return hashFunction(CSPHashes(hash));
                default: return "error"; // unreachable
            }
        }

        private static string HashingHelper(string text, HashAlgorithm algorithm)
        {
            Func<string, byte[]> getHash = input => algorithm.ComputeHash(Encoding.UTF8.GetBytes(input));

            var sb = new StringBuilder();
            Array.ForEach(getHash(text), b => sb.Append(b.ToString("X")));

            return sb.ToString();
        }

        private static HashAlgorithm NormalHashes(HashType hash)
        {
            switch (hash)
            {
                case HashType.MD5:
                    return MD5.Create();
                case HashType.SHA1:
                    return SHA1.Create();
                case HashType.SHA256:
                    return SHA256.Create();
                case HashType.SHA384:
                    return SHA384.Create();
                case HashType.SHA512:
                    return SHA512.Create();
                default: return null; // unreachable
            }
        }

        private static HashAlgorithm CngHashes(HashType hash)
        {
            switch (hash)
            {
                case HashType.MD5:
                    return MD5Cng.Create();
                case HashType.SHA1:
                    return SHA1Cng.Create();
                case HashType.SHA256:
                    return SHA256Cng.Create();
                case HashType.SHA384:
                    return SHA384Cng.Create();
                case HashType.SHA512:
                    return SHA512Cng.Create();
                default: return null; // unreachable
            }
        }

        private static HashAlgorithm ManagedHashes(HashType hash)
        {
            switch (hash)
            {
                case HashType.SHA1:
                    return SHA1Managed.Create();
                case HashType.SHA256:
                    return SHA256Managed.Create();
                case HashType.SHA384:
                    return SHA384Managed.Create();
                case HashType.SHA512:
                    return SHA512Managed.Create();
                default: return null; // unreachable
            }
        }

        private static HashAlgorithm CSPHashes(HashType hash)
        {
            switch (hash)
            {
                case HashType.MD5:
                    return MD5CryptoServiceProvider.Create();
                case HashType.SHA1:
                    return SHA1CryptoServiceProvider.Create();
                case HashType.SHA256:
                    return SHA256CryptoServiceProvider.Create();
                case HashType.SHA384:
                    return SHA384CryptoServiceProvider.Create();
                case HashType.SHA512:
                    return SHA512CryptoServiceProvider.Create();
                default: return null; // unreachable
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Security.Cryptography;
使用系统文本;
使用System.Threading.Tasks;
命名空间实用程序.Methods
{
公共枚举哈希类型{MD5,SHA512,SHA256,SHA384,SHA1}
公共枚举哈希子类型{Normal,Cng,Managed,CryptoServiceProvider}
公共静态类TextHasher
{
公共静态字符串哈希(此字符串输入,HashType哈希,HashSubType subType=HashSubType.Normal)
{
Func hashFunction=alg=>hashinghelp(输入,alg);
开关(子类型)
{
案例哈希子类型。正常:
返回hashFunction(normalhash(hash));
案例HashSubType.Cng:
返回hashFunction(cnghash(hash));
案例哈希子类型。托管:
返回hashFunction(ManagedHashes(hash));
case HashSubType.CryptoServiceProvider:
返回hashFunction(CSPHashes(hash));
默认值:返回“error”;//不可访问
}
}
私有静态字符串哈希帮助器(字符串文本,哈希算法)
{
Func getHash=input=>algorithm.ComputeHash(Encoding.UTF8.GetBytes(input));
var sb=新的StringBuilder();
ForEach(getHash(text),b=>sb.Append(b.ToString(“X”)));
使某人返回字符串();
}
私有静态哈希算法normalhash(HashType哈希)
{
开关(散列)
{
case HashType.MD5:
返回MD5.Create();
case HashType.SHA1:
返回SHA1.Create();
case HashType.SHA256:
返回SHA256.Create();
case HashType.SHA384:
返回SHA384.Create();
case HashType.SHA512:
返回SHA512.Create();
默认值:返回null;//不可访问
}
}
私有静态哈希算法CngHashes(哈希类型哈希)
{
开关(散列)
{
case HashType.MD5:
返回MD5Cng.Create();
case HashType.SHA1:
返回SHA1Cng.Create();
case HashType.SHA256:
返回SHA256Cng.Create();
case HashType.SHA384:
返回SHA384Cng.Create();
case HashType.SHA512:
返回SHA512Cng.Create();
默认值:返回null;//不可访问
}
}
私有静态哈希算法ManagedHash(哈希类型哈希)
{
开关(散列)
{
case HashType.SHA1:
返回SHA1Managed.Create();
case HashType.SHA256:
返回SHA256Managed.Create();
case HashType.SHA384:
返回SHA384Managed.Create();
case HashType.SHA512:
返回SHA512Managed.Create();
默认值:返回null;//不可访问
}
}
私有静态哈希算法CSPHashes(哈希类型哈希)
{
开关(散列)
{
case HashType.MD5:
返回MD5CryptoServiceProvider.Create();
case HashType.SHA1:
返回SHA1CryptoServiceProvider.Create();
case HashType.SHA256:
返回SHA256CryptoServiceProvider.Create();
case HashType.SHA384:
返回SHA384CryptoServiceProvider.Create();
case HashType.SHA512:
返回SHA512CryptoServiceProvider.Create();
默认值:返回null;//不可访问
}
}
}
}

有什么帮助吗?

这是因为您总是调用相同的静态方法。SHA256是一个抽象类,其子类不提供替代方法。事实上,Resharper将向您发出警告,说明您正在从派生类型访问静态成员

实际上,调用SHA256.Create与调用相同。这两个类在内部调用相同的实现,并将结果简单地强制转换为不同的类型

该方法将创建machine.config中指定的默认实现,并且可以在app.config中重写

如果要使用特定的提供程序,请使用传递要使用的提供程序的名称

例如:

SHA256.Create("System.Security.Cryptography.SHA256Cng");
HashAlgorithm.Create("System.Security.Cryptography.SHA256Cng");
SHA256.Create("System.Security.Cryptography.SHA256CryptoServiceProvider");
编辑

的文档指定了有效算法名称的列表。MSDN文章介绍了如何将算法名称映射到其他提供商(您自己的、第三方的、硬件加速的或其他任何提供商)并使用它们代替默认算法

编辑2

也可以通过编程方式更改映射。因此,要将“Dog”映射到SHA512CryptoServiceProvider,只需编写:

CryptoConfig.AddAlgorithm(
             typeof(System.Security.Cryptography.SHA512CryptoServiceProvider),
             "Dog");
var t4 = HashAlgorithm.Create("Dog");

这并不特别奇怪(但我认为它应该会生成警告),因为
Create
是在
SHA256
类上静态定义的,而其他类继承自该类,因此