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

前端 未结 3 1945
無奈伤痛
無奈伤痛 2021-02-11 20:02

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 w

相关标签:
3条回答
  • 2021-02-11 20:45

    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.

    0 讨论(0)
  • 2021-02-11 21:00

    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.

    0 讨论(0)
  • 2021-02-11 21:00

    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

    Note: Above solution has no external library dependency. As mentioned above, use this only for simple encryption. If you are looking for a secure cryptographic solution do not use this.

    0 讨论(0)
提交回复
热议问题