Click here to Skip to main content
15,896,201 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 
hi i have your article about the generate MD5 code for string

it's very very use full

i want know that is there any way in C#.net to return the MD5 code to the source string?

in fact i want to know if i have a MD5 code, can i get the source string that converted to this MD5 code?

can you help me?

pleas send your answer to my email (( royaye.mahtabi@yahoo.com)) or post your answer to this forum with reply

best regards
kiani,mhedi
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 

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.