55

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();
3
  • 4
    You appear to be confusing hash tables (an object which stores values against named keys, like {name: "bob", dob: "27/1/1970"}) with a hash function (a function for mapping a large data set to a small one, like md5) Commented Dec 29, 2011 at 17:14
  • Yes, now I understand that what I look for has nothing to do with hash tables. Commented Dec 29, 2011 at 17:19
  • crypto.subtle.digest(algorithm, data) Commented Aug 24, 2021 at 16:26

4 Answers 4

57

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.

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

2 Comments

Use the digest() function to get a hash of an array buffer. developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
To get this to work on a string (instead of array buffer), see below...
9
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("");
}

2 Comments

You can also use it in Node environments as crypto.webcrypto.subtle. So, you could write await (crypto.subtle || crypto.webcrypto.subtle).digest(...) (of course, it will throw if if not supported).
this code is taken almost completely from the MDN Docs
6

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.

3 Comments

Well in sort of yes. But my question was about new developments in browsers, they put new function constantly and I wonder if they finally add hashes too. Is it ever planned, or by design must it that be because something is blocking this issue ?
oh, I see, not that I know of in the context of the browser but if you're using node.js there is already a crypto package that handles these kinds of things
@rsk82 you probably want to accept the new WebCrypto answer then. Sure, it's been years, but future visitors are still finding this question looking for the same answer =)
-8

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

2 Comments

This is a useful reversible 1:1 encoding that turns plaintext into less plain, and larger output than the input. Both are opposites properties to those of the one-way cryptographic hash functions sought here.
The op and the titel do not suggest that it has to be cryptographic and secure. The functions that the op mentions are not all secure or unbreakable. He asks for "and the like", and the focus seems on "builtin". This is a valid answer and it is helpful to some. There are many situations a hash value can be helpful where security is not the problem

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.