SECURITY

Programming Hashing Algorithms (part 3) - Hashing Data from Memory

1/6/2011 8:59:13 AM

3. Hashing Data from Memory

The simplest way to generate a hash code is to use data held in memory. The overloaded ComputeHash method is able to create a hash code from a byte array, as shown by the following signature:

# C#

public byte[] ComputeHash(
byte[] buffer
);

# Visual Basic .NET

Overloads Public Function ComputeHash( _
ByVal buffer( ) As Byte _
) As Byte( )

The ComputeHash returns a byte array that contains the hash code for the message data. The following class demonstrates how to create a hash code for a StringSystem.Text.Encoding class to convert the string to a byte array: value; note that you should use the

# C#

using System;
using System.Text;
using System.Security.Cryptography;

class StringHash {

static void Main(string[] args) {

// define the string that we will
// create a hash code for
String x_str = "Programming .NET Security";

// create a byte array from the string
byte[] x_message_data = Encoding.Default.GetBytes(x_str);

// create an instance of the MD5 hashing algorithm
HashAlgorithm x_hash_alg = HashAlgorithm.Create("MD5");

// obtain the hash code from the HashAlgorithm by
// using the ComputeHash method
byte[] x_hash_code = x_hash_alg.ComputeHash(x_message_data);

// print out the hash code to the console
foreach (byte x_byte in x_hash_code) {
Console.Write("{0:X2} ", x_byte);
}
}
}

# Visual Basic .NET

Imports System
Imports System.Text
Imports System.Security.Cryptography

Module StringHash

Sub Main( )
' define the string that we will
' create a hash code for
Dim x_str As String = "Programming .NET Security"

' create a byte array from the string
Dim x_message_data( ) As Byte = Encoding.Default.GetBytes(x_str)

' create an instance of the MD5 hashing algorithm
Dim x_hash_alg As HashAlgorithm = HashAlgorithm.Create("MD5")

' obtain the hash code from the HashAlgorithm by
' using the ComputeHash method
Dim x_hash_code( ) As Byte = x_hash_alg.ComputeHash(x_message_data)

' print out the hash code to the console
Dim x_byte As Byte
For Each x_byte In x_hash_code
Console.Write("{0:X2} ", x_byte)
Next
End Sub

End Module


The output from this example is:

E1 62 9F C2 96 85 C3 A4 5B 94 97 57 D8 9C 65 78

To make the hash code easier to read, convert each byte to a hexadecimal value and print a space between each one. When you used the ComputeHash method in the previous example, you wanted to process an entire byte array; however, the HashAlgorithm class includes a different version of the method, which can process a region of a byte array:

# C#

public byte[] ComputeHash(
byte[] buffer,
int offset,
int count
);

# Visual Basic .NET

Overloads Public Function ComputeHash( _
ByVal buffer( ) As Byte, _
ByVal offset As Integer, _
ByVal count As Integer _
) As Byte( )
Other  
  •  Programming .NET Security : Hashing Algorithms Explained
  •  Programming .NET Security : Cryptography Explained (part 2)
  •  Programming .NET Security : Cryptography Explained (part 1) - Confidentiality
  •  .NET security : Administering Isolated Storage
  •  .NET security : Programming Isolated Storage
  •  .NET security : Isolated Storage Explained
  •  Programming Role-Based Security
  •  Role-Based Security Explained
  •  Infrastructure Security: The Application Level
  •  Infrastructure Security: The Host Level
  •  Infrastructure Security: The Network Level
  •  .NET Components : Configuring Permissions
  •  The .NET Security Architecture
  •  ASP.NET 4 in VB 2010 : Membership - Role-Based Security
  •  ASP.NET 4 in VB 2010 : The Security Controls
  •  Security Fundamentals : Windows Authentication
  •  Security Fundamentals : Forms Authentication
  •  Working with Assemblies : Overview of Security Changes in .NET 4.0
  •  Publishing ASP.NET Web Applications : MSDeploy Publish
  •  Configuring a Web Application for Security
  •  
    Top 10
    A Look At Truecrypt The Open Source Security Tool
    Price Of Piracy
    Acer Aspire 5600U 23" Touchscreen All-in-One PC
    Zalman FX100-Cube Fanless Cooler
    Devolo dLAN LiveCam Starter Kit
    Has Apple Lost It? (Part 2)
    Has Apple Lost It? (Part 1)
    Sony Computer Entertainment (Part 3)
    Sony Computer Entertainment (Part 2)
    Sony Computer Entertainment (Part 1)
    Most View
    AMD A6-3500 - Llano integrated-graphics processors
    Samsung launched projector, smartphone, and 10.1'' note device
    Working with Access and Connectivity Policies in Vista
    Getting the Most Out of the Microsoft Outlook Client : Using Outlook 2007 (part 2) - Sharing Information with Users Outside the Company
    iPhone Application Development : Making Multivalue Choices with Pickers - Understanding Pickers
    Managing Windows Firewall in Windows Vista
    What To Look For When Buying A New Phone Or Tablet (Part 10)
    Delete & Recover Data (Part 4) - Securely Deleting Data Using Eraser 6.0
    The New Domain Names (Part 2)
    Windows Server 2008 : Domain Name System and IPv6 - DNS in Windows Server 2008 R2
    Customizing the Taskbar in Vista
    Kindle Fire HD - Most Advanced 7" Tablet (Part 2)
    Remote Administration of Exchange Server 2010 Servers : RDP with Exchange Server 2010 (part 1) - Planning and Using Remote Desktop for Administration
    Exchange Server 2010 server roles (part 1) - Mailbox Server role
    Nuforce Air DAC Review
    SQL Server 2005 Native XML Web Services : Exposing SQL Programmability as Web Services (part 1)
    SQL Server 2008 : Working with Multiple-Source Queries - Using Linked Servers
    Nintendo WII U - Modern HD Gaming Console (Part 5)
    SQL Server 2008 R2 : Creating and Managing Indexes - Creating Indexes (part 2) - Creating Indexes with SSMS
    Adobe InDesign CS5 : Importing Graphic Objects (part 3) - Using the Image Import Options Dialog Box