Skip to content

Commit

Permalink
Fix GenericHash.HashSaltPersonal with a null key
Browse files Browse the repository at this point in the history
Fixes #95
  • Loading branch information
ektrah committed Apr 27, 2024
1 parent c8e59d3 commit 65bdec7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Sodium.Core/GenericHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static byte[] Hash(byte[] message, byte[]? key, int bytes)
if (key == null)
key = Array.Empty<byte>();
else if (key.Length > KEY_BYTES_MAX || key.Length < KEY_BYTES_MIN)
throw new KeyOutOfRangeException(nameof(key), key?.Length ?? 0, $"key must be between {KEY_BYTES_MIN} and {KEY_BYTES_MAX} bytes in length.");
throw new KeyOutOfRangeException(nameof(key), key.Length, $"key must be between {KEY_BYTES_MIN} and {KEY_BYTES_MAX} bytes in length.");
if (bytes > BYTES_MAX || bytes < BYTES_MIN)
throw new BytesOutOfRangeException(nameof(bytes), bytes, $"bytes must be between {BYTES_MIN} and {BYTES_MAX} bytes in length.");

Expand Down Expand Up @@ -109,12 +109,12 @@ public static byte[] HashSaltPersonal(byte[] message, byte[]? key, byte[] salt,
throw new ArgumentNullException(nameof(personal), "Personal string cannot be null");
if (key == null)
key = Array.Empty<byte>();
if (key.Length > KEY_BYTES_MAX || key.Length < KEY_BYTES_MIN)
throw new KeyOutOfRangeException(nameof(key), key?.Length ?? 0, $"key must be between {KEY_BYTES_MIN} and {KEY_BYTES_MAX} bytes in length.");
else if (key.Length > KEY_BYTES_MAX || key.Length < KEY_BYTES_MIN)
throw new KeyOutOfRangeException(nameof(key), key.Length, $"key must be between {KEY_BYTES_MIN} and {KEY_BYTES_MAX} bytes in length.");
if (salt.Length != SALT_BYTES)
throw new SaltOutOfRangeException(nameof(salt), salt?.Length ?? 0, $"Salt must be {SALT_BYTES} bytes in length.");
throw new SaltOutOfRangeException(nameof(salt), salt.Length, $"Salt must be {SALT_BYTES} bytes in length.");
if (personal.Length != PERSONAL_BYTES)
throw new PersonalOutOfRangeException(nameof(personal), personal?.Length ?? 0, $"Personal bytes must be {PERSONAL_BYTES} bytes in length.");
throw new PersonalOutOfRangeException(nameof(personal), personal.Length, $"Personal bytes must be {PERSONAL_BYTES} bytes in length.");
if (bytes > BYTES_MAX || bytes < BYTES_MIN)
throw new BytesOutOfRangeException(nameof(bytes), bytes, $"bytes must be between {BYTES_MIN} and {BYTES_MAX} bytes in length.");

Expand Down
11 changes: 11 additions & 0 deletions test/Sodium.Tests/GenericHashExceptionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ public void GenericHashSaltPersonalNoPersonal()
});
}

[Test]
public void GenericHashSaltPersonalNoKey()
{
const string SALT = "5b6b41ed9b343fe0";
const string PERSONAL = "5126fb2a37400d2a";
Assert.DoesNotThrow(() =>
{
Utilities.BinaryToHex(GenericHash.HashSaltPersonal("message", null, SALT, PERSONAL));
});
}

[Test]
public void GenericHashSaltPersonalKeyTooLong()
{
Expand Down

0 comments on commit 65bdec7

Please sign in to comment.