Hash Digest / Array Comparison in C#

Posted by Erik Karulf on Stack Overflow See other posts from Stack Overflow or by Erik Karulf
Published on 2010-12-31T18:28:50Z Indexed on 2010/12/31 18:54 UTC
Read the original article Hit count: 282

Filed under:
|
|
|

Hi All,

I'm writing an application that needs to verify HMAC-SHA256 checksums. The code I currently have looks something like this:

    static bool VerifyIntegrity(string secret, string checksum, string data)
    {
        // Verify HMAC-SHA256 Checksum
        byte[] key = System.Text.Encoding.UTF8.GetBytes(secret);
        byte[] value = System.Text.Encoding.UTF8.GetBytes(data);
        byte[] checksum_bytes = System.Text.Encoding.UTF8.GetBytes(checksum);
        using (var hmac = new HMACSHA256(key))
        {
            byte[] expected_bytes = hmac.ComputeHash(value);
            return checksum_bytes.SequenceEqual(expected_bytes);
        }
    }

I know that this is susceptible to timing attacks.

Is there a message digest comparison function in the standard library? I realize I could write my own time hardened comparison method, but I have to believe that this is already implemented elsewhere.

© Stack Overflow or respective owner

Related posts about c#

Related posts about security