Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

hash

sha

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

like image 446
haughtonomous Avatar asked Mar 14 '23 09:03

haughtonomous


2 Answers

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.

like image 198
Ben Strawson Avatar answered Mar 27 '23 19:03

Ben Strawson


There is no key for SHA256. It is hash function and returns the same value each time for the same input. This is not encryption with key.

like image 42
i486 Avatar answered Mar 27 '23 21:03

i486