简体   繁体   中英

What is the difference between .NET's HMAC and HMAC KeyedHashAlgorithm?

Security.Cryptography.HMACSHA256.Create()Security.Cryptography.KeyedHashAlgorithm.Create("HmacSHA256")什么Security.Cryptography.KeyedHashAlgorithm.Create("HmacSHA256")

First, about Security.Cryptography.HMACSHA256.Create() --

Create method is the method of HMAC class, from which HMACSHA256 is derived. In short:

public class HMACSHA256 : HMAC {
...
}

where HMAC is defined as:

public abstract class HMAC : KeyedHashAlgorithm {
    new static public HMAC Create () {
        return Create("System.Security.Cryptography.HMAC");
    }

    new static public HMAC Create (string algorithmName) {
        return (HMAC) CryptoConfig.CreateFromName(algorithmName);
    }
    ...
}

Second, about Security.Cryptography.KeyedHashAlgorithm.Create("HmacSHA256")

public abstract class KeyedHashAlgorithm : HashAlgorithm { 
    new static public KeyedHashAlgorithm Create(String algName) {
        return (KeyedHashAlgorithm) CryptoConfig.CreateFromName(algName);    
    }
    ...
}

As you can see, both calls result in calling CryptoConfig.CreateFromName method, but with different parameter values, ie, System.Security.Cryptography.HMAC in first case, and HmacSHA256 in second case. Internally, there are some tables and reflection logic inside CryptoConfig.CreateFromName method.

The result of first call is SHA1 hash, and the result of second call is SHA256 .

Nothing. Security.Cryptography.KeyedHashAlgorithm.Create("HmacSHA256") ( reference source ) uses reflection to lookup Security.Cryptography.HMACSHA256 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM