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;
}
}
}