• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
Create Hashes - MD5, SHA1, SHA256, SHA384, SHA512
AuthorCumpsDToday I'll talk about hash functions. A hash algorithm takes a piece of text and gives a hash as result. Thishash is unique for the given text. If you use the hash function on the same text again, you'll get the samehash. But there is no way to get the given text from the hash. This is great for storing passwords.Now, in PHP it's as easy as using
md5()
or
sha1()
. But in C# it takes a bit more work. This is what wewant to simplify.So we'll create a Hash class to create hashes.Create a new project and add a class (Hash).
using System;using System.Security.Cryptography;using System.Text;namespace Hash {public class Hash {public Hash() { } } /* Hash */} /* Hash */
Let's start by adding an enum representing all the hash functions we are going to support.
public enum HashType :int { MD5, SHA1, SHA256, SHA384, SHA512 }
Our class will have 2 public methods, one for creating a hash and one for checking a hash against a giventext.First we'll create the
GetHash
method:
public static string GetHash(string strPlain, HashType hshType) {string strRet;switch (hshType) {case HashType.MD5: strRet = GetMD5(strPlain); break;case HashType.SHA1: strRet = GetSHA1(strPlain); break;case HashType.SHA256: strRet = GetSHA256(strPlain); break;case HashType.SHA384: strRet = GetSHA384(strPlain); break;case HashType.SHA512: strRet = GetSHA512(strPlain); break;default: strRet = "Invalid HashType"; break;}return strRet;} /* GetHash */
And our
CheckHash
will depend on this so we might as well add it now.
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...