title: Am I supposed to save the salt somewhere when hashing a password?
body: <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Console.WriteLine("Sergio");
          Console.WriteLine(HashString("Sergio"));
          Console.WriteLine(HashString("Sergio"));
          Console.WriteLine(HashString("Sergio"));
          Console.ReadKey();
      }
  
      public static string HashString(string value)
      {
          int minSaltSize = 4;
          int maxSaltSize = 8;
          Random random = new Random();
          int saltSize = random.Next(minSaltSize, maxSaltSize);
          byte[] saltBytes = new byte[saltSize];
  
          RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
          rng.GetNonZeroBytes(saltBytes);
  
          //Convert the string value into a byte array.
          UTF8Encoding utf8Encoder = new UTF8Encoding();
          byte[] plainTextBytes = utf8Encoder.GetBytes(value);
  
          //Allocate an array to hold the text bytes and the salt bytes.
          byte[] plainTextWithSaltBytes = new Byte[saltBytes.Length + plainTextBytes.Length];
  
          for (int i = 0; i &lt; plainTextBytes.Length; i++)
          {
              plainTextWithSaltBytes[i] = plainTextBytes[i];
          }
  
          for (int i = 0; i &lt; saltBytes.Length; i++)
          {
              plainTextWithSaltBytes[plainTextBytes.Length + i] = saltBytes[i];
          }
  
          HashAlgorithm hash = new SHA256Managed();
          byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes);
          string hashedValue = Convert.ToBase64String(hashBytes);
          return hashedValue;
      }
  }
  </code></pre>
  
  <p>The result of my code is that hashed "Sergio" is different everytime. This is caused by the random factor included in the salt. My question is, am I supposed to save the salt somewhere? Or do I define a constant salt in my code?</p>
  
  <p>Thank you!</p>