Coder Social home page Coder Social logo

Comments (9)

DwayneBull avatar DwayneBull commented on May 31, 2024 2

Given that the powers are small ( only as long as the hashed string ) it would be better to use a manual pow function and avoid bigint. To avoid the reliance on System.Numerics and keep existing compatibility.

i.e.

private long Unhash(string input, string alphabet)
 {
    long number = 0;

    for (var i = 0; i < input.Length; i++)
    {
        var pos = alphabet.IndexOf(input[i]);
        number += (long)(pos * Pow(alphabet.Length, input.Length - i - 1));
    }
    return number;
}
private static long Pow(int target, int power)
{
        if (power == 0) return 1;
        long result = target;
        while (power > 1)
        {
            result *= target;
            power--;
        }
        return result;
}

There are a couple of other edge cases in the code that could fail due to floating point math.

from hashids.net.

ullmark avatar ullmark commented on May 31, 2024

Cool, thanks! I'll have a look and include it in the next version!

from hashids.net.

douglasg14b avatar douglasg14b commented on May 31, 2024

@MarkOtter Maybe make a pull request to get the fix in? It doesn't seem like this project has been updated for a few years.

from hashids.net.

minhhungit avatar minhhungit commented on May 31, 2024

@ullmark @douglasg14b hi any news sir

have you had a chance to review this issue @ullmark

from hashids.net.

ullmark avatar ullmark commented on May 31, 2024

A pull request would have been nice :) I'll update the code and make sure tests execute and then make a release.

from hashids.net.

ullmark avatar ullmark commented on May 31, 2024

Doesn't seem "System.Numerics" is included in .netstandard 1.0...

from hashids.net.

ullmark avatar ullmark commented on May 31, 2024

I could apply @MarkOtter code when not using .netstandard1.0. Would that suffice @minhhungit ?

from hashids.net.

minhhungit avatar minhhungit commented on May 31, 2024

I'm still using .net framework so I don't worry much about .netstandard

from hashids.net.

MarkOtter avatar MarkOtter commented on May 31, 2024

It seems like System.Numerics is available in Standard 2.0+, or there is a NuGet package for Standard 1.1+.

@ullmark I think the decision should be made what minimum version you'd like to support with this code. Given the implementation support chart, I think going with at least 1.1 is a safe decision.

from hashids.net.

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.