Skip to content

Commit

Permalink
handle empty hash when decoding guard char, fixes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
manigandham committed Apr 19, 2021
1 parent 75641a4 commit a6759b7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Hashids.net/Hashids.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,17 +388,17 @@ private long[] GetNumbersFrom(string hash)
if (string.IsNullOrWhiteSpace(hash))
return EmptyArray;


var result = new List<long>();
int i = 0;

var hashArray = hash.Split(_guards, StringSplitOptions.RemoveEmptyEntries);
if (hashArray.Length == 0)
return EmptyArray;

var i = 0;
if (hashArray.Length == 3 || hashArray.Length == 2)
{
i = 1;
}

var result = new List<long>();
var hashBreakdown = hashArray[i];
if (hashBreakdown[0] != default(char))
{
Expand Down
11 changes: 11 additions & 0 deletions test/Hashids.net.test/Hashids_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public void AlphabetWithDashes_Encodes()
source.Should().BeEquivalentTo(result);
}

[Fact]
public void GuardCharacterOnly_DecodesToEmptyArray()
{
// no salt creates guard characters: "abde"
var customHashids = new Hashids("");
var decodedValue = customHashids.Decode("a");
decodedValue.Should().BeEquivalentTo(Array.Empty<int>());
}

[Fact]
private void it_has_correct_default_alphabet()
{
Hashids.DEFAULT_ALPHABET.Should().Be(defaultAlphabet);
}
Expand Down

0 comments on commit a6759b7

Please sign in to comment.