HashedPasswordEntity.cs
<summary>///a hash password////
</summary> public
class hashedpasswordentity
{
// <summary>
///password hash value
///</summary> [SuppressMessage ("Microsoft.Design", "CA1051: Do not declare the visible instance field ", justification =" It's readonly. ")]
Public readonly string Hash;
<summary>
///password Salt value
///</summary> [SuppressMessage ("Microsoft.Design", "CA1051: Do not declare the visible instance field ", justification =" It's readonly. ")]
Public readonly string Salt;
<summary>//
Ctor.
</summary>//
<param name= "hash" > Password hash </param>//
<param name= "Salt" > Password Salt value </param> Public
hashedpasswordentity (string hash, string salt)
{
hash = hash;
Salt = salt;
}
}
Hasher.cs
Using System;
Using System.Diagnostics.CodeAnalysis;
Using System.Security.Cryptography;
Using System.Text;
namespace Hashlibrary {//<summary>//Password hash class///</summary> public class Hasher {
<summary>//Extended ASCII.
</summary> private static Encoding Encoding = encoding.getencoding (437);
<summary>////The length of the hash generated///</summary> public readonly int hashlength;
<summary>///The length of the salt///</summary> public readonly int saltlength; <summary>///constructor////</summary> public hasher (): This (32, 32) {}//
<summary>//Ctor. </summary>//<param name= "Hashlength" > The length of the generated hash </param>//<param name= "Saltlen
Gth "> The length of the resulting salt </param> public hasher (int hashlength, int saltlength) { if (hashlength <= 0) {throw new ArgumentOutOfRangeException ("Hashlength"); } if (saltlength <= 0) {throw new ArgumentOutOfRangeException ("Saltlen
Gth ");
} hashlength = Hashlength;
Saltlength = Saltlength; }///<summary>///Check whether the given password hash is equal to the given hash///</summary>//<param name= "pas Sword "> Hash password </param>//<param name=" hashed "> Hash check </param> public bool Check (string PA ssWOrd, hashedpasswordentity hashed) {if (password = = null) throw new Argumentnullexc
Eption ("password");
if (hashed = = null) throw new ArgumentNullException ("hashed"); var bytes = Encoding.GetBytes (hashed.
Salt); Return hashed.
Hash = = Hashpassword (password, bytes); }///<summary>//Salt,Then hash the given password with PBKDF2//</summary>//<param name= "password" > Password add salt then hash </param>//
<returns> add salt and hash password </returns> public hashedpasswordentity hashpassword (string password) {
var bytes = Generatesalt ();
var hash = hashpassword (password, bytes);
var salt = encoding.getstring (bytes);
return new hashedpasswordentity (hash, salt); }///<summary>//salt, then hash the given password with PBKDF2///</summary>//<param name= "pas Sword "> Password add salt then hash </param>//<param name=" Salt "> Salt value to use </param>//<returns> add salt and H Ash's password/returns> private string Hashpassword (string password, byte[] salt) {//PBKDF2 applies a pseudo-random function to export the key.
The length of the exported key is inherently unlimited (however, the maximum valid search space for the exported key is limited by the structure of the basic pseudo-random function). PBKDF2 simply means that the salted hash is repeated over and over again, and this number is selectable using the (var pbkdf2 = new Rfc2898derivebytes (password, salt)) {var bytes = Pbkdf2.
GetBytes (hashlength);
Return encoding.getstring (bytes); }}///<summary>//Generate a random salt///</summary>//<returns> generated salt value &L T;/returns> Private byte[] Generatesalt () {var random = new Random (unchecked (int) DateTime .
Now.ticks));
var salt = new Byte[saltlength]; Random.
Nextbytes (salt);
return salt; }
}
}
Program.cs
Class Program {static void Main (string[] args) {Empty ();
Console.WriteLine ("-----------------------------------------");
Defaultlengths ();
Console.WriteLine ("-----------------------------------------");
Customlengths ();
Console.readkey ();
} public static void Empty () {var hasher = new Hasher (); var hashed = Hasher. Hashpassword (String.
Empty); Console.WriteLine (Hasher. Check (String.
Empty, hashed)); Console.WriteLine (Hasher.
Check ("", hashed)); Console.WriteLine (Hasher. hashlength+ "-----------" +hashed.
Hash.length); Console.WriteLine (Hasher. Saltlength + "-----------" + hashed.
Salt.length);
} public static void Defaultlengths () {var hasher = new Hasher (); var hashed = Hasher.
Hashpassword ("foo"); Console.WriteLine (Hasher. Check ("foo", Hashed)); Console.WriteLine (Hasher.
Check ("Bar", hashed)); Console.WriteLine (Hasher. Hashlength + "-----------" + hashed.
Hash.length); Console.WriteLine (Hasher. Saltlength + "-----------" + hashed.
Salt.length);
} public static void Customlengths () {var hasher = new Hasher (100, 8); var hashed = Hasher.
Hashpassword ("foo"); Console.WriteLine (Hasher.
Check ("foo", hashed)); Console.WriteLine (Hasher.
Check ("Bar", hashed)); Console.WriteLine (Hasher. Hashlength + "-----------" + hashed.
Hash.length); Console.WriteLine (Hasher. Saltlength + "-----------" + hashed.
Salt.length); }
}
run the results as shown in the figure: