1
0
mirror of https://github.com/bitwarden/server.git synced 2025-02-13 01:21:29 +01:00

move to private method

This commit is contained in:
Kyle Spearrin 2024-12-08 19:59:53 -05:00
parent 3ceda47bf9
commit 8bc523ee84

View File

@ -274,24 +274,7 @@ public class AccountsController : Controller
var kdfInformation = await _userRepository.GetKdfInformationByEmailAsync(model.Email); var kdfInformation = await _userRepository.GetKdfInformationByEmailAsync(model.Email);
if (kdfInformation == null) if (kdfInformation == null)
{ {
if (_defaultKdfHmacKey == null) kdfInformation = GetDefaultKdf(model.Email);
{
kdfInformation = _defaultKdfResults[0];
}
else
{
// Compute the HMAC hash of the email
var hmacMessage = Encoding.UTF8.GetBytes(model.Email.Trim().ToLowerInvariant());
using var hmac = new System.Security.Cryptography.HMACSHA256(_defaultKdfHmacKey);
var hmacHash = hmac.ComputeHash(hmacMessage);
// Convert the hash to a number
var hashHex = BitConverter.ToString(hmacHash).Replace("-", string.Empty).ToLowerInvariant();
var hashFirst8Bytes = hashHex.Substring(0, 16);
var hashNumber = long.Parse(hashFirst8Bytes, System.Globalization.NumberStyles.HexNumber);
// Find the default KDF value for this hash number
var hashIndex = (int)(Math.Abs(hashNumber) % _defaultKdfResults.Count);
kdfInformation = _defaultKdfResults[hashIndex];
}
} }
return new PreloginResponseModel(kdfInformation); return new PreloginResponseModel(kdfInformation);
} }
@ -310,4 +293,26 @@ public class AccountsController : Controller
Token = token Token = token
}; };
} }
private UserKdfInformation GetDefaultKdf(string email)
{
if (_defaultKdfHmacKey == null)
{
return _defaultKdfResults[0];
}
else
{
// Compute the HMAC hash of the email
var hmacMessage = Encoding.UTF8.GetBytes(email.Trim().ToLowerInvariant());
using var hmac = new System.Security.Cryptography.HMACSHA256(_defaultKdfHmacKey);
var hmacHash = hmac.ComputeHash(hmacMessage);
// Convert the hash to a number
var hashHex = BitConverter.ToString(hmacHash).Replace("-", string.Empty).ToLowerInvariant();
var hashFirst8Bytes = hashHex.Substring(0, 16);
var hashNumber = long.Parse(hashFirst8Bytes, System.Globalization.NumberStyles.HexNumber);
// Find the default KDF value for this hash number
var hashIndex = (int)(Math.Abs(hashNumber) % _defaultKdfResults.Count);
return _defaultKdfResults[hashIndex];
}
}
} }