Search code examples
c#hashsha

How do I specify the key for computing a SHA256 hash?


I have had a requirement handed to me to modify an existing application to compute the SHA256 hash of a message body (basically a large string) using a specific key (the key to be provided in the message header).

However in none of the .Net SHA256 classes (eg System.Security.Cryptography.SHA256Managed) can I find a reference to a Key property or anything like that.

Does anyone know how to do this in .Net? (This ideally has to be .Net3.5, which the application targets.)

TIA


Solution

  • You've been asked to compute the Hash-based Message Authentication Code (HMAC) with SHA256. For this you will want to use the HMACSHA256 class - the documentation also includes an example of how to implement this.