Sign Data in .NET using HMACSHA1 (HMAC Standard)
HMACSHA1 library in .Net Framework is used to digitally sign user data to assure that it is not tampered. For instance data can be hashed before storing it in database (or some other storage). So hash is also stored in database. When the data is fetched it is hashed again and the resulted hash is compared to the previously saved hash.
Data can be signed with a key (byte array) like so:
private byte[] SignWithHMAC(byte[] dataToSign, byte[] keyBody) { HMACSHA1 hmac = new HMACSHA1(keyBody); CryptoStream cs = new CryptoStream(Stream.Null, hmac, CryptoStreamMode.Write); cs.Write(dataToSign, 0, dataToSign.Length); cs.Close(); byte[] hashResult = hmac.Hash; return hashResult; }
HMACSHA1 implements HMAC algorithm standard which stands for hash message authentication code.
Sunday, December 31, 2006 2:02 AM