underscore.string
underscore.string copied to clipboard
Adding a hash method to underscore.string.js
A hash method for underscore.string.js
I needed a string-to-hash method for my current project to create keys for strings and to add a simple password check without transferring the password as plain text to the server. I think this hash method for strings fits perfectly with underscore.string - actually, I was a bit surprised that it did not exist already, neither here nor in underscore.js.
About the added method:
_s.hash(str)
returns a number unique for the given string. The method is the same as implemented in Java and thus a kind of standard. It can be used for creating IDs; avoiding plain text when checking or transferring passwords or for caching. The implementation has been chosen according to http://jsperf.com/hashing-strings/22 (the one with the highest performance on the majority of browsers).
Why not just use sha1 or md5?
Hmmm, I do not really care what method is used internally - what ever is more efficient. But the suggested one is compatible with Java. I did not check if the Java way produces the same result as sha1 or md5, though. But i agree that it is a good idea to stick with standards like sha1 and md5. Nevertheless, it would be nice and handy to have this kind of method to be part of underscore.string.js. :)