Click here to Skip to main content
11,486,673 members (69,993 online)
Click here to Skip to main content

Generating an MD5 Hash from a String Using LINQ

, 7 May 2013 CPOL 6.3K 3
Rate this:
Please Sign up or sign in to vote.
Generating an MD5 hash from a string using LINQ.

While working on another blog post, I encountered the need to generate a MD5 hash by passing in a string value and didn’t see any very appealing solutions off-hand that didn’t involve iterating through a loop. I elected to visit everyone’s favorite friend, LINQ to handle this. 

The Problem

You need to generate an MD5 hash using a string, but you want a quick and easy way to do it.

The Solution

Firstly, you will need to ensure that you are referencing the appropriate namespace (System.Security.Cryptography) to work with MD5 hashes by adding the following using statement :

using System.Security.Cryptography;

Now, just use the following LINQ statement that will allow you to pass in your value and generate an MD5 hash of the string: 

//Your string value
string yourStringValue = "This is a test";
//Generate a hash for your strings (this appends each
//of the bytes of the value into a single hashed string
var hashValue = string.Join("", MD5.Create().ComputeHash(
   Encoding.ASCII.GetBytes(yourStringValue)).Select(s => s.ToString("x2")));
//For .NET 3.5 and below,  you will need to use the .ToArray() following the Select method: 
var hashValue = string.Join("", MD5.Create().ComputeHash(
  Encoding.ASCII.GetBytes(yourStringValue)).Select(s => s.ToString("x2")).ToArray()); 

Or if you would prefer it as a method :

public string GenerateMD5(string yourString)
{
   return string.Join("", MD5.Create().ComputeHash(
      Encoding.ASCII.GetBytes(yourString)).Select(s => s.ToString("x2")));
}

Example :

var example = "This is a Test String"; //"This is a Test String"
var hashed = GenerateMD5(example);     //"7ae92fb767e5024552c90292d3cb1071"

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rion Williams
Software Developer (Senior)
United States United States
An experienced Software Developer and Graphic Designer with an extensive knowledge of object-oriented programming, software architecture, design methodologies and database design principles. Specializing in Microsoft Technologies and focused on leveraging a strong technical background and a creative skill-set to create meaningful and successful applications.

Well versed in all aspects of the software development life-cycle and passionate about embracing emerging development technologies and standards, building intuitive interfaces and providing clean, maintainable solutions for even the most complex of problems.
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 Pin
Omprakash Kukana25-Sep-13 20:40
professionalOmprakash Kukana25-Sep-13 20:40 
GeneralMy vote of 1 Pin
Mahdi 8216102128-Apr-13 1:20
memberMahdi 8216102128-Apr-13 1:20 
GeneralRe: My vote of 1 Pin
Rion Williams28-Apr-13 15:19
memberRion Williams28-Apr-13 15:19 
Could you post how you are using this or encountering this error? The code itself is fairly straight-forward and I would hate to have it down-voted all because of what could be a possible syntax error.

What version of .NET are you using? Did you include the appropriate references that this requires :
using System.Text;
using System.Security.Cryptography;
I would be glad to help you with any issues that you are having with it if I can.
GeneralRe: My vote of 1 Pin
Mahdi 821610216-May-13 19:19
memberMahdi 821610216-May-13 19:19 
GeneralRe: My vote of 1 Pin
Rion Williams7-May-13 3:32
memberRion Williams7-May-13 3:32 
QuestionAn Alternative Pin
George Swan25-Apr-13 0:01
memberGeorge Swan25-Apr-13 0:01 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 7 May 2013
Article Copyright 2013 by Rion Williams
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid