Clean up and document CryptographyProvider.cs

This commit is contained in:
Patrick Barron 2020-04-14 16:13:41 -04:00
parent ecaae2c8de
commit fd750a9c79

View File

@ -31,7 +31,7 @@ namespace Emby.Server.Implementations.Cryptography
private RandomNumberGenerator _randomNumberGenerator; private RandomNumberGenerator _randomNumberGenerator;
private bool _disposed = false; private bool _disposed;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="CryptographyProvider"/> class. /// Initializes a new instance of the <see cref="CryptographyProvider"/> class.
@ -56,15 +56,13 @@ namespace Emby.Server.Implementations.Cryptography
{ {
// downgrading for now as we need this library to be dotnetstandard compliant // downgrading for now as we need this library to be dotnetstandard compliant
// with this downgrade we'll add a check to make sure we're on the downgrade method at the moment // with this downgrade we'll add a check to make sure we're on the downgrade method at the moment
if (method == DefaultHashMethod) if (method != DefaultHashMethod)
{ {
using (var r = new Rfc2898DeriveBytes(bytes, salt, iterations)) throw new CryptographicException($"Cannot currently use PBKDF2 with requested hash method: {method}");
{
return r.GetBytes(32);
}
} }
throw new CryptographicException($"Cannot currently use PBKDF2 with requested hash method: {method}"); using var r = new Rfc2898DeriveBytes(bytes, salt, iterations);
return r.GetBytes(32);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -74,26 +72,23 @@ namespace Emby.Server.Implementations.Cryptography
{ {
return PBKDF2(hashMethod, bytes, salt, DefaultIterations); return PBKDF2(hashMethod, bytes, salt, DefaultIterations);
} }
else if (_supportedHashMethods.Contains(hashMethod))
{ if (!_supportedHashMethods.Contains(hashMethod))
using (var h = HashAlgorithm.Create(hashMethod))
{ {
throw new CryptographicException($"Requested hash method is not supported: {hashMethod}");
}
using var h = HashAlgorithm.Create(hashMethod);
if (salt.Length == 0) if (salt.Length == 0)
{ {
return h.ComputeHash(bytes); return h.ComputeHash(bytes);
} }
else
{
byte[] salted = new byte[bytes.Length + salt.Length]; byte[] salted = new byte[bytes.Length + salt.Length];
Array.Copy(bytes, salted, bytes.Length); Array.Copy(bytes, salted, bytes.Length);
Array.Copy(salt, 0, salted, bytes.Length, salt.Length); Array.Copy(salt, 0, salted, bytes.Length, salt.Length);
return h.ComputeHash(salted); return h.ComputeHash(salted);
} }
}
}
throw new CryptographicException($"Requested hash method is not supported: {hashMethod}");
}
/// <inheritdoc /> /// <inheritdoc />
public byte[] ComputeHashWithDefaultMethod(byte[] bytes, byte[] salt) public byte[] ComputeHashWithDefaultMethod(byte[] bytes, byte[] salt)