Javascript hash function built in

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();

asked Dec 29, 2011 at 17:10

3

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.

answered Jul 9, 2017 at 16:14

Sam BullSam Bull

2,2031 gold badge14 silver badges17 bronze badges

1

Paul Johnston has implemented the following algorithms in javascript

MD5, RIPEMD-160, SHA-1, SHA-256 and sha-512

You can find the source code and some examples here: http://pajhome.org.uk/crypt/md5/

I hope this is what you were looking for.

answered Dec 29, 2011 at 17:19

Javascript hash function built in

Greg GuidaGreg Guida

6,9824 gold badges28 silver badges40 bronze badges

3

async function sha256(source) {
    const sourceBytes = new TextEncoder().encode(source);
    const digest = await crypto.subtle.digest("SHA-256", sourceBytes);
    const resultBytes = [...new Uint8Array(digest)];
    return resultBytes.map(x => x.toString(16).padStart(2, '0')).join("");
}

answered Dec 6, 2021 at 9:24

YozYoz

5155 silver badges17 bronze badges

1

Note: This answer was written in 2014 when the Web Cryptography API was not available. Do not use this in the context where cryptographic security is needed. This may be useful when you need a simple reversible encryption with "builtin" support.

When I need simple client side hashing without external libraries I use the browsers' built in atob() and btoa() functions.

window.btoa() creates a base-64 encoded ASCII string from a "string" of binary data.

function utf8_to_b64( str ) {
    return window.btoa(encodeURIComponent( escape( str )));
}

The window.atob() function decodes a string of data which has been encoded using base-64 encoding.

function b64_to_utf8( str ) {
    return unescape(decodeURIComponent(window.atob( str )));
}

http://caniuse.com/#search=btoa and http://caniuse.com/#search=atob shows it is hugely supported by the modern browsers

Example taken from https://developer.mozilla.org/en-US/docs/Web/API/window.btoa

answered Aug 21, 2014 at 9:56

Javascript hash function built in

Dipesh KCDipesh KC

3,1613 gold badges30 silver badges50 bronze badges

2

Not the answer you're looking for? Browse other questions tagged javascript hash or ask your own question.

Does JavaScript have a built in hash function?

JavaScript hash() function | Explained. In computer programming, the data structure is used to store, organise, and utilise the data. Javascript hash() function converts the input strings into fixed-sized data (numbers) and returns the hash value.

What JavaScript library provides a hashing function?

jshashes is lightweight library implementing the most extended cryptographic hash function algorithms in pure JavaScript (ES5 compliant).

Is JavaScript object HashTable?

A JavaScript Object is an example of a Hash Table because data is represented a key/value pairs. A hashing function can be used to map the key to an index by taking an input of any size and returning a hash code identifier of a fixed size.

What is hashing in JS?

Hashing is a technique to convert a range of key values into a range of indexes of an array. We're going to use modulo operator to get a range of key values. Consider an example of a hash table of size 20, and the following items are to be stored. Item is in the (key, value) format.