Skip to main content
Code Review

Return to Answer

Added using block recommended by MSDN
Source Link
user33306
user33306

One way to avoid using constant strings is to use the char.IsLetterOrDigit method, just restrict the values mod 92 and add to 33 to get all printable characters to 125:

enum Password_Options
{
 ALPHANUM,
 ALL
}
RNGCryptoServiceProvider rProvider = new RNGCryptoServiceProvider();
private string CreatePassword(int length, Password_Options options)
{
 StringBuilder res = new StringBuilder();
 byte[] random = new byte[1];
 using (rProvider)
 {
  while (0 < length--)
 {
 char rndChar = '0円';
 do
 {
 rProvider.GetBytes(random);
 rndChar = (char)((random[0] % 92) + 33);
 } while (options == Password_Options.ALPHANUM && !char.IsLetterOrDigit(rndChar));
 res.Append(rndChar);
 }
 }
 return res.ToString();
}

According to MSDN you should always dispose of the rng provider after using it. One way is with a using block.

One way to avoid using constant strings is to use the char.IsLetterOrDigit method, just restrict the values mod 92 and add to 33 to get all printable characters to 125:

enum Password_Options
{
 ALPHANUM,
 ALL
}
RNGCryptoServiceProvider rProvider = new RNGCryptoServiceProvider();
private string CreatePassword(int length, Password_Options options)
{
 StringBuilder res = new StringBuilder();
 byte[] random = new byte[1];
 while (0 < length--)
 {
 char rndChar = '0円';
 do
 {
 rProvider.GetBytes(random);
 rndChar = (char)((random[0] % 92) + 33);
 } while (options == Password_Options.ALPHANUM && !char.IsLetterOrDigit(rndChar));
 res.Append(rndChar);
 }
 return res.ToString();
}

One way to avoid using constant strings is to use the char.IsLetterOrDigit method, just restrict the values mod 92 and add to 33 to get all printable characters to 125:

enum Password_Options
{
 ALPHANUM,
 ALL
}
RNGCryptoServiceProvider rProvider = new RNGCryptoServiceProvider();
private string CreatePassword(int length, Password_Options options)
{
 StringBuilder res = new StringBuilder();
 byte[] random = new byte[1];
 using (rProvider)
 {
  while (0 < length--)
 {
 char rndChar = '0円';
 do
 {
 rProvider.GetBytes(random);
 rndChar = (char)((random[0] % 92) + 33);
 } while (options == Password_Options.ALPHANUM && !char.IsLetterOrDigit(rndChar));
 res.Append(rndChar);
 }
 }
 return res.ToString();
}

According to MSDN you should always dispose of the rng provider after using it. One way is with a using block.

deleted 80 characters in body
Source Link
user33306
user33306

One way to avoid using constant strings is to use the char.IsLetterOrDigit method, just restrict the values mod 92 and add to 33 to get all printable characters to 125:

enum Password_Options
{
 ALPHANUM,
 ALL
}
RNGCryptoServiceProvider rProvider = new RNGCryptoServiceProvider();
private string CreatePassword(int length, Password_Options options)
{
 StringBuilder res = new StringBuilder();
 byte[] random = new byte[1];
 RNGCryptoServiceProvider rProvider = new RNGCryptoServiceProvider();
 while (0 < length--)
 {
 char rndChar = '0円';
 do
 {
 rProvider.GetBytes(random);
 rndChar = (char)((random[0] % 92) + 33);
 } while (options == Password_Options.ALPHANUM && !char.IsLetterOrDigit(rndChar));
 res.Append(rndChar);
 }
 return res.ToString();
}

One way to avoid using constant strings is to use the char.IsLetterOrDigit method, just restrict the values mod 92 and add to 33:

enum Password_Options
{
 ALPHANUM,
 ALL
}
private string CreatePassword(int length, Password_Options options)
{
 StringBuilder res = new StringBuilder();
 byte[] random = new byte[1];
 RNGCryptoServiceProvider rProvider = new RNGCryptoServiceProvider();
 while (0 < length--)
 {
 char rndChar = '0円';
 do
 {
 rProvider.GetBytes(random);
 rndChar = (char)((random[0] % 92) + 33);
 } while (options == Password_Options.ALPHANUM && !char.IsLetterOrDigit(rndChar));
 res.Append(rndChar);
 }
 return res.ToString();
}

One way to avoid using constant strings is to use the char.IsLetterOrDigit method, just restrict the values mod 92 and add to 33 to get all printable characters to 125:

enum Password_Options
{
 ALPHANUM,
 ALL
}
RNGCryptoServiceProvider rProvider = new RNGCryptoServiceProvider();
private string CreatePassword(int length, Password_Options options)
{
 StringBuilder res = new StringBuilder();
 byte[] random = new byte[1];
 while (0 < length--)
 {
 char rndChar = '0円';
 do
 {
 rProvider.GetBytes(random);
 rndChar = (char)((random[0] % 92) + 33);
 } while (options == Password_Options.ALPHANUM && !char.IsLetterOrDigit(rndChar));
 res.Append(rndChar);
 }
 return res.ToString();
}
Post Undeleted by Community Bot
Post Deleted by Community Bot
deleted 80 characters in body
Source Link
user33306
user33306

One way to avoid using constant strings is to use the char.IsLetterOrDigit method, just restrict the values mod 92 and add to 33:

enum Password_Options
{
 ALPHANUM,
 ALL
}
private string CreatePassword(int length, Password_Options options)
{
 StringBuilder res = new StringBuilder();
 byte[] random = new byte[1];
 RNGCryptoServiceProvider rProvider = new RNGCryptoServiceProvider();
 while (0 < length--)
 {
 rProvider.GetBytes(random);
 char rndChar = (char)((random[0] % 92) + 33);
 if (options == Password_Options.ALPHANUM)
  {'0円';
 while(!char.IsLetterOrDigit(rndChar))do
 {
 rProvider.GetBytes(random);
 rndChar = (char)((random[0] % 92) + 33);
 } while (options == Password_Options.ALPHANUM && }!char.IsLetterOrDigit(rndChar));
 res.Append(rndChar);
 }
 return res.ToString();
}

One way to avoid using constant strings is to use the char.IsLetterOrDigit method, just restrict the values mod 92 and add to 33:

enum Password_Options
{
 ALPHANUM,
 ALL
}
private string CreatePassword(int length, Password_Options options)
{
 StringBuilder res = new StringBuilder();
 byte[] random = new byte[1];
 RNGCryptoServiceProvider rProvider = new RNGCryptoServiceProvider();
 while (0 < length--)
 {
 rProvider.GetBytes(random);
 char rndChar = (char)((random[0] % 92) + 33);
 if (options == Password_Options.ALPHANUM)
  {
 while(!char.IsLetterOrDigit(rndChar))
 {
 rProvider.GetBytes(random);
 rndChar = (char)((random[0] % 92) + 33);
 } }
 res.Append(rndChar);
 }
 return res.ToString();
}

One way to avoid using constant strings is to use the char.IsLetterOrDigit method, just restrict the values mod 92 and add to 33:

enum Password_Options
{
 ALPHANUM,
 ALL
}
private string CreatePassword(int length, Password_Options options)
{
 StringBuilder res = new StringBuilder();
 byte[] random = new byte[1];
 RNGCryptoServiceProvider rProvider = new RNGCryptoServiceProvider();
 while (0 < length--)
 {
 char rndChar = '0円';
 do
 {
 rProvider.GetBytes(random);
 rndChar = (char)((random[0] % 92) + 33);
 } while (options == Password_Options.ALPHANUM && !char.IsLetterOrDigit(rndChar));
 res.Append(rndChar);
 }
 return res.ToString();
}
Source Link
user33306
user33306
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /