RIPEMD160 类

定义

表示 MD160 哈希算法的所有实现均从中继承的抽象类。

public ref class RIPEMD160 abstract : System::Security::Cryptography::HashAlgorithm
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class RIPEMD160 : System.Security.Cryptography.HashAlgorithm
[<System.Runtime.InteropServices.ComVisible(true)>]
type RIPEMD160 = class
    inherit HashAlgorithm
Public MustInherit Class RIPEMD160
Inherits HashAlgorithm
继承
RIPEMD160
派生
属性

示例

下面的代码示例计算 RIPEMD160 目录中所有文件的哈希。

using namespace System;
using namespace System::IO;
using namespace System::Security::Cryptography;

// Print the byte array in a readable format.
void PrintByteArray( array<Byte>^array )
{
   int i;
   for ( i = 0; i < array->Length; i++ )
   {
      Console::Write( String::Format( "{0:X2}", array[ i ] ) );
      if ( (i % 4) == 3 )
            Console::Write( " " );

   }
   Console::WriteLine();
}

int main()
{
   array<String^>^args = Environment::GetCommandLineArgs();
   if ( args->Length < 2 )
   {
      Console::WriteLine( "Usage: hashdir <directory>" );
      return 0;
   }

   try
   {
      
      // Create a DirectoryInfo object representing the specified directory.
      DirectoryInfo^ dir = gcnew DirectoryInfo( args[ 1 ] );
      
      // Get the FileInfo objects for every file in the directory.
      array<FileInfo^>^files = dir->GetFiles();
      
      // Initialize a RIPE160 hash object.
      RIPEMD160 ^ myRIPEMD160 = RIPEMD160Managed::Create();
      array<Byte>^hashValue;
      
      // Compute and print the hash values for each file in directory.
      System::Collections::IEnumerator^ myEnum = files->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         FileInfo^ fInfo = safe_cast<FileInfo^>(myEnum->Current);
         
         // Create a fileStream for the file.
         FileStream^ fileStream = fInfo->Open( FileMode::Open );
         
         // Compute the hash of the fileStream.
         hashValue = myRIPEMD160->ComputeHash( fileStream );
         
         // Write the name of the file to the Console.
         Console::Write( "{0}: ", fInfo->Name );
         
         // Write the hash value to the Console.
         PrintByteArray( hashValue );
         
         // Close the file.
         fileStream->Close();
      }
      return 0;
   }
   catch ( DirectoryNotFoundException^ ) 
   {
      Console::WriteLine( "Error: The directory specified could not be found." );
   }
   catch ( IOException^ ) 
   {
      Console::WriteLine( "Error: A file in the directory could not be accessed." );
   }

}
using System;
using System.IO;
using System.Security.Cryptography;
using System.Windows.Forms;

public class HashDirectory
{

    [STAThreadAttribute]
    public static void Main(String[] args)
    {
        string directory = "";
        if (args.Length < 1)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult dr = fbd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                directory = fbd.SelectedPath;
            }
            else
            {
                Console.WriteLine("No directory selected.");
                return;
            }
        }
        else
        {
            directory = args[0];
        }

        try
        {
            // Create a DirectoryInfo object representing the specified directory.
            DirectoryInfo dir = new DirectoryInfo(directory);
            // Get the FileInfo objects for every file in the directory.
            FileInfo[] files = dir.GetFiles();
            // Initialize a RIPE160 hash object.
            RIPEMD160 myRIPEMD160 = RIPEMD160Managed.Create();
            byte[] hashValue;
            // Compute and print the hash values for each file in directory.
            foreach (FileInfo fInfo in files)
            {
                // Create a fileStream for the file.
                FileStream fileStream = fInfo.Open(FileMode.Open);
                // Be sure it's positioned to the beginning of the stream.
                fileStream.Position = 0;
                // Compute the hash of the fileStream.
                hashValue = myRIPEMD160.ComputeHash(fileStream);
                // Write the name of the file to the Console.
                Console.Write(fInfo.Name + ": ");
                // Write the hash value to the Console.
                PrintByteArray(hashValue);
                // Close the file.
                fileStream.Close();
            }
            return;
        }
        catch (DirectoryNotFoundException)
        {
            Console.WriteLine("Error: The directory specified could not be found.");
        }
        catch (IOException)
        {
            Console.WriteLine("Error: A file in the directory could not be accessed.");
        }
    }
    // Print the byte array in a readable format.
    public static void PrintByteArray(byte[] array)
    {
        int i;
        for (i = 0; i < array.Length; i++)
        {
            Console.Write(String.Format("{0:X2}", array[i]));
            if ((i % 4) == 3) Console.Write(" ");
        }
        Console.WriteLine();
    }
}
Imports System.IO
Imports System.Security.Cryptography
Imports System.Windows.Forms

Public Class HashDirectory

    Public Shared Sub Main(ByVal args() As String)
        Dim directory As String
        If args.Length < 1 Then
            Dim fdb As New FolderBrowserDialog
            Dim dr As DialogResult = fdb.ShowDialog()
            If (dr = DialogResult.OK) Then
                directory = fdb.SelectedPath
            Else
                Console.WriteLine("No directory selected")
                Return
            End If
        Else
            directory = args(0)
        End If
        Try
            ' Create a DirectoryInfo object representing the specified directory.
            Dim dir As New DirectoryInfo(directory)
            ' Get the FileInfo objects for every file in the directory.
            Dim files As FileInfo() = dir.GetFiles()
            ' Initialize a RIPE160 hash object.
            Dim myRIPEMD160 As RIPEMD160 = RIPEMD160Managed.Create()
            Dim hashValue() As Byte
            ' Compute and print the hash values for each file in directory.
            Dim fInfo As FileInfo
            For Each fInfo In files
                ' Create a fileStream for the file.
                Dim fileStream As FileStream = fInfo.Open(FileMode.Open)
                ' Be sure it's positioned to the beginning of the stream.
                fileStream.Position = 0
                ' Compute the hash of the fileStream.
                hashValue = myRIPEMD160.ComputeHash(fileStream)
                ' Write the name of the file to the Console.
                Console.Write(fInfo.Name + ": ")
                ' Write the hash value to the Console.
                PrintByteArray(hashValue)
                ' Close the file.
                fileStream.Close()
            Next fInfo
            Return
        Catch DExc As DirectoryNotFoundException
            Console.WriteLine("Error: The directory specified could not be found.")
        Catch IOExc As IOException
            Console.WriteLine("Error: A file in the directory could not be accessed.")
        End Try

    End Sub

    ' Print the byte array in a readable format.
    Public Shared Sub PrintByteArray(ByVal array() As Byte)
        Dim i As Integer
        For i = 0 To array.Length - 1
            Console.Write(String.Format("{0:X2}", array(i)))
            If i Mod 4 = 3 Then
                Console.Write(" ")
            End If
        Next i
        Console.WriteLine()

    End Sub
End Class

注解

哈希函数将任意长度的二进制字符串映射到固定长度的小型二进制字符串。 加密哈希函数具有 属性,在计算上无法找到两个哈希为同一值的不同输入;也就是说,如果相应的数据也匹配,则两组数据的哈希应匹配。 对数据的少量更改会导致哈希中出现大量不可预知的更改。

RIPEMD-160 是一个 160 位加密哈希函数。 它旨在用作 128 位哈希函数 MD4、MD5 和 RIPEMD 的替代。 RIPEMD 是在1988-1992) 欧盟RIPE (RAC完整性基元评估项目框架下开发的。

注意

RIPEMD160 安全哈希算法 SHA-256 和 SHA-512 及其派生类已取代。 SHA256SHA512 提供比 RIPEMD160更好的安全性和性能。 仅用于 RIPEMD160 与旧应用程序和数据的兼容性。

构造函数

RIPEMD160()

初始化 RIPEMD160 类的新实例。

字段

HashSizeValue

表示计算所得的哈希代码的大小(以位为单位)。

(继承自 HashAlgorithm)
HashValue

表示计算所得的哈希代码的值。

(继承自 HashAlgorithm)
State

表示哈希计算的状态。

(继承自 HashAlgorithm)

属性

CanReuseTransform

获取一个值,该值指示是否可重复使用当前转换。

(继承自 HashAlgorithm)
CanTransformMultipleBlocks

当在派生类中重写时,获取一个值,该值指示是否可以转换多个块。

(继承自 HashAlgorithm)
Hash

获取计算所得的哈希代码的值。

(继承自 HashAlgorithm)
HashSize

获取计算所得的哈希代码的大小(以位为单位)。

(继承自 HashAlgorithm)
InputBlockSize

当在派生类中重写时,获取输入块的大小。

(继承自 HashAlgorithm)
OutputBlockSize

当在派生类中重写时,获取输出块的大小。

(继承自 HashAlgorithm)

方法

Clear()

释放 HashAlgorithm 类使用的所有资源。

(继承自 HashAlgorithm)
ComputeHash(Byte[])

计算指定字节数组的哈希值。

(继承自 HashAlgorithm)
ComputeHash(Byte[], Int32, Int32)

计算指定字节数组的指定区域的哈希值。

(继承自 HashAlgorithm)
ComputeHash(Stream)

计算指定 Stream 对象的哈希值。

(继承自 HashAlgorithm)
ComputeHashAsync(Stream, CancellationToken)

异步计算指定 Stream 对象的哈希值。

(继承自 HashAlgorithm)
Create()

创建 RIPEMD160 哈希算法的默认实现的实例。

Create(String)

创建 RIPEMD160 哈希算法的指定实现的实例。

Dispose()

释放 HashAlgorithm 类的当前实例所使用的所有资源。

(继承自 HashAlgorithm)
Dispose(Boolean)

释放由 HashAlgorithm 占用的非托管资源,还可以另外再释放托管资源。

(继承自 HashAlgorithm)
Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
HashCore(Byte[], Int32, Int32)

当在派生类中重写时,将写入对象的数据路由到哈希算法以计算哈希值。

(继承自 HashAlgorithm)
HashCore(ReadOnlySpan<Byte>)

将写入对象的数据路由到哈希算法以计算哈希值。

(继承自 HashAlgorithm)
HashFinal()

在派生类中重写时,在加密哈希算法处理最后一个数据后结束哈希计算。

(继承自 HashAlgorithm)
Initialize()

将哈希算法重置为其初始状态。

(继承自 HashAlgorithm)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)
TransformBlock(Byte[], Int32, Int32, Byte[], Int32)

计算输入字节数组指定区域的哈希值,并将输入字节数组指定区域复制到输出字节数组的指定区域。

(继承自 HashAlgorithm)
TransformFinalBlock(Byte[], Int32, Int32)

计算指定字节数组的指定区域的哈希值。

(继承自 HashAlgorithm)
TryComputeHash(ReadOnlySpan<Byte>, Span<Byte>, Int32)

尝试计算指定字节数组的哈希值。

(继承自 HashAlgorithm)
TryHashFinal(Span<Byte>, Int32)

在哈希算法处理最后一个数据后,尝试结束哈希计算。

(继承自 HashAlgorithm)

显式接口实现

IDisposable.Dispose()

释放由 HashAlgorithm 占用的非托管资源,还可以另外再释放托管资源。

(继承自 HashAlgorithm)

适用于

另请参阅