Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 98,764 C# Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 1,038 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C# Expert

Register to Make This Box Go Away!



One way hash using SHA1CryptoServiceProvider

This is a snippet I use for applying a one way hash utilizing the SHA1CryptoServiceProvider class in .Net. I use Encoding.Unicode.GetBytes() so it will be compatible with systems already using the CAPICOM version. The CAPICOM views all input as UNICODE not text

Submitted By: PsychoCoder
Actions:
Rating:
Views: 457

Language: C#

Last Modified: May 12, 2008
Instructions: Need a reference to the System.Security.Cryptography and System.Web.Security Namespaces

Snippet


  1. //Namespace Reference
  2. using System.Security.Cryptography;
  3. using System.Web.Security;
  4.  
  5.  
  6. /// <summary>
  7. /// method to hash the users password. To match the CAPICOM hash
  8. /// we convert the string to UNICODE first
  9. /// </summary>
  10. /// <returns></returns>
  11. public string HashString(string str)
  12. {
  13.     //create our SHA1 provider
  14.     SHA1 sha = new SHA1CryptoServiceProvider();
  15.     string hashedValue = string.Empty;
  16.     //hash the data
  17.     byte[] hashedData = sha.ComputeHash(Encoding.Unicode.GetBytes(str));
  18.    
  19.     //loop through each byte in the byte array
  20.     foreach (byte b in hashedData)
  21.     {
  22.         //convert each byte and append
  23.         hashedValue += String.Format("{0,2:X2}", b);
  24.     }
  25.  
  26.     //return the hashed value
  27.     return hashedValue;
  28. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->