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

  • SEARCH
  • Home
  • 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 8428881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:10:45+00:00 2026-06-10T05:10:45+00:00

So I recently created a static class for password related methods and had to

  • 0

So I recently created a static class for password related methods and had to make a method that generated a secure salt.

Initially I implemented RNGCryptoServiceProvider and filed n bytes into an array, which I converted to base64 and returned.

The issue was that with the output length, after conversion of course it was longer than n (which makes sense).

To fix this I changed the function to the method below, and I was just wondering if by trimming the base64 string any security risks are raised?

/// <summary>
/// Generates a salt for use with the Hash method.
/// </summary>
/// <param name="length">The length of string to generate.</param>
/// <returns>A cryptographically secure random salt.</returns>
public static string GenerateSalt(int length)
{
    // Check the length isn't too short.
    if (length < MIN_LENGTH)
    {
        throw new ArgumentOutOfRangeException("length", "Please increase the salt length to meet the minimum acceptable value of " + MIN_LENGTH + " characters.");
    }

    // Calculate the number of bytes required.
    // https://en.wikipedia.org/wiki/Base64#Padding
    // http://stackoverflow.com/questions/17944/how-to-round-up-the-result-of-integer-division
    int bytelen = ((3 * length) + 4 - 1) / 4;

    // Create our empty salt array.
    byte[] bytes = new byte[bytelen];

    // Where we'll put our generated salt.
    string salt;

    // Generate a random secure salt.
    using (RNGCryptoServiceProvider randcrypto = new RNGCryptoServiceProvider())
    {
        // Fill our array with random bytes.
        randcrypto.GetBytes(bytes);

        // Get a base64 string from the random byte array.
        salt = GetBase64(bytes);
    }

    // Trim the end off only if we need to.
    if (salt.Length > length)
    {
        // Substring is the fastest method to use.
        salt = salt.Substring(0, length);
    }

    // Return the salt.
    return salt;
}

Also as a side question, I was having a quick look around and couldn’t actually find what the hash function of the C# implementation of RNGCryptoServiceProvider actually is. Anyone know offhand?

  • 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-10T05:10:46+00:00Added an answer on June 10, 2026 at 5:10 am

    There is no security risk with that way of generating the salt.

    The salt doesn’t need that level of security at all, it’s just there so that rainbow tables can’t be used to crack the hash/encryption. The regular Random class would be enough to create a salt.

    Example:

    /// <summary>
    /// Generates a salt for use with the Hash method.
    /// </summary>
    /// <param name="length">The length of string to generate.</param>
    /// <returns>A random salt.</returns>
    public static string GenerateSalt(int length) {
        // Check the length isn't too short.
        if (length < MIN_LENGTH) {
            throw new ArgumentOutOfRangeException("length", "Please increase the salt length to meet the minimum acceptable value of " + MIN_LENGTH + " characters.");
        }
    
        // Where we'll put our generated salt.
        StringBuilder salt = new StringBuilder(length);
    
        // Fill our string with random characters.
        Random rnd = new Random();
        string chars = "0123456798ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        for (int i = 0; i < length; i++) {
          salt.Append(chars[rnd.Next(chars.Length)]);
        }
    
        // Return the salt.
        return salt.ToString();
    }
    

    Note: If the function would be used more than once close in time, you would use a single Random object and pass into the function, as Random instances created too close in time will give the same random sequence.

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

Sidebar

Related Questions

I recently created a generic Matrix<T> class that acts as a wrapper around a
I recently created a new header file that I want to include in the
I recently created a advanced form with elements that use jquery's $().hide & $().show
I recently created a class which has a constructor taking 3 enumerations as arguments.
I recently created a function in javascript that takes in a file name and
I recently created a WCF service that works fine when tested from Visual Studio
I recently switched one of my static html files to a Spring controller that
I recently learned about using C# extension methods to make calling events easier and
I recently created a ListView using ListAdapter and applied a static background image behind
I posted a question recently: Initialization of Static Class members. Now please check this

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.