Export (0) Print
Expand All

RIPEMD160 Class

Note: This class is new in the .NET Framework version 2.0.

Represents the abstract class from which all implementations of the MD160 hash algorithm inherit.

Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)] 
public abstract class RIPEMD160 : HashAlgorithm
/** @attribute ComVisibleAttribute(true) */ 
public abstract class RIPEMD160 extends HashAlgorithm
ComVisibleAttribute(true) 
public abstract class RIPEMD160 extends HashAlgorithm

Hash functions map binary strings of an arbitrary length to small binary strings of a fixed length. A cryptographic hash function has the property that it is computationally infeasible to find two distinct inputs that hash to the same value; that is, hashes of two sets of data should match if the corresponding data also matches. Small changes to the data result in large unpredictable changes in the hash.

RIPEMD-160 is a 160-bit cryptographic hash function. It is intended for use as a replacement for the 128-bit hash functions MD4, MD5, and RIPEMD. RIPEMD was developed in the framework of the EU project RIPE (RACE Integrity Primitives Evaluation, 1988-1992).

The following code example calculates the RIPEMD160 hash for all files in a directory.

using System;
using System.IO;
using System.Security.Cryptography;

	public class HashDirectory
	{
		// 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();
		}

		public static void Main(String[] args)
		{
			if (args.Length < 1)
			{
				Console.WriteLine("Usage: hashdir <directory>");
				return;
			}
			try
			{
				// Create a DirectoryInfo object representing the specified directory.
				DirectoryInfo dir = new DirectoryInfo(args[0]);
				// 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);
					// 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.");
			}
		}
	}

import System.*;
import System.IO.*;
import System.Security.Cryptography.*;

public class HashDirectory
{
    // Print the byte array in a readable format.
    public static void PrintByteArray(ubyte array[])
    {
        int i;
        for (i = 0; i < array.get_Length(); i++) {
            Console.Write(String.Format("{0:X2}", array.get_Item(i)));
            if (i % 4 == 3) {
                Console.Write(" ");
            }
        }
        Console.WriteLine();
    } //PrintByteArray

    public static void main(String[] args)
    {        
        if (args.get_Length() < 1) {
            Console.WriteLine("Usage: hashdir <directory>");
            return;
        }
        try {
            // Create a DirectoryInfo object representing the specified 
            // directory.
            DirectoryInfo dir = new DirectoryInfo(args[0]);
            // Get the FileInfo objects for every file in the directory.
            FileInfo files[] = dir.GetFiles();
            // Initialize a RIPE160 hash object.
            RIPEMD160 myRIPEMD160 = RIPEMD160Managed.Create();
            ubyte hashValue[];
            FileInfo fInfo = null;
            for (int iCtr = 0; iCtr < files.get_Length(); iCtr++) {
                // Compute and print the hash values for each file in directory.
                fInfo = files[iCtr];
                // 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(fInfo.get_Name() + ": ");
                // Write the hash value to the Console.
                PrintByteArray(hashValue);
                // Close the file.
                fileStream.Close();
            }
            return;
        }
        catch (DirectoryNotFoundException exp) {
            Console.WriteLine("Error: The directory specified could"
                + "not be found.");
        }
        catch (IOException exp) {
            Console.WriteLine("Error: A file in the directory could not "
                + "be accessed.");
        }
    } //main
} //HashDirectory

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft