0

I've been searching around for a simple-lightweight hashing algorithm for JavaScript. I did find this numerically-based answer on Stack Overflow here.

Unfortunately, I am unable to use this since it's numerically based and I'll need to use this hash as a unique index elsewhere in my code. Often this function returns negative numbers and that would be a big no-no (try 'hello world!'.hashCode() from the snippet linked above to see what I mean).

I've been tempted to use the md5 hashing libraries out there for JS but they're simply to bulky for my purpose and encryption libraries (such as this) are overkill.

It's worth noting that the information within this hash isn't sensitive in anyway and it wouldn't necessarily matter if it was decrypted. The purpose of this function would be to simply generate fixed-length output data that acts as a shortened reference to the original data that I would pass in.

Any help, tips and comments are much appreciated :)

2
  • 1
    How about Math.abs('hello world!'.hashCode())? Commented Jul 1, 2013 at 9:44
  • @KooiInc Wouldn't that increase collisions two-fold? Commented Jul 1, 2013 at 10:46

2 Answers 2

0

The solution proposed by Kooilnc, to use the absolute value, should do the tric for you. However, if you want to use a hashing function to generate a reference, i assume that the reference you get should be unique as to match the exact element it was generated from. If this is the case, be aware of collisions. Hashing function can create hashes that are similar even though the original messages are different and we call this a collision. If i remember correctly, SHA-1 is also available for java script and is not all that bulk. Good luck

Sign up to request clarification or add additional context in comments.

Comments

0

I am unable to use this since it's numerically based and I'll need to use this hash as a unique index elsewhere in my code.

Hash functions are normally numerically based and are rarely perfect (produce unique keys). I think you need something different:

function GuidGen()
{
    this.items = {};
    this.size = 0;
}

GuidGen.prototype.get = function(str)
{
    if (!(str in this.items))
    {
        this.items[str] = this.size++;
    }
    return this.items[str];
}

// usage:
id = new GuidGen();
id.get("hello world");  // 0
id.get("spam");         // 1
id.get("eggs");         // 2
id.get("hello world");  // 0

6 Comments

Sorry, I should have been a bit clearer. Your answer is completely legitimate, but my needs are slightly different. Often there are many string that are similar in my set and these will need distinguishing by adding a timestamp + misc. data. This results in very long strings (often 30 - 40 characters in length). This, unfortunately, is unacceptable, hence my approach to hashing. The hope is that I will be able to have strings produced of a fixed length that would be good enough as unique identifiers.
@zesda Can you elaborate on the usage of these strings? In particular whether or not they are shared across different programs/computers and how long their lifetime is.
Essentially I have a file uploader written in JS. I'm going to use these strings to reference the files that are uploaded. The issue arises when it comes to referencing these uploaded files on a unique basis. Indexing these file uploads is a messy business because it's an asynchronous process, so I'm using information from the files themselves to generate unique string-based IDs. The information available is type, name, size and last modified dates. But these aren't unique without a timestamp. Hence my ambition to use hashing to reduce string length.
Right. So what's stopping you from using a random number as a 'hash'? You could still get collisions, but that's always going to happen with fixed-length keys (unless you request free IDs from the server).
Random numbers do not inherit their values from the original data. If I can't replicate the hash elsewhere then how do I reference my file?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.