yomorun / hashids-java

Hashids algorithm v1.0.0 implementation in Java
http://hashids.org
MIT License
1.02k stars 155 forks source link

StringIndexOutOfBoundsException when decoding a guard and minHashLength >= 3 #49

Closed meiao closed 7 years ago

meiao commented 7 years ago

I got this exception when trying to decode some bad data. This test case illustrates the exception.

public class TestClass
{
    @Test
    public void test()
    {
        Hashids hasher = new Hashids("this is my salt", 3);
        long[] numbers = hasher.decode("A");
    }
}

And it throws this exception.

java.lang.StringIndexOutOfBoundsException: String index out of range: 2
    at java.lang.String.charAt(String.java:658)
    at org.hashids.Hashids._encode(Hashids.java:292)
    at org.hashids.Hashids._decode(Hashids.java:350)
    at org.hashids.Hashids.decode(Hashids.java:194)
    at TestClass.test(TestClass.java:15)

Looks like any character that is in HashIds.guards will throw this exception. Other input like "asdf" or any other character not in HashIds.guards seem to work ok.

Debugging, I noticed that on the call to _encode, usually an array with 0L is passed, but when one of those chars is used, the call is made with an empty array.

meiao commented 7 years ago

Having multiples of that character also throws the exception. hasher.decode("AAAAAAAAA");

0x3333 commented 7 years ago

Hi meiao, I'll look into this issue and get back to you asap. Thanks.

0x3333 commented 7 years ago

Hi meiao,

This has been fixed on commit eaa4499f44226cbe7c (Issue #45). But this fix has not been released yet. As a workaround, you could build it yourself, until we release another version.

Thanks.

0x3333 commented 7 years ago

Just to inform a new version has been issued! 1.0.3.