Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8772699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:05:56+00:00 2026-06-13T18:05:56+00:00

I would like to ask if this hashing technique is good enough for asp.net?

  • 0

I would like to ask if this hashing technique is good enough for asp.net?

I plan to save the hashed password in a single field called ‘password’ and then hash the user input password on the login page and see if it matches

Here is the code:

public const int SALT_BYTES = 24;
    public const int HASH_BYTES = 24;
    public const int PBKDF2_ITERATIONS = 1000;

    public const int ITERATION_INDEX = 0;
    public const int SALT_INDEX = 1;
    public const int PBKDF2_INDEX = 2;

    /// <summary>
    /// Creates a salted PBKDF2 hash of the password.
    /// </summary>
    /// <param name="password">The password to hash.</param>
    /// <returns>The hash of the password.</returns>
    public static string CreateHash(string password)
    {
        // Generate a random salt
        RNGCryptoServiceProvider csprng = new RNGCryptoServiceProvider();
        byte[] salt = new byte[SALT_BYTES];
        csprng.GetBytes(salt);

        // Hash the password and encode the parameters
        byte[] hash = PBKDF2(password, salt, PBKDF2_ITERATIONS, HASH_BYTES);
        return PBKDF2_ITERATIONS + ":" +
           Convert.ToBase64String(salt) + ":" +
           Convert.ToBase64String(hash);
    }

    /// <summary>
    /// Validates a password given a hash of the correct one.
    /// </summary>
    /// <param name="password">The password to check.</param>
    /// <param name="goodHash">A hash of the correct password.</param>
    /// <returns>True if the password is correct. False otherwise.</returns>
    public static bool ValidatePassword(string password, string goodHash)
    {
        // Extract the parameters from the hash
        char[] delimiter = { ':' };
        string[] split = goodHash.Split(delimiter);
        int iterations = Int32.Parse(split[ITERATION_INDEX]);
        byte[] salt = Convert.FromBase64String(split[SALT_INDEX]);
        byte[] hash = Convert.FromBase64String(split[PBKDF2_INDEX]);

        byte[] testHash = PBKDF2(password, salt, iterations, hash.Length);
        return SlowEquals(hash, testHash);
    }

    /// <summary>
    /// Compares two byte arrays in length-constant time. This comparison
    /// method is used so that password hashes cannot be extracted from 
    /// on-line systems using a timing attack and then attacked off-line.
    /// </summary>
    /// <param name="a">The first byte array.</param>
    /// <param name="b">The second byte array.</param>
    /// <returns>True if both byte arrays are equal. False otherwise.</returns>
    private static bool SlowEquals(byte[] a, byte[] b)
    {
        uint diff = (uint)a.Length ^ (uint)b.Length;
        for (int i = 0; i < a.Length && i < b.Length; i++)
            diff |= (uint)(a[i] ^ b[i]);
        return diff == 0;
    }

    /// <summary>
    /// Computes the PBKDF2-SHA1 hash of a password.
    /// </summary>
    /// <param name="password">The password to hash.</param>
    /// <param name="salt">The salt.</param>
    /// <param name="iterations">The PBKDF2 iteration count.</param>
    /// <param name="outputBytes">The length of the hash to generate, in bytes.</param>
    /// <returns>A hash of the password.</returns>
    private static byte[] PBKDF2(string password, byte[] salt, int iterations, int outputBytes)
    {
        Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, salt);

        pbkdf2.IterationCount = iterations;
        return pbkdf2.GetBytes(outputBytes);
    }

If there are ways I can I improve this it would be awesome if you could point out certain codes that I could replace.

The code is from: http://crackstation.net/hashing-security.htm#aspsourcecode

Sir/Ma’am Thank you++ 😀

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:05:57+00:00Added an answer on June 13, 2026 at 6:05 pm

    The code you mentioned above looks good. I did not find any salt though.

    I would like to mention following code to generate salted HASH.

    See if it can help you out for the same.

     ''' <summary>
    ''' Gets the hash of the string.
    ''' </summary>
    ''' <param name="pPassword">Provided password to encrypt</param>
    Private Function GetHash(ByVal pPassword As String) As String
        Dim sHashedString As String
        dim sSalt1 as string = "YourSalt"
        dim sSalt2 as string = "YourSalt"
        Dim sSaltedString = sSalt1 & pPassword & sSalt2
    
        Try
            sHashedString = ConvertByteArrayToString(New System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sSaltedString)))
        Catch oException As Exception
            sHashedString = String.Empty
        End Try
    
        Return sHashedString
    End Function
    
    ''' <summary>
    ''' Converts the byte array to string.
    ''' </summary>
    ''' <param name="arrInput">The arr input.</param><returns></returns>
    Private Function ConvertByteArrayToString(ByVal arrInput() As Byte) As String
        Dim i As Integer
        Dim sOutput As New System.Text.StringBuilder(arrInput.Length)
    
        For i = 0 To arrInput.Length - 1
            sOutput.Append(arrInput(i).ToString("X2"))
        Next
    
        Return sOutput.ToString()
    End Function
    

    You can simply provide password to generate encrypted string.
    This function is lighter compared to other functions i tried.
    You can simply compare password as string while validating it. No need to add any other functions or methods for validation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like to know if there's something wrong in this asp.net code: mydatareader
I'm new to C# .NET. I would like to ask how this works... What
Im not good in querying. I would like to ask if this query works.
I hope this is the right place to ask this. I would like to
I would like to ask you about some advices about this code. It works,
I would like to ask is there any way to achieve this functionality: I
this is my first question in here, and I would like to ask if
I know this is a weird question to ask, but I would like to
I would like to ask you this question because I am a bit stuck
I would like to ask for help with this, I wanted to check for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.