HMACSHA512构造函数和工厂之间的区别

时间:2016-12-23 15:01:28

标签: c# cryptography

为什么这会返回512位的散列大小...

var text = "Hello World";
var buffer = Encoding.UTF8.GetBytes(text);

var hmac = new System.Security.Cryptography.HMACSHA512();
hmac.Key = GetRandomBits(512);
hmac.ComputeHash(buffer);

Assert.That(hmac.HashSize, Is.EqualTo(512));

...这个哈希大小为160位?

var text = "Hello World";
var buffer = Encoding.UTF8.GetBytes(text);

var hmac = System.Security.Cryptography.HMACSHA512.Create();
hmac.Key = GetRandomBits(512);
hmac.ComputeHash(buffer);

Assert.That(hmac.HashSize, Is.EqualTo(512)); // failure

构造函数和工厂都与HMACSHA512相关,所以我赞同相同的输出。

2 个答案:

答案 0 :(得分:1)

没有HMACSHA512.Create()。您实际上正在调用HMAC.Create()(因为该语言允许从派生类型中调用静态方法)

所以你刚刚获得HMAC",这似乎是HMACSHA1。

答案 1 :(得分:0)

在我看来,当使用这种方式时,Create factory方法不会执行HMACSHA512。

documentation为我们打破了它。

  

返回值类型:System.Security.Cryptography.HMAC一个新的SHA-1   实例,除非使用了更改默认设置    元件。

所以看起来它们大小不同的原因是因为Create Method正在按预期返回SHA-1实例而不是HMACSHA512实例。

相关问题