I've this piece of .NET code that is based on a Microsoft sample on how to generate a key and sign using HMACSH256.
But I've altered the key generation a little bit and also decided to use Base64 to transport the key as a string:
Key generation:
byte[] secretkey = new Byte[64];
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(secretkey);
using (SHA512 mySHA256 = SHA512.Create())
{
var hashedKey = mySHA256.ComputeHash(secretkey);
var encodedKey = Convert.ToBase64String(hashedKey);
return encodedKey;
}
}
So, besides generating the random bytes I also decided to hash the key with SHA512. And then Base64 encode to get the final string representation, which will be used only in transport, since before usage the receiver of the message will decode back to the SHA512 digest.
Any issues with the way I'm doing it? Unless I'm mistaken all the hashing will do is introduce more randomness, since the key will remain at the 64 byte length. Is it worth it?
Also, any issues with transporting the key as a Base64 string?
Thanks, and I hope someone can offer some feedback on this. Have a good one!