Coder Social home page Coder Social logo

Comments (7)

crmeadowcroft avatar crmeadowcroft commented on May 29, 2024

Should have mentioned this was using the default arguments except a minimum hash length of 8. The exception with 1 character still happens using any minimum hash lengths including the default (0).

The System.ArgumentException: Destination is too short. (Parameter 'destination') happens whenever a string is passed in which is less than the minimum hash length.

from hashids.net.

Clive321A avatar Clive321A commented on May 29, 2024

Hi, I just came across this with a Unit test trying to decode the phrase "Junk"

Even the TryDecodeSingle fails? would have thought that one should have just returned false instead of throwing an Exception?
I ended up wrapping the whole thing in a Try Catch, which works, but is this intended behaviour?

from hashids.net.

ullmark avatar ullmark commented on May 29, 2024

@CliveBennett I agree, a function named "TryBlabla" shouldn't ever produce an exception imo if we want to follow the C# naming "standard"...

We should fix that.

from hashids.net.

crmeadowcroft avatar crmeadowcroft commented on May 29, 2024

Sorry I'm not able to work out a Pull request, but in the function "private long GetNumberFrom(string hash)" changing:

Span<char> resultBuffer = stackalloc char[guardedHash.Length];

to

var resultBufferLength = Math.Max(Math.Max(guardedHash.Length, 20), _minHashLength);`
Span<char> resultBuffer = stackalloc char[resultBufferLength];

seems to fix things. The constant length 20 seems to be used commonly through the algorithm though I don't know why. Also could add tests:

[Fact]
void Issue_75_1_character_hash_string_should_not_throw_out_of_range_exception()
{
    var hashids = new Hashids("salt");
    Assert.Throws<NoResultException>(() => hashids.DecodeSingle("a"));
}

[Fact]
void Issue_75_too_short_hash_string_should_not_throw_argument_exception()
{
    var hashids = new Hashids("salt", 40);
    Assert.Throws<NoResultException>(() => hashids.DecodeSingle("ab"));
}

from hashids.net.

manigandham avatar manigandham commented on May 29, 2024

thanks @crmeadowcroft for pointing that out, added a new PR to fix

from hashids.net.

manigandham avatar manigandham commented on May 29, 2024

fixed in 1.6.1: https://www.nuget.org/packages/Hashids.net/1.6.1

from hashids.net.

jetersen avatar jetersen commented on May 29, 2024

Thanks for fixing the bug I introduced in #65

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.