Change braces to use VS style

This commit is contained in:
Lucas Nicodemus 2015-04-13 14:49:04 -06:00
parent 379a689ae2
commit 493dc0c069
2 changed files with 70 additions and 26 deletions

View file

@ -924,10 +924,12 @@ namespace TShockAPI
{ {
user.Name = args.Player.Name; user.Name = args.Player.Name;
echoPassword = args.Parameters[0]; echoPassword = args.Parameters[0];
try { try
{
user.CreateBCryptHash(args.Parameters[0]); user.CreateBCryptHash(args.Parameters[0]);
} }
catch (ArgumentOutOfRangeException) { catch (ArgumentOutOfRangeException)
{
args.Player.SendErrorMessage("Password must be greater than or equal to " + TShock.Config.MinimumPasswordLength + " characters."); args.Player.SendErrorMessage("Password must be greater than or equal to " + TShock.Config.MinimumPasswordLength + " characters.");
return; return;
} }
@ -936,9 +938,12 @@ namespace TShockAPI
{ {
user.Name = args.Parameters[0]; user.Name = args.Parameters[0];
echoPassword = args.Parameters[1]; echoPassword = args.Parameters[1];
try { try
{
user.CreateBCryptHash(args.Parameters[1]); user.CreateBCryptHash(args.Parameters[1]);
} catch (ArgumentOutOfRangeException) { }
catch (ArgumentOutOfRangeException)
{
args.Player.SendErrorMessage("Password must be greater than or equal to " + TShock.Config.MinimumPasswordLength + " characters."); args.Player.SendErrorMessage("Password must be greater than or equal to " + TShock.Config.MinimumPasswordLength + " characters.");
return; return;
} }
@ -991,7 +996,9 @@ namespace TShockAPI
user.Name = args.Parameters[1]; user.Name = args.Parameters[1];
try { try {
user.CreateBCryptHash(args.Parameters[2]); user.CreateBCryptHash(args.Parameters[2]);
} catch (ArgumentOutOfRangeException) { }
catch (ArgumentOutOfRangeException)
{
args.Player.SendErrorMessage("Password must be greater than or equal to " + TShock.Config.MinimumPasswordLength + " characters."); args.Player.SendErrorMessage("Password must be greater than or equal to " + TShock.Config.MinimumPasswordLength + " characters.");
return; return;
} }

View file

@ -348,15 +348,21 @@ namespace TShockAPI.DB
/// </summary> /// </summary>
/// <param name="password">string password - The password to check against the user object.</param> /// <param name="password">string password - The password to check against the user object.</param>
/// <returns>bool true, if the password matched, or false, if it didn't.</returns> /// <returns>bool true, if the password matched, or false, if it didn't.</returns>
public bool VerifyPassword(string password) { public bool VerifyPassword(string password)
try { {
if (BCrypt.Net.BCrypt.Verify(password, this.Password)) { try
{
if (BCrypt.Net.BCrypt.Verify(password, this.Password))
{
// If necessary, perform an upgrade to the highest work factor. // If necessary, perform an upgrade to the highest work factor.
upgradePasswordWorkFactor(password); upgradePasswordWorkFactor(password);
return true; return true;
} }
} catch (SaltParseException) { }
if (hashPassword(password).ToUpper() == this.Password.ToUpper()) { catch (SaltParseException)
{
if (hashPassword(password).ToUpper() == this.Password.ToUpper())
{
// The password is not stored using BCrypt; upgrade it to BCrypt immediately // The password is not stored using BCrypt; upgrade it to BCrypt immediately
upgradePasswordToBCrypt(password); upgradePasswordToBCrypt(password);
return true; return true;
@ -368,21 +374,28 @@ namespace TShockAPI.DB
/// <summary>Upgrades a password to BCrypt, from an insecure hashing algorithm.</summary> /// <summary>Upgrades a password to BCrypt, from an insecure hashing algorithm.</summary>
/// <param name="password">string password - the raw user password (unhashed) to upgrade</param> /// <param name="password">string password - the raw user password (unhashed) to upgrade</param>
protected internal void upgradePasswordToBCrypt(string password) { protected internal void upgradePasswordToBCrypt(string password)
{
// Save the old password, in the event that we have to revert changes. // Save the old password, in the event that we have to revert changes.
string oldpassword = this.Password; string oldpassword = this.Password;
// Convert the password to BCrypt, and save it. // Convert the password to BCrypt, and save it.
try { try
{
this.Password = BCrypt.Net.BCrypt.HashPassword(password, TShock.Config.BCryptWorkFactor); this.Password = BCrypt.Net.BCrypt.HashPassword(password, TShock.Config.BCryptWorkFactor);
} catch (ArgumentOutOfRangeException) { }
catch (ArgumentOutOfRangeException)
{
TShock.Log.ConsoleError("Invalid BCrypt work factor in config file! Upgrading user password to BCrypt using default work factor."); TShock.Log.ConsoleError("Invalid BCrypt work factor in config file! Upgrading user password to BCrypt using default work factor.");
this.Password = BCrypt.Net.BCrypt.HashPassword(password); this.Password = BCrypt.Net.BCrypt.HashPassword(password);
} }
try { try
{
TShock.Users.SetUserPassword(this, this.Password); TShock.Users.SetUserPassword(this, this.Password);
} catch (UserManagerException e) { }
catch (UserManagerException e)
{
TShock.Log.ConsoleError(e.ToString()); TShock.Log.ConsoleError(e.ToString());
this.Password = oldpassword; // Revert changes this.Password = oldpassword; // Revert changes
} }
@ -390,20 +403,28 @@ namespace TShockAPI.DB
/// <summary>Upgrades a password to the highest work factor available in the config.</summary> /// <summary>Upgrades a password to the highest work factor available in the config.</summary>
/// <param name="password">string password - the raw user password (unhashed) to upgrade</param> /// <param name="password">string password - the raw user password (unhashed) to upgrade</param>
protected internal void upgradePasswordWorkFactor(string password) { protected internal void upgradePasswordWorkFactor(string password)
{
// If the destination work factor is not greater, we won't upgrade it or re-hash it // If the destination work factor is not greater, we won't upgrade it or re-hash it
int currentWorkFactor = Convert.ToInt32((this.Password.Split('$')[2])); int currentWorkFactor = Convert.ToInt32((this.Password.Split('$')[2]));
if (currentWorkFactor < TShock.Config.BCryptWorkFactor) { if (currentWorkFactor < TShock.Config.BCryptWorkFactor)
try { {
try
{
this.Password = BCrypt.Net.BCrypt.HashPassword(password, TShock.Config.BCryptWorkFactor); this.Password = BCrypt.Net.BCrypt.HashPassword(password, TShock.Config.BCryptWorkFactor);
} catch (ArgumentOutOfRangeException) { }
catch (ArgumentOutOfRangeException)
{
TShock.Log.ConsoleError("Invalid BCrypt work factor in config file! Refusing to change work-factor on exsting password."); TShock.Log.ConsoleError("Invalid BCrypt work factor in config file! Refusing to change work-factor on exsting password.");
} }
try { try
{
TShock.Users.SetUserPassword(this, this.Password); TShock.Users.SetUserPassword(this, this.Password);
} catch (UserManagerException e) { }
catch (UserManagerException e)
{
TShock.Log.ConsoleError(e.ToString()); TShock.Log.ConsoleError(e.ToString());
} }
} }
@ -411,19 +432,35 @@ namespace TShockAPI.DB
/// <summary>Creates a BCrypt hash for a user and stores it in this object.</summary> /// <summary>Creates a BCrypt hash for a user and stores it in this object.</summary>
/// <param name="password">string password - the plain text password to hash</param> /// <param name="password">string password - the plain text password to hash</param>
public void CreateBCryptHash(string password) { public void CreateBCryptHash(string password)
{
if (password.Trim().Length < Math.Max(4, TShock.Config.MinimumPasswordLength)) { if (password.Trim().Length < Math.Max(4, TShock.Config.MinimumPasswordLength))
{
throw new ArgumentOutOfRangeException("password", "Password must be > " + TShock.Config.MinimumPasswordLength + " characters."); throw new ArgumentOutOfRangeException("password", "Password must be > " + TShock.Config.MinimumPasswordLength + " characters.");
} }
try { try
{
this.Password = BCrypt.Net.BCrypt.HashPassword(password.Trim(), TShock.Config.BCryptWorkFactor); this.Password = BCrypt.Net.BCrypt.HashPassword(password.Trim(), TShock.Config.BCryptWorkFactor);
} catch (ArgumentOutOfRangeException) { }
catch (ArgumentOutOfRangeException)
{
TShock.Log.ConsoleError("Invalid BCrypt work factor in config file! Creating new hash using default work factor."); TShock.Log.ConsoleError("Invalid BCrypt work factor in config file! Creating new hash using default work factor.");
this.Password = BCrypt.Net.BCrypt.HashPassword(password.Trim()); this.Password = BCrypt.Net.BCrypt.HashPassword(password.Trim());
} }
} }
/// <summary>Creates a BCrypt hash for a user and stores it in this object.</summary>
/// <param name="password">string password - the plain text password to hash</param>
/// <param name="workFactor">int workFactor - the work factor to use in generating the password hash</param>
public void CreateBCryptHash(string password, int workFactor)
{
if (password.Trim().Length < Math.Max(4, TShock.Config.MinimumPasswordLength))
{
throw new ArgumentOutOfRangeException("password", "Password must be > " + TShock.Config.MinimumPasswordLength + " characters.");
}
this.Password = BCrypt.Net.BCrypt.HashPassword(password.Trim(), workFactor);
}
/// <summary> /// <summary>
/// A dictionary of hashing algortihms and an implementation object. /// A dictionary of hashing algortihms and an implementation object.
/// </summary> /// </summary>