Skip to content

Commit eade50f

Browse files
committed
Fix key length error message in ArgumentOutOfRangeException
Updated the error message in `ArgumentOutOfRangeException` to correctly reflect the required key lengths for the algorithm. The message now specifies that the key length must be 256, 384, or 512 bits (32, 48, or 64 symbols), aligning with the logic in the `switch` statement. This improves clarity and prevents developer confusion. Update key length logic to use UTF-8 byte count Refactor key length validation to use UTF-8 byte count instead of symbol count. Introduced `utf8KeyLength` to calculate the byte length of the key. Updated `algorithm` selection logic to use `utf8KeyLength` for determining the appropriate `SecurityAlgorithms` value. Adjusted exception message to reflect the change, specifying key length in bytes rather than symbols.
1 parent 1902fae commit eade50f

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

‎Sources/EasyExtensions.AspNetCore.Authorization/Extensions/ConfigurationExtensions.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ internal static JwtSettings GetJwtSettings(this IConfiguration configuration)
2323
string key = (!jwtSettings.Exists() ? configuration["JwtKey"] : jwtSettings["Key"])
2424
?? throw new KeyNotFoundException("JwtSettings.Key or JwtKey is not set");
2525

26-
string algorithm = key.Length switch
26+
int utf8KeyLength = System.Text.Encoding.UTF8.GetByteCount(key);
27+
string algorithm = utf8KeyLength switch
2728
{
2829
256 / 8 => SecurityAlgorithms.HmacSha256,
2930
384 / 8 => SecurityAlgorithms.HmacSha384,
3031
512 / 8 => SecurityAlgorithms.HmacSha512,
31-
_ => throw new ArgumentOutOfRangeException(nameof(configuration), "Key length must be 128, 192 or 256 bits (16, 24 or 32 symbols), but was " + key.Length + " symbols.")
32+
_ => throw new ArgumentOutOfRangeException(nameof(configuration), "Key length must be 256, 384 or 512 bits (32, 48 or 64 bytes), but was " + utf8KeyLength + " bytes.")
3233
};
3334

3435
return new JwtSettings

0 commit comments

Comments
 (0)