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

32,118

Solution 1

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.

Solution 2

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.

Solution 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("");
}
Share:
32,118
rsk82
Author by

rsk82

Updated on December 06, 2021

Comments

  • rsk82
    rsk82 over 2 years

    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();
    
  • rsk82
    rsk82 over 12 years
    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 ?
  • Greg Guida
    Greg Guida over 12 years
    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
  • ecmanaut
    ecmanaut over 8 years
    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.
  • scubadivingfool
    scubadivingfool about 6 years
    Use the digest() function to get a hash of an array buffer. developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
  • mit
    mit almost 5 years
    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
  • Mike 'Pomax' Kamermans
    Mike 'Pomax' Kamermans almost 3 years
    @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 =)
  • AgainPsychoX
    AgainPsychoX about 2 years
    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).