RIPEMD160 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示 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 哈希。
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(1988-1992 年种族完整性基元评估)的框架内开发的。
注意
RIPEMD160 已被安全哈希算法 SHA-256 和 SHA-512 及其派生类取代。 SHA256 和 SHA512 比 RIPEMD160提供更好的安全性和性能。 仅使用 RIPEMD160 与旧应用程序和数据兼容。
构造函数
RIPEMD160() |
初始化 RIPEMD160 类的新实例。 |
字段
HashSizeValue |
表示计算哈希代码的大小(以位为单位)。 (继承自 HashAlgorithm) |
HashValue |
表示计算的哈希代码的值。 (继承自 HashAlgorithm) |
State |
表示哈希计算的状态。 (继承自 HashAlgorithm) |
属性
CanReuseTransform |
获取一个值,该值指示是否可以重复使用当前转换。 (继承自 HashAlgorithm) |
CanTransformMultipleBlocks |
在派生类中重写时,获取一个值,该值指示是否可以转换多个块。 (继承自 HashAlgorithm) |
Hash |
获取计算的哈希代码的值。 (继承自 HashAlgorithm) |
HashSize |
获取计算哈希代码的大小(以位为单位)。 (继承自 HashAlgorithm) |
InputBlockSize |
在派生类中重写时,获取输入块大小。 (继承自 HashAlgorithm) |
OutputBlockSize |
在派生类中重写时,获取输出块大小。 (继承自 HashAlgorithm) |
方法
显式接口实现
IDisposable.Dispose() |
释放 HashAlgorithm 使用的非托管资源,并选择性地释放托管资源。 (继承自 HashAlgorithm) |