hdev
hdev

Reputation: 6537

Difference between HMACSHA512 constructor and factory

Why does this returns a hash size of 512 bit ...

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));

... and this a hash size of 160 bit?

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

The constructor and the factory are both related to HMACSHA512, so I assumend the same output.

Upvotes: 2

Views: 206

Answers (2)

bartonjs
bartonjs

Reputation: 33128

There is no HMACSHA512.Create(). You're actually calling HMAC.Create() (because the language allows writing calls to static methods off of derived types)

So you're just getting "an HMAC", which seems to be HMACSHA1.

Upvotes: 1

P. Roe
P. Roe

Reputation: 2117

It looks to me like the Create factory method is not doing HMACSHA512 when used in this way.

The documentation breaks it down for us.

Return Value Type: System.Security.Cryptography.HMAC A new SHA-1 instance, unless the default settings have been changed by using the element.

So it looks like the reason they are different in size is because the Create Method is returning a SHA-1 instance instead of the HMACSHA512 instance as you expected.

Upvotes: 0

Related Questions