[英]Is there any difference between SHA256.Create() and HashAlgorithm.Create("SHA-256")?
Do these two code blocks return the same thing?这两个代码块返回相同的东西吗? Assume
arr
is the same byte[]
in both examples:假设
arr
在两个示例中是相同的byte[]
:
Code sample 1代码示例 1
HashAlgorithm a = HashAlgorithm.Create("SHA-256");
var result = a.ComputeHash(arr);
Code sample 2代码示例 2
SHA256 b = SHA256.Create();
var result = b.ComputeHash(arr);
UPDATE : I got the sample project of creating AWS signature code in C# (which is written in .Net 4.5) and am trying to use its classes in a dotnetcode5 project and just because HashAlgorithm.Create()
is not available in dotnetcode5 yet, I have decided to use the second approach instead of the first one.更新:我得到了在 C# 中创建 AWS 签名代码的示例项目(用 .Net 4.5 编写),并试图在 dotnetcode5 项目中使用它的类,只是因为
HashAlgorithm.Create()
在 dotnetcode5 中尚不可用,我决定使用第二种方法而不是第一种方法。 The problem is that the second example returns a canonical result witch is not valid in AWS.问题是第二个示例返回的规范结果在 AWS 中无效。
SHA256.Create()
does this internally: SHA256.Create()
在内部执行此操作:
return (HashAlgorithm) CryptoConfig.CreateFromName("System.Security.Cryptography.SHA256");
HashAlgorithm.Create("SHA-256")
will result in this: HashAlgorithm.Create("SHA-256")
将导致:
return (SHA256) CryptoConfig.CreateFromName("SHA-256");
Both of these calls will result in the creation of an instance of SHA256Managed
.这两个调用都将导致创建
SHA256Managed
实例。
See https://msdn.microsoft.com/en-us/library/system.security.cryptography.cryptoconfig(v=vs.110).aspx#Anchor_5请参阅https://msdn.microsoft.com/en-us/library/system.security.cryptography.cryptoconfig(v=vs.110).aspx#Anchor_5
So there is no difference between these two approaches.所以这两种方法没有区别。
I think the main question that OP is missing is how to compare the two bytes array.我认为 OP 缺少的主要问题是如何比较两个字节数组。
If you do something like:如果您执行以下操作:
static void Main(string[] args)
{
byte[] arr = Encoding.ASCII.GetBytes("sample");
HashAlgorithm a = HashAlgorithm.Create("SHA-256");
var resulthash = a.ComputeHash(arr);
SHA256 b = SHA256.Create();
var resultsha = b.ComputeHash(arr);
Console.WriteLine(StructuralComparisons.StructuralEqualityComparer.Equals(resulthash, resultsha ));
}
you will get correct response.你会得到正确的回应。
Note you can't do something like resulthash==resultsha
that will return false.请注意,您不能执行诸如
resulthash==resultsha
会返回 false 的操作。
Both will result the same because the do call the same method internally两者都会产生相同的结果,因为它们在内部调用了相同的方法
new static public SHA256 Create() {
return Create("System.Security.Cryptography.SHA256");
}
new static public SHA256 Create(String hashName) {
return (SHA256) CryptoConfig.CreateFromName(hashName);
}
static public HashAlgorithm Create(String hashName) {
return (HashAlgorithm) CryptoConfig.CreateFromName(hashName);
}
the difference is just the return type ( SHA256
is derived from HashAlgorithm
)区别仅在于返回类型(
SHA256
派生自HashAlgorithm
)
Reference for SHA256 , Reference for HashAlgorithm SHA256 参考,HashAlgorithm 参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.