Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A simple hashing class

0.00/5 (No votes)
12 Feb 2014 1  
This is an alternative for Send Mail With Attachment File

Introduction

Welcome today we will be looking at creating a hashing class in c# using mono and Mono Develop.

Using the code 

A simple hashing class for beginners in c# 

using System;
using System.Security.Cryptography;
namespace Chico
{
	public sealed class Rfc2898Hasher
	{			
		private string hash;		
		public Rfc2898Hasher(byte[] password, byte[] salt, int iterationCount, int cb_a, int cb_b)
		{
			using(Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(password, salt, iterationCount))
			{				
				byte[] ivhash = rfc.GetBytes(cb_a);
				byte[] keyhash = rfc.GetBytes(cb_b);
				int num = unchecked((int)ivhash.Length + keyhash.Length);
				int num2 = 0;				
				byte[] ivkey = new byte[num];
				for(int i = 0; i < ivhash.Length; i++, num2++)
				{
					ivkey[i] = ivhash[i];
				}				
				for(int i = 0; i < keyhash.Length; i++, num2++)
				{
					ivkey[num2] = keyhash[i];					
				}				
				this.hash = Convert.ToBase64String(ivkey);
			}
		}
		public Rfc2898Hasher(byte[] password, byte[] salt, int cb_a, int cb_b)
		{
			using(Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(password, salt))
			{				
				byte[] ivhash = rfc.GetBytes(cb_a);
				byte[] keyhash = rfc.GetBytes(cb_b);
				int num = unchecked((int)ivhash.Length + keyhash.Length);
				int num2 = 0;				
				byte[] ivkey = new byte[num];
				for(int i = 0; i < ivhash.Length; i++, num2++)
				{
					ivkey[i] = ivhash[i];
				}				
				for(int i = 0; i < keyhash.Length; i++, num2++)
				{
					ivkey[num2] = keyhash[i];					
				}				
				this.hash = Convert.ToBase64String(ivkey);
			}
		}
		public Rfc2898Hasher(string password, byte[] salt, int cb_a, int cb_b)
		{
			using(Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(password, salt))
			{				
				byte[] ivhash = rfc.GetBytes(cb_a);
				byte[] keyhash = rfc.GetBytes(cb_b);
				int num = unchecked((int)ivhash.Length + keyhash.Length);
				int num2 = 0;				
				byte[] ivkey = new byte[num];
				for(int i = 0; i < ivhash.Length; i++, num2++)
				{
					ivkey[i] = ivhash[i];
				}				
				for(int i = 0; i < keyhash.Length; i++, num2++)
				{
					ivkey[num2] = keyhash[i];					
				}				
				this.hash = Convert.ToBase64String(ivkey);
			}
		}
		public Rfc2898Hasher(string password, byte[] salt, int iterationCount, int cb_a, int cb_b)
		{
			using(Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(password, salt, iterationCount))
			{				
				byte[] ivhash = rfc.GetBytes(cb_a);
				byte[] keyhash = rfc.GetBytes(cb_b);
				int num = unchecked((int)ivhash.Length + keyhash.Length);
				int num2 = 0;				
				byte[] ivkey = new byte[num];
				for(int i = 0; i < ivhash.Length; i++, num2++)
				{
					ivkey[i] = ivhash[i];
				}				
				for(int i = 0; i < keyhash.Length; i++, num2++)
				{
					ivkey[num2] = keyhash[i];					
				}				
				this.hash = Convert.ToBase64String(ivkey);
			}
		}				
		public new string GetHashCode()
		{
			if(this.hash == null)
			{
				throw new NotSupportedException("hash not set");
			}
			return this.hash;			
		}
	}
}

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