Coder Social home page Coder Social logo

2^53 + 1 issue about hashids.github.io HOT 5 CLOSED

hashids avatar hashids commented on August 27, 2024
2^53 + 1 issue

from hashids.github.io.

Comments (5)

alco avatar alco commented on August 27, 2024

Here's a basic proof of concept https://github.com/alco/hashids.js/commit/0b268e6873d24095a5c82548cd6d09c9e8ceb95d

The idea is to wrap input numbers in objects that provide the necessary arithmetic functions. By default the wrapper is only used internally and it simply uses native arithmetic. We also limit the range of input numbers (https://github.com/alco/hashids.js/commit/0b268e6873d24095a5c82548cd6d09c9e8ceb95d#diff-ac45510b2aa86482ae1d7551d7e6a36cR137) to ensure compatibility with implementations that use 64-bit integers or big integers.

If the user of the library passes in an object instead of a number, we don't check its range and assume it is an already wrapped number that implements all the required functions. This way, users will be able to extend the library with support for larger numbers without touching its implementation.

The only change to the public interface is in the decrypt function (https://github.com/alco/hashids.js/commit/0b268e6873d24095a5c82548cd6d09c9e8ceb95d#diff-ac45510b2aa86482ae1d7551d7e6a36cR339) were we need to pass a wrapped zero value so that we know which type to use when decoding the string back into numbers.

from hashids.github.io.

jd327 avatar jd327 commented on August 27, 2024

I like the idea of letting the user extend Hashids with their own library to handle big numbers a lot. I think this a) keeps Hashids small and independent of 3rd party libs for those that don't care about handling big numbers and b) the user is free to pick their own implementation.

For example I know for JavaScript, there's a few:
https://github.com/jtobey/javascript-bignum
https://github.com/alexbardas/bignumber.js
http://jsfromhell.com/classes/bignumber

So, people might be inclined to use one over the other. Or maybe make their own.

@alco, thanks for the proof of concept. One followup question though - is there a reason it would be better to provide objects with math functions included to encode(), rather than provide a math lib to the constructor initially and provide strings of large ints to encode() after that?

from hashids.github.io.

alco avatar alco commented on August 27, 2024

@ivanakimov It makes sense to pass the functions to encode or the Hashids constructor, it just didn't cross my mind at the time. I'll take a look at the libraries you mentioned and will try building another prototype with support for big numbers.

from hashids.github.io.

zakame avatar zakame commented on August 27, 2024

For perl, apparently it only breaks for 2^60.

I can probably use bignum here for >= 2^60.

from hashids.github.io.

4kimov avatar 4kimov commented on August 27, 2024

Closing since the project is moving. New algo has maxValue() method to determine upper integer limit, which will differ per implementation.

from hashids.github.io.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.