HashAlgorithm
表示所有加密哈希算法实现均必须从中派生的基类
继承层次结构
System.Object
System.Security.Cryptography.HashAlgorithm
System.Security.Cryptography.KeyedHashAlgorithm
System.Security.Cryptography.MD5
System.Security.Cryptography.RIPEMD160
System.Security.Cryptography.SHA1
System.Security.Cryptography.SHA256
System.Security.Cryptography.SHA384
System.Security.Cryptography.SHA512
[ComVisibleAttribute(true)] public abstract class HashAlgorithm : IDisposable, ICryptoTransform
示例代码
static void Main() { HashAlgorithm md5 = MD5.Create(); Console.WriteLine("\n MD5.Create():"); Console.WriteLine("GetTypeName = " + md5.GetType().Name); Console.WriteLine("ToString = " + md5.ToString()); HashAlgorithm md5Cng = new MD5Cng(); Console.WriteLine("\n new MD5Cng():"); Console.WriteLine("GetTypeName = " + md5Cng.GetType().Name); Console.WriteLine("ToString = " + md5Cng.ToString()); HashAlgorithm md5Csp = new MD5CryptoServiceProvider(); Console.WriteLine("\n new MD5CryptoServiceProvider():"); Console.WriteLine("GetTypeName = " + md5Csp.GetType().Name); Console.WriteLine("ToString = " + md5Csp.ToString()); HashAlgorithm sha1 = new SHA1Managed(); Console.WriteLine("\n new SHA1Managed():"); Console.WriteLine("GetTypeName = " + sha1.GetType().Name); Console.WriteLine("ToString = " + sha1.ToString()); HashAlgorithm sha1Cng = new SHA1Cng(); Console.WriteLine("\n new SHA1Cng():"); Console.WriteLine("GetTypeName = " + sha1Cng.GetType().Name); Console.WriteLine("ToString = " + sha1Cng.ToString()); HashAlgorithm sha1Csp = new SHA1CryptoServiceProvider(); Console.WriteLine("\n new SHA1CryptoServiceProvider():"); Console.WriteLine("GetTypeName = " + sha1Csp.GetType().Name); Console.WriteLine("ToString = " + sha1Csp.ToString()); HashAlgorithm sha256 = new SHA256Managed(); Console.WriteLine("\n new SHA256Managed():"); Console.WriteLine("GetTypeName = " + sha256.GetType().Name); Console.WriteLine("ToString = " + sha256.ToString()); HashAlgorithm sha384 = new SHA384Cng(); Console.WriteLine("\n new SHA384Cng():"); Console.WriteLine("GetTypeName = " + sha384.GetType().Name); Console.WriteLine("ToString = " + sha384.ToString()); HashAlgorithm sha512 = new SHA512CryptoServiceProvider(); Console.WriteLine("\n new SHA512CryptoServiceProvider():"); Console.WriteLine("GetTypeName = " + sha512.GetType().Name); Console.WriteLine("ToString = " + sha512.ToString()); Console.ReadLine(); }
运行结果:
MD5.Create(): GetTypeName = MD5CryptoServiceProvider ToString = System.Security.Cryptography.MD5CryptoServiceProvider new MD5Cng(): GetTypeName = MD5Cng ToString = System.Security.Cryptography.MD5Cng new MD5CryptoServiceProvider(): GetTypeName = MD5CryptoServiceProvider ToString = System.Security.Cryptography.MD5CryptoServiceProvider new SHA1Managed(): GetTypeName = SHA1Managed ToString = System.Security.Cryptography.SHA1Managed new SHA1Cng(): GetTypeName = SHA1Cng ToString = System.Security.Cryptography.SHA1Cng new SHA1CryptoServiceProvider(): GetTypeName = SHA1CryptoServiceProvider ToString = System.Security.Cryptography.SHA1CryptoServiceProvider new SHA256Managed(): GetTypeName = SHA256Managed ToString = System.Security.Cryptography.SHA256Managed new SHA384Cng(): GetTypeName = SHA384Cng ToString = System.Security.Cryptography.SHA384Cng new SHA512CryptoServiceProvider(): GetTypeName = SHA512CryptoServiceProvider ToString = System.Security.Cryptography.SHA512CryptoServiceProvider
运行时信息:
md5 {System.Security.Cryptography.MD5CryptoServiceProvider} [System.Security.Cryptography.MD5CryptoServiceProvider]: {System.Security.Cryptography.MD5CryptoServiceProvider} CanReuseTransform: true CanTransformMultipleBlocks: true Hash: “md5.Hash”引发了“System.NullReferenceException”类型的异常 HashSize: 128 InputBlockSize: 1 OutputBlockSize: 1 md5Cng {System.Security.Cryptography.MD5Cng} [System.Security.Cryptography.MD5Cng]: {System.Security.Cryptography.MD5Cng} CanReuseTransform: true CanTransformMultipleBlocks: true Hash: “md5Cng.Hash”引发了“System.NullReferenceException”类型的异常 HashSize: 128 InputBlockSize: 1 OutputBlockSize: 1 md5Csp {System.Security.Cryptography.MD5CryptoServiceProvider} [System.Security.Cryptography.MD5CryptoServiceProvider]: {System.Security.Cryptography.MD5CryptoServiceProvider} CanReuseTransform: true CanTransformMultipleBlocks: true Hash: “md5Csp.Hash”引发了“System.NullReferenceException”类型的异常 HashSize: 128 InputBlockSize: 1 OutputBlockSize: 1 sha1 {System.Security.Cryptography.SHA1Managed} [System.Security.Cryptography.SHA1Managed]: {System.Security.Cryptography.SHA1Managed} CanReuseTransform: true CanTransformMultipleBlocks: true Hash: “sha1.Hash”引发了“System.NullReferenceException”类型的异常 HashSize: 160 InputBlockSize: 1 OutputBlockSize: 1 sha1Cng {System.Security.Cryptography.SHA1Cng} [System.Security.Cryptography.SHA1Cng]: {System.Security.Cryptography.SHA1Cng} CanReuseTransform: true CanTransformMultipleBlocks: true Hash: “sha1Cng.Hash”引发了“System.NullReferenceException”类型的异常 HashSize: 160 InputBlockSize: 1 OutputBlockSize: 1 sha1Csp {System.Security.Cryptography.SHA1CryptoServiceProvider} [System.Security.Cryptography.SHA1CryptoServiceProvider]: {System.Security.Cryptography.SHA1CryptoServiceProvider} CanReuseTransform: true CanTransformMultipleBlocks: true Hash: “sha1Csp.Hash”引发了“System.NullReferenceException”类型的异常 HashSize: 160 InputBlockSize: 1 OutputBlockSize: 1 sha256 {System.Security.Cryptography.SHA256Managed} [System.Security.Cryptography.SHA256Managed]: {System.Security.Cryptography.SHA256Managed} CanReuseTransform: true CanTransformMultipleBlocks: true Hash: “sha256.Hash”引发了“System.NullReferenceException”类型的异常 HashSize: 256 InputBlockSize: 1 OutputBlockSize: 1 sha384 {System.Security.Cryptography.SHA384Cng} [System.Security.Cryptography.SHA384Cng]: {System.Security.Cryptography.SHA384Cng} CanReuseTransform: true CanTransformMultipleBlocks: true Hash: “sha384.Hash”引发了“System.NullReferenceException”类型的异常 HashSize: 384 InputBlockSize: 1 OutputBlockSize: 1 sha512 {System.Security.Cryptography.SHA512CryptoServiceProvider} [System.Security.Cryptography.SHA512CryptoServiceProvider]: {System.Security.Cryptography.SHA512CryptoServiceProvider} CanReuseTransform: true CanTransformMultipleBlocks: true Hash: “sha512.Hash”引发了“System.NullReferenceException”类型的异常 HashSize: 512 InputBlockSize: 1 OutputBlockSize: 1
MD5
表示的抽象类的所有实现均从中 MD5 哈希算法继承
继承层次结构
System.Object
System.Security.Cryptography.HashAlgorithm
System.Security.Cryptography.MD5
System.Security.Cryptography.MD5Cng
System.Security.Cryptography.MD5CryptoServiceProvider
[ComVisibleAttribute(true)] public abstract class MD5 : HashAlgorithm
MD5.Create()
创建一个实例的默认实现 MD5 哈希算法
MD5Cng
提供 CNG (下一代加密技术) 实现的 MD5 (消息摘要 5) 128 位哈希算法
继承层次结构
System.Object
System.Security.Cryptography.HashAlgorithm
System.Security.Cryptography.MD5
System.Security.Cryptography.MD5Cng
[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)] public sealed class MD5Cng : MD5
MD5CryptoServiceProvider
计算 MD5 哈希值的输入数据使用加密服务提供商 (CSP) 提供的实现。 此类不能被继承
继承层次结构
System.Object
System.Security.Cryptography.HashAlgorithm
System.Security.Cryptography.MD5
System.Security.Cryptography.MD5CryptoServiceProvider
[ComVisibleAttribute(true)] public sealed class MD5CryptoServiceProvider : MD5
SHA1
计算 SHA1 输入数据的哈希值
继承层次结构
System.Object
System.Security.Cryptography.HashAlgorithm
System.Security.Cryptography.SHA1
System.Security.Cryptography.SHA1Cng
System.Security.Cryptography.SHA1CryptoServiceProvider
System.Security.Cryptography.SHA1Managed
[ComVisibleAttribute(true)] public abstract class SHA1 : HashAlgorithm
SHA1Cng
提供的下一代加密技术 (CNG) 实现的安全哈希算法 (SHA)
继承层次结构
System.Object
System.Security.Cryptography.HashAlgorithm
System.Security.Cryptography.SHA1
System.Security.Cryptography.SHA1Cng
[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)] public sealed class SHA1Cng : SHA1
SHA1CryptoServiceProvider
计算 SHA1 哈希值的输入数据使用加密服务提供商 (CSP) 提供的实现。 此类不能被继承
继承层次结构
System.Object
System.Security.Cryptography.HashAlgorithm
System.Security.Cryptography.SHA1
System.Security.Cryptography.SHA1CryptoServiceProvider
[ComVisibleAttribute(true)] public sealed class SHA1CryptoServiceProvider : SHA1
SHA1Managed
计算 SHA1 使用托管的库的输入数据的哈希值
继承层次结构
System.Object
System.Security.Cryptography.HashAlgorithm
System.Security.Cryptography.SHA1
System.Security.Cryptography.SHA1Managed
[ComVisibleAttribute(true)] public class SHA1Managed : SHA1