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



为什么这会返回哈希大小为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有关,因此我假设相同的输出。

没有HMACSHA512.Create()。您实际上是在调用HMAC.Create()(因为该语言允许将呼叫写入派生类型的静态方法)

所以您只是得到" hmac",似乎是hmacsha1。

在我看来,创建工厂方法在以这种方式使用时不做HMACSHA512。

文档为我们分解了。

返回值类型:system.security.cryptography.hmac a新SHA-1 实例,除非通过使用默认设置更改了默认设置 元素。

因此,看起来它们大小不同的原因是因为创建方法是返回SHA-1实例而不是HMACSHA512实例。

最新更新