Click here to Skip to main content
14,698,390 members
Articles » Languages » C# » General
Article
Posted 2 Mar 2006

Stats

37.7K views
670 downloads
13 bookmarked

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
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.

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:

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

Share

About the Author

donperry
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 23:36
MemberVikas Gandhi12-Jul-07 23:36 
AnswerRe: How to compare 2 md5 strings Pin
donperry13-Jul-07 5:17
Memberdonperry13-Jul-07 5:17 
QuestionMD5 to String Pin
Kiani.Mehdi24-Feb-07 7:41
MemberKiani.Mehdi24-Feb-07 7:41 
AnswerRe: MD5 to String Pin
donperry25-Feb-07 8:36
Memberdonperry25-Feb-07 8:36 
GeneralRe: MD5 to String Pin
Kiani.Mehdi25-Feb-07 9:04
MemberKiani.Mehdi25-Feb-07 9: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.