HashAlgorithm哈希算法信息

本文介绍了加密哈希算法的基本概念,包括MD5、SHA1、SHA256等算法的实现与应用,探讨了不同算法的继承关系,并提供了创建各种哈希算法实例的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值