yomorun / hashids-java

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

Why hashids has MAX_NUMBER limitation #47

Closed opensso closed 7 years ago

opensso commented 7 years ago

Hello,

I was looking into this great library for my project, but I quickly found undocumented limitation that this library doesn't support any number > 9007199254740992L. Could you please explain the reasoning behind this limitation?

ids.encode(new Random().nextLong()

Exception in thread "main" java.lang.IllegalArgumentException: number can not be greater than 9007199254740992L at org.hashids.Hashids.encode(Hashids.java:178) at io.remotable.experiments.ids.IdsExperiment.main(IdsExperiment.java:21)

0x3333 commented 7 years ago

The original and reference implementation is a JS version. JS number limitation is (2^53 - 1). In the java implementation we used Long because Integer has a max value of (2^31 - 1). As Long has a limitation of (2^63 - 1) we have to limit the upper bound. That's why we cannot accept values greater than JS.

opensso commented 7 years ago

Thanks for explanation. It would be nice to have ability to provide Max number as constructor argument.

lichenhao commented 5 years ago

Why don't use BigInt for Javascript to resolve the number max value in Javascript instead of use a more little number for java ?

0x3333 commented 5 years ago

We are just a port from JS version.

lichenhao commented 5 years ago

JS version is preparing the BigInt package and will support it with V2. Why don't make the MAX_NUMBER as options instead of final value.

adrian-skybaker commented 3 years ago

This was a suprise to me as well, as the org.hashids.Hashids#encode(Long) method makes no mention of this, so on paper the interface supports any long.

This seems like something that should be on the javadoc. For random numbers, ThreadLocalRandom.current().nextLong(Hashids.MAX_NUMBER) works well.