C# Password Hashing

From , 3 Years ago, written in C#, viewed 213 times.
URL https://pastebin.vip/view/a5462039
  1. namespace PasswordHash
  2.  {
  3.      /// <summary>
  4.      /// Salted password hashing with PBKDF2-SHA1.
  5.      /// Author: havoc AT defuse.ca
  6.      /// www: http://crackstation.net/hashing-security.htm
  7.      /// Compatibility: .NET 3.0 and later.
  8.      /// </summary>
  9.      class PasswordHash
  10.      {
  11.          // The following constants may be changed without breaking existing hashes.
  12.          public const int SALT_BYTES = 24;
  13.          public const int HASH_BYTES = 24;
  14.          public const int PBKDF2_ITERATIONS = 1000;
  15.  
  16.          public const int ITERATION_INDEX = 0;
  17.          public const int SALT_INDEX = 1;
  18.          public const int PBKDF2_INDEX = 2;
  19.                
  20.          /// <summary>
  21.          /// Creates a salted PBKDF2 hash of the password.
  22.          /// </summary>
  23.          /// <param name="password">The password to hash.</param>
  24.          /// <returns>The hash of the password.</returns>
  25.          public static string CreateHash(string password)
  26.          {
  27.              // Generate a random salt
  28.              RNGCryptoServiceProvider csprng = new RNGCryptoServiceProvider();
  29.              byte[] salt = new byte[SALT_BYTES];
  30.              csprng.GetBytes(salt);
  31.  
  32.              // Hash the password and encode the parameters
  33.              byte[] hash = PBKDF2(password, salt, PBKDF2_ITERATIONS, HASH_BYTES);
  34.              return PBKDF2_ITERATIONS + ":" +
  35.                  Convert.ToBase64String(salt) + ":" +
  36.                  Convert.ToBase64String(hash);
  37.          }
  38.  
  39.          /// <summary>
  40.          /// Validates a password given a hash of the correct one.
  41.          /// </summary>
  42.          /// <param name="password">The password to check.</param>
  43.          /// <param name="goodHash">A hash of the correct password.</param>
  44.          /// <returns>True if the password is correct. False otherwise.</returns>
  45.          public static bool ValidatePassword(string password, string goodHash)
  46.          {
  47.              // Extract the parameters from the hash
  48.              char[] delimiter = { ':' };
  49.              string[] split = goodHash.Split(delimiter);
  50.              int iterations = Int32.Parse(split[ITERATION_INDEX]);
  51.              byte[] salt = Convert.FromBase64String(split[SALT_INDEX]);
  52.              byte[] hash = Convert.FromBase64String(split[PBKDF2_INDEX]);
  53.  
  54.              byte[] testHash = PBKDF2(password, salt, iterations, hash.Length);
  55.              return SlowEquals(hash, testHash);
  56.          }
  57.  
  58.          /// <summary>
  59.          /// Compares two byte arrays in length-constant time. This comparison
  60.          /// method is used so that password hashes cannot be extracted from
  61.          /// on-line systems using a timing attack and then attacked off-line.
  62.          /// </summary>
  63.          /// <param name="a">The first byte array.</param>
  64.          /// <param name="b">The second byte array.</param>
  65.          /// <returns>True if both byte arrays are equal. False otherwise.</returns>
  66.          private static bool SlowEquals(byte[] a, byte[] b)
  67.          {
  68.              uint diff = (uint)a.Length ^ (uint)b.Length;
  69.              for (int i = 0; i < a.Length && i < b.Length; i++)
  70.                  diff |= (uint)(a[i] ^ b[i]);
  71.              return diff == 0;
  72.          }
  73.  
  74.          /// <summary>
  75.          /// Computes the PBKDF2-SHA1 hash of a password.
  76.          /// </summary>
  77.          /// <param name="password">The password to hash.</param>
  78.          /// <param name="salt">The salt.</param>
  79.          /// <param name="iterations">The PBKDF2 iteration count.</param>
  80.          /// <param name="outputBytes">The length of the hash to generate, in bytes.</param>
  81.          /// <returns>A hash of the password.</returns>
  82.          private static byte[] PBKDF2(string password, byte[] salt, int iterations, int outputBytes)
  83.          {
  84.              Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, salt);
  85.              pbkdf2.IterationCount = iterations;
  86.              return pbkdf2.GetBytes(outputBytes);
  87.          }
  88.      }
  89.  }
  90. //csharp/4099

Reply to "C# Password Hashing"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website