cha*_*aru 3 .net c# hash f# cryptography
Security.Cryptography.HMACSHA256.Create()和之间有什么区别Security.Cryptography.KeyedHashAlgorithm.Create("HmacSHA256")?
首先,关于Security.Cryptography.HMACSHA256.Create()-
Createmethod是HMAC类的方法,从中HMACSHA256派生出来.简而言之:
public class HMACSHA256 : HMAC {
...
}
Run Code Online (Sandbox Code Playgroud)
其中HMAC定义为:
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);
}
...
}
Run Code Online (Sandbox Code Playgroud)
第二,关于 Security.Cryptography.KeyedHashAlgorithm.Create("HmacSHA256")
public abstract class KeyedHashAlgorithm : HashAlgorithm {
new static public KeyedHashAlgorithm Create(String algName) {
return (KeyedHashAlgorithm) CryptoConfig.CreateFromName(algName);
}
...
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,两个调用都会导致调用CryptoConfig.CreateFromName方法,但是使用不同的参数值,即System.Security.Cryptography.HMAC在第一种情况下,HmacSHA256在第二种情况下.在内部,方法中有一些表和反射逻辑CryptoConfig.CreateFromName.
第一次调用的结果是SHA1hash,第二次调用的结果是SHA256.