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

前端 未结 3 1947
無奈伤痛
無奈伤痛 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 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.

提交回复
热议问题