Search code examples
c#salt-cryptographyhmacsha1

Check HMAC-SHA1 without key in C#


I am generating HMAC-SHA1 without key in C# and it returns every time different hash for same value, how can I match hashes,

My code is https://dotnetfiddle.net/3a3tiP

  • is it possible or not to match these hashes?
  • I think HMAC-SHA1 not possible without key Am I right?
  • If above 'Yes' then why C# allow to generate without key and how is it doing?

Solution

  • Yes, because you are using parameterless constructor to build HMACSHA1 instance, and MSDN says

    HMACSHA1() - Initializes a new instance of the HMACSHA1 class with a randomly generated key.

    Just add some constant key and you'll get same hash every time. e.g.

    var hmacSha = new HMACSHA1(Encoding.UTF8.GetBytes("yourConstantKey"));
    

    And answering your questions:

    1. Yes, use same key for 2 generations.
    2. Yes.
    3. It generates random key for you