Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / C#
Article

MD5 and SHA-1 Hashing (for String and Files)

Rate me:
Please Sign up or sign in to vote.
1.63/5 (9 votes)
2 Mar 2006 39.2K   702   13   5
MD5 and SHA-1 Hashing for String and Files using the .NET cryptography Provider

Introduction

This Page deals provides an Example code to show how to implement the .NET MD5 and SHA-1 cryptography Class.

In order to use the MD5 or SHA-1 Class that .NET provides, u must realize that it accepts and returns byte arrays only.

C#
public byte[] ComputeHash ( byte[] buffer , System.Int32 offset , System.Int32 count )
//or public byte[] ComputeHash ( byte[] buffer)
//buffer is the string represented in bytes

this means that u have to convert the string to a byte array, send it to the function and prepare to convert the result byte array back to string.

this was done using this code i found here on code project:

C#
public string encryptString(byte[] RawStringBytes)
{
    /// <summary>
    /// converts the MD5 of SHA-1 result byte[] to a string 
representative
    /// </summary>
    string hashString ="";
    for(int i=0;i<RawStringBytes.Length;i++)
    {
        hashString += Convert.ToString(RawStringBytes[i], 16).PadLeft(2,'0');
    }
    return hashString.PadLeft(32,'0');
}

just download and examine the c# coding for more details on how it works.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Ayson Baxter Bsc.

born 1984, this young Jamaican programmer is enthused by Network programming and security.


Comments and Discussions

 
QuestionHow to compare 2 md5 strings Pin
Vikas Gandhi12-Jul-07 22:36
Vikas Gandhi12-Jul-07 22:36 
AnswerRe: How to compare 2 md5 strings Pin
donperry13-Jul-07 4:17
donperry13-Jul-07 4:17 
QuestionMD5 to String Pin
Kiani.Mehdi24-Feb-07 6:41
Kiani.Mehdi24-Feb-07 6:41 
AnswerRe: MD5 to String Pin
donperry25-Feb-07 7:36
donperry25-Feb-07 7:36 
GeneralRe: MD5 to String Pin
Kiani.Mehdi25-Feb-07 8:04
Kiani.Mehdi25-Feb-07 8:04 
hi thank's for your answer
but i read about rainbow tables
i read that these tables can reverse md5 code

can you explain about this?
thank's for your help again
best regards
kiani,mehdi

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.