Comments (9)
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.
Cool, thanks! I'll have a look and include it in the next version!
from hashids.net.
@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.
@ullmark @douglasg14b hi any news sir
have you had a chance to review this issue @ullmark
from hashids.net.
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.
Doesn't seem "System.Numerics" is included in .netstandard 1.0...
from hashids.net.
I could apply @MarkOtter code when not using .netstandard1.0. Would that suffice @minhhungit ?
from hashids.net.
I'm still using .net framework so I don't worry much about .netstandard
from hashids.net.
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)
- how to Creates short unique ids from integers. (only positive numbers & zero) HOT 1
- Bug on long encoding with 16 alphabet chars HOT 8
- Using multiple options for encoding/decoding HOT 1
- Attempting to Decode short strings results in incorrect exceptions being produced HOT 7
- Hashids not generating with proper minhashlength HOT 1
- HashIds not working after adding custom Output formatter HOT 2
- How do I get HashIDs of a specific length? HOT 1
- How use id with Guid/UUID/Sequential Guid HOT 1
- Is there a guarantee of encoded sequence uniqueness with differing salts? HOT 4
- Insufficient Variation In Generated HashIds HOT 1
- Document the effective character limit of the salt HOT 5
- Decoding input smaller than minHashLength unhandled exceptions are thrown HOT 4
- Json support? HOT 3
- Regarding HashidsNet HOT 1
- Website seems down HOT 2
- Valid use-case for library? HOT 1
- Is this lib still being maintained? HOT 2
- Decode null string thows NullReferenceException HOT 1
- Same id generated for different integers HOT 3
- Same hash different integers HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hashids.net.