Click here to Skip to main content
15,867,785 members
Articles / Game Development / Unity
Alternative
Tip/Trick

A simple hashing class

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
12 Feb 2014CPOL 5.2K   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# 

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I do not claim to be wrong! I just rarely ever write.

Comments and Discussions

 
-- There are no messages in this forum --