Complete Password Hashing Solution using C# with Example
Pbkdf2 using System; using System.Linq; using System.Security.Cryptography; namespace YourCryptoNamespace { /// /// Salted password hashing with PBKDF2-SHA1. /// Compatibility: .NET 3.0 and later. /// /// See http://crackstation.net/hashing-security.htm for much more on password hashing. public static class PasswordHashProvider { /// /// The salt byte size, 64 length ensures safety but could be increased / decreased /// private const int SaltByteSize = 64; /// /// The hash byte size, /// private const int HashByteSize = 64; /// /// High iteration count is less likely to be cracked /// private const int Pbkdf2Iterations = 10000; /// /// Creates a salted PBKDF2 hash of the password. /// /// /// The salt and the hash have to be persisted side by side for the password. They could be persisted as bytes or as a string using the convenience methods in the next class to convert from byte[] to string and later back again when executing password validation. /// /// The password to hash. /// The hash of the password. public static PasswordHashContainer CreateHash(string password) { // Generate a random salt using (var csprng = new RNGCryptoServiceProvider()) { // create a unique salt for every password hash to prevent rainbow and dictionary based attacks var salt = new byte[SaltByteSize]; csprng.GetBytes(salt); // Hash the password and encode the parameters var hash = Pbkdf2(password, salt, Pbkdf2Iterations, HashByteSize); return new PasswordHashContainer(hash, salt); } } /// /// Recreates a password hash based on the incoming password string and the stored salt /// /// The password to check. /// The salt existing. /// the generated hash based on the password and salt public static byte[] CreateHash(string password, byte[] salt) { // Extract the parameters from the hash return Pbkdf2(password, salt, Pbkdf2Iterations, HashByteSize); } /// /// Validates a password given a hash of the correct one. /// /// The password to check. /// The existing stored salt. /// The hash of the existing password. /// true if the password is correct. false otherwise. public static bool ValidatePassword(string password, byte[] salt, byte[] correctHash) { // Extract the parameters from the hash byte[] testHash = Pbkdf2(password, salt, Pbkdf2Iterations, HashByteSize); return CompareHashes(correctHash, testHash); } /// /// Compares two byte arrays (hashes) /// /// The array1. /// The array2. /// true if they are the same, otherwise false public static bool CompareHashes(byte[] array1, byte[] array2) { if (array1.Length != array2.Length) return false; return !array1.Where((t, i) => t != array2[i]).Any(); } /// /// Computes the PBKDF2-SHA1 hash of a password. /// /// The password to hash. /// The salt. /// The PBKDF2 iteration count. /// The length of the hash to generate, in bytes. /// A hash of the password. private static byte[] Pbkdf2(string password, byte[] salt, int iterations, int outputBytes) { using (var pbkdf2 = new Rfc2898DeriveBytes(password, salt)) { pbkdf2.IterationCount = iterations; return pbkdf2.GetBytes(outputBytes); } } } /// /// Container for password hash and salt and iterations. /// public sealed class PasswordHashContainer { /// /// Gets the hashed password. /// public byte[] HashedPassword { get; private set; } /// /// Gets the salt. /// public byte[] Salt { get; private set; } /// /// Initializes a new instance of the class. /// /// The hashed password. /// The salt. public PasswordHashContainer(byte[] hashedPassword, byte[] salt) { this.HashedPassword = hashedPassword; this.Salt = salt; } } /// /// Convenience methods for converting between hex strings and byte array. /// public static class ByteConverter { /// /// Converts the hex representation string to an array of bytes /// /// The hexed string. /// public static byte[] GetHexBytes(string hexedString) { var bytes = new byte[hexedString.Length / 2]; for (var i = 0; i < bytes.Length; i++) { var strPos = i * 2; var chars = hexedString.Substring(strPos, 2); bytes[i] = Convert.ToByte(chars, 16); } return bytes; } /// /// Gets a hex string representation of the byte array passed in. /// /// The bytes. public static string GetHexString(byte[] bytes) { return BitConverter.ToString(bytes).Replace("-", "").ToUpper(); } } } /* * Password Hashing With PBKDF2 (http://crackstation.net/hashing-security.htm). * Copyright (c) 2013, Taylor Hornby * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ Please see this excellent resource Crackstation - Salted Password Hashing - Doing it Right for more information. Part of this solution (the hashing function) was based on the code from that site.