Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any builtin javascript string hash function in newest browsers?

Everytime a new versions of browsers show up I hear about new stuff being added, like say webGL and other technologies that no one really knows if they catch up.

But I wonder if someone ever thought about such basic stuff in JS like hashing functions (MD5,SHA1 and the like).

By newest browsers I mean today's development versions too like Opera 12, Chrome 17 or Firefox 10.

Looking now for solution I found this comment on another thread here: https://stackoverflow.com/questions/7204097/short-hashing-function-for-javascript (Do you know that javascript objects already are hashtables ?). So what are these 'hashtables' ? Does it mean that I can make any string into a hash, but not an established one like md5 or sha1 but some JS build in specific ?

basically what I need to do is:

var txt="Hello world!"; var hash = txt.toSha1(); 
like image 734
rsk82 Avatar asked Dec 29 '11 17:12

rsk82


People also ask

Does JavaScript have a built in hash function?

Definition of JavaScript hash() Hash function in Javascript is any function that takes input as arbitrary size data and produces output as fixed-size data. Normally, the returned value of the hash function is called hash code, hash, or hash value.

How do you hash in JavaScript?

You can implement a Hash Table in JavaScript in three steps: Create a HashTable class with table and size initial properties. Add a hash() function to transform keys into indices. Add the set() and get() methods for adding and retrieving key/value pairs from the table.

What is hash code in JavaScript?

A hash function is used to map a given key to a location in the hash table. A hash code is the result of running this hash function over a given key. In V8, the hash code is just a random number, independent of the object value.


1 Answers

For anybody still looking for this information. There is a WebCrypto API which appears to have been finalised at the beginning of 2017.

To use it in a browser, you can find it at window.crypto.subtle which contains methods for encryption, digests etc. Documentation on the available functions here.

like image 163
Sam Bull Avatar answered Sep 17 '22 12:09

Sam Bull