RIPEMD160Managed 类

定义

使用托管库计算输入数据的 RIPEMD160 哈希值。

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

示例

下面的代码示例演示如何使用 类对文件进行 RIPEMD160Managed 编码,然后如何解码文件。

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 是在欧盟项目 RIPE (RACE 完整性基元评估(1988-1992) )的框架下开发的。

注意

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

构造函数

RIPEMD160Managed()

初始化 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)
Dispose()

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

(继承自 HashAlgorithm)
Dispose(Boolean)

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

(继承自 HashAlgorithm)
Equals(Object)

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

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

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

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

HashCore(ReadOnlySpan<Byte>)

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

(继承自 HashAlgorithm)
HashFinal()

当在派生类中重写时,在加密流对象处理完最后的数据后完成哈希计算。

Initialize()

使用托管库初始化 RIPEMD160Managed 类的实例。

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)

适用于

另请参阅