Apply suggestions from code review

Adding minor stylistic suggestions from Bond-009

Co-Authored-By: LogicalPhallacy <44458166+LogicalPhallacy@users.noreply.github.com>
This commit is contained in:
LogicalPhallacy 2019-02-18 00:31:03 -08:00 committed by GitHub
parent d8e6808d77
commit 9f3aa2cead
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 264 additions and 255 deletions

View File

@ -107,7 +107,7 @@ namespace Emby.Server.Implementations.Cryptography
} }
else else
{ {
throw new CryptographicException(String.Format("Requested hash method is not supported: {0}", HashMethod)); throw new CryptographicException($"Requested hash method is not supported: {HashMethod}"));
} }
} }
@ -121,14 +121,21 @@ namespace Emby.Server.Implementations.Cryptography
int iterations = defaultiterations; int iterations = defaultiterations;
if (!hash.Parameters.ContainsKey("iterations")) if (!hash.Parameters.ContainsKey("iterations"))
{ {
hash.Parameters.Add("iterations", defaultiterations.ToString()); hash.Parameters.Add("iterations", defaultiterations.ToString(CultureInfo.InvariantCulture));
} }
else else
{ {
try { iterations = int.Parse(hash.Parameters["iterations"]); } try
catch (Exception e) { iterations = defaultiterations; throw new Exception($"Couldn't successfully parse iterations value from string:{hash.Parameters["iterations"]}", e); } {
iterations = int.Parse(hash.Parameters["iterations"]);
}
catch (Exception e)
{
iterations = defaultiterations;
throw new Exception($"Couldn't successfully parse iterations value from string:{hash.Parameters["iterations"]}", e);
}
} }
return PBKDF2(hash.Id, hash.HashBytes, hash.SaltBytes,iterations); return PBKDF2(hash.Id, hash.HashBytes, hash.SaltBytes, iterations);
} }
public byte[] GenerateSalt() public byte[] GenerateSalt()

View File

@ -79,7 +79,8 @@ namespace Emby.Server.Implementations.Data
foreach (var user in RetrieveAllUsers()) foreach (var user in RetrieveAllUsers())
{ {
// If the user password is the sha1 hash of the empty string, remove it // If the user password is the sha1 hash of the empty string, remove it
if (!string.Equals(user.Password, "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709") || !string.Equals(user.Password, "$SHA1$DA39A3EE5E6B4B0D3255BFEF95601890AFD80709")) if (!string.Equals(user.Password, "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709", StringComparison.Ordinal)
|| !string.Equals(user.Password, "$SHA1$DA39A3EE5E6B4B0D3255BFEF95601890AFD80709", StringComparison.Ordinal))
{ {
continue; continue;
} }

View File

@ -44,7 +44,7 @@ namespace Emby.Server.Implementations.Library
PasswordHash readyHash = new PasswordHash(resolvedUser.Password); PasswordHash readyHash = new PasswordHash(resolvedUser.Password);
byte[] CalculatedHash; byte[] CalculatedHash;
string CalculatedHashString; string CalculatedHashString;
if (_cryptographyProvider.GetSupportedHashMethods().Any(i => i == readyHash.Id)) if (_cryptographyProvider.GetSupportedHashMethods().Contains(readyHash.Id))
{ {
if (String.IsNullOrEmpty(readyHash.Salt)) if (String.IsNullOrEmpty(readyHash.Salt))
{ {
@ -64,7 +64,7 @@ namespace Emby.Server.Implementations.Library
} }
else else
{ {
throw new Exception(String.Format("Requested crypto method not available in provider: {0}", readyHash.Id)); throw new Exception(String.Format($"Requested crypto method not available in provider: {readyHash.Id}"));
} }
//var success = string.Equals(GetPasswordHash(resolvedUser), GetHashedString(resolvedUser, password), StringComparison.OrdinalIgnoreCase); //var success = string.Equals(GetPasswordHash(resolvedUser), GetHashedString(resolvedUser, password), StringComparison.OrdinalIgnoreCase);
@ -95,7 +95,7 @@ namespace Emby.Server.Implementations.Library
if (user.EasyPassword != null && !user.EasyPassword.Contains("$")) if (user.EasyPassword != null && !user.EasyPassword.Contains("$"))
{ {
string hash = user.EasyPassword; string hash = user.EasyPassword;
user.EasyPassword = String.Format("$SHA1${0}", hash); user.EasyPassword = string.Format("$SHA1${0}", hash);
} }
} }
} }
@ -122,7 +122,8 @@ namespace Emby.Server.Implementations.Library
passwordHash.Salt = PasswordHash.ConvertToByteString(passwordHash.SaltBytes); passwordHash.Salt = PasswordHash.ConvertToByteString(passwordHash.SaltBytes);
passwordHash.Id = _cryptographyProvider.DefaultHashMethod; passwordHash.Id = _cryptographyProvider.DefaultHashMethod;
passwordHash.Hash = GetHashedStringChangeAuth(newPassword, passwordHash); passwordHash.Hash = GetHashedStringChangeAuth(newPassword, passwordHash);
}else if (newPassword != null) }
else if (newPassword != null)
{ {
passwordHash.Hash = GetHashedString(user, newPassword); passwordHash.Hash = GetHashedString(user, newPassword);
} }