Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why hashids has MAX_NUMBER limitation #47

Closed
opensso opened this issue Jul 3, 2017 · 6 comments
Closed

Why hashids has MAX_NUMBER limitation #47

opensso opened this issue Jul 3, 2017 · 6 comments

Comments

@opensso
Copy link

opensso commented Jul 3, 2017

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
Copy link
Collaborator

0x3333 commented Jul 3, 2017

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.

@0x3333 0x3333 closed this as completed Jul 3, 2017
@opensso
Copy link
Author

opensso commented Jul 3, 2017

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

@lichenhao
Copy link

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
Copy link
Collaborator

0x3333 commented Aug 19, 2019

We are just a port from JS version.

@lichenhao
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants