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 6019483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:25:42+00:00 2026-05-23T03:25:42+00:00

I need to implement hashing (I am not referring encryption) to make some data

  • 0

I need to implement hashing (I am not referring encryption) to make some data fields (passwords or some details that do not require getting back in original format, rather only need to match in db) secure. Can you please suggest me the best practices to implement hashing. I will be using C# and SQL Server and it will be a web site.

  • 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-05-23T03:25:42+00:00Added an answer on May 23, 2026 at 3:25 am

    OK now you’ve said you’re protecting passwords you have some options.

    The .NET framework has some built in algorithms – MD5, SHA1, SHA2. MD5 and SHA1 are considered obsolete and dangerous now, instead stick to SHA256.

    For example (taken from my book)

    static byte[] GenerateSaltedHash(string password, byte[] salt)
    {
      byte[] plainText = Encoding.UTF8.GetBytes(password);
      HashAlgorithm algorithm = new SHA256Managed();
    
      byte[] plainTextWithSaltBytes = 
        new byte[plainText.Length + salt.Length];
    
      for (int i = 0; i < plainText.Length; i++)
      {
        plainTextWithSaltBytes[i] = plainText[i];
      }
      for (int i = 0; i < salt.Length; i++)
      {
        plainTextWithSaltBytes[plainText.Length + i] = salt[i];
      }
    
      byte[] hash = algorithm.ComputeHash(plainTextWithSaltBytes);            
    }
    

    Now the salt is there to stop precomputed lookups of hashes (hashing itself is not enough any more, people have precomputed hashes of dictionary words and more). But how do you get a salt? Well it’s any unique value really, usually a random set of bytes.

        public byte[] GenerateSalt(int length)
        {
            salt = new byte[length];
    
            // Strong runtime pseudo-random number generator, on Windows uses CryptAPI
            // on Unix /dev/urandom
            RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();
    
            random.GetNonZeroBytes(salt);
    
            return salt;
        }
    

    So you’d call GenerateSalt(32) first to get the salt (32 is just an example, longer if you wish. You will need to store the salt alongside the password – you don’t need to worry about protecting it at all.

    Finally you’ll need a compare function. When you want to check passwords you would take the user input, get the salt for that user, generate the hash for the supplied password and stored salt, and then compare. You would do this using something like

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
    public static bool ConstantCompare(byte[] array1, byte[] array2)
    {
        const byte Zero = 0;
        int maxLength = array1.Length > array2.Length ? array1.Length : array2.Length;
        bool wereEqual = array1.Length == array2.Length;
    
        byte[] paddedArray1 = new byte[maxLength];
        byte[] paddedArray2 = new byte[maxLength];
        for (int i = 0; i < maxLength; i++)
        {
            paddedArray1[i] = array1.Length > i ? array1[i] : Zero;
            paddedArray2[i] = array2.Length > i ? array2[i] : Zero;
        }
        bool compareResult = true;
        for (int i = 0; i < maxLength; i++)
        {
            compareResult = compareResult & paddedArray1[i] == paddedArray2[i];
        }
        return compareResult & wereEqual;
    }
    

    I should, of course, point out the ASP.NET membership functions do salt and hash, so they should probably be a first point of call. No point in rolling your own if someone else has done the work.

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

Sidebar

Related Questions

I need to implement some crypto protocol on C# and want to say that
I need to implement some kind of metric space search in Postgres(*) (PL or
I need to implement a Remember me button in a login form that uses
I need to implement a static extension method supporting member constraints on some basic
First, my question is not about password hashing, but password encryption. I'm building a
i have big decision tree with many Yes/No questions. i need implement some code
I need implement a Data entry like receiver field in mail app (Ex. To:
I need to implement a server that downloads a webpage and sends it to
I need to implement an application that is simultaneously communicating with multiple clients using
I need to implement salts in my encryption, but to do so, I need

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.