Skip to content

Commit

Permalink
Fixed issue #30. Same behavour as the default implementation(javascri…
Browse files Browse the repository at this point in the history
…pt). Array length check moved up.
  • Loading branch information
0x3333 committed Oct 3, 2016
1 parent f42f1ba commit 256777e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/main/java/org/hashids/Hashids.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,18 @@ public String decryptHex(String hash) {
* @return the encrypt string
*/
public String encode(long... numbers) {
for (long number : numbers) {
if (numbers.length == 0) {
return "";
}

for (long number : numbers) {
if (number < 0) {
return "";
}
if (number > MAX_NUMBER) {
throw new IllegalArgumentException("number can not be greater than " + MAX_NUMBER + "L");
}
}

if (numbers.length == 0) {
return "";
}
return this._encode(numbers);
}

Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/hashids/HashidsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,13 @@ public void test_issue23() {
res2 = a.decode(expected);
Assert.assertEquals(res2[0], num_to_hash);
}

@Test
public void test_issue30() {
String expected = "", res;
long num_to_hash = -1L;
Hashids a = new Hashids("this is my salt");
res = a.encode(num_to_hash);
Assert.assertEquals(expected, res);
}
}

0 comments on commit 256777e

Please sign in to comment.