在最新的浏览器中是否有任何内置的 javascript 字符串哈希函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8670909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:00:51  来源:igfitidea点击:

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

javascripthash

提问by rsk82

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.

每次出现新版本的浏览器时,我都会听到新的东西被添加,比如 webGL 和其他没有人真正知道它们是否能赶上的技术。

But I wonder if someone ever thought about such basic stuff in JS like hashing functions (MD5,SHA1 and the like).

但我想知道是否有人在 JS 中考虑过像散列函数(MD5、SHA1 等)这样的基本东西。

By newest browsers I mean today's development versions too like Opera 12, Chrome 17 or Firefox 10.

我所说的最新浏览器是指当今的开发版本,例如 Opera 12、Chrome 17 或 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 ?

现在寻找解决方案,我在这里的另一个线程上找到了这条评论:https: //stackoverflow.com/questions/7204097/short-hashing-function-for-javascript你知道 javascript 对象已经是哈希表了吗?)。那么这些“哈希表”是什么?这是否意味着我可以将任何字符串变成散列,但不是像 md5 或 sha1 这样的已建立的字符串,而是一些特定的 JS 构建?

basically what I need to do is:

基本上我需要做的是:

var txt="Hello world!";
var hash = txt.toSha1();

回答by Sam Bull

For anybody still looking for this information. There is a WebCrypto APIwhich appears to have been finalised at the beginning of 2017.

对于仍在寻找此信息的任何人。有一个WebCrypto API似乎已在 2017 年初完成。

To use it in a browser, you can find it at window.crypto.subtlewhich contains methods for encryption, digests etc. Documentation on the available functions here.

要在浏览器中使用它,您可以在window.crypto.subtle其中找到它,其中包含加密、摘要等方法。有关可用功能的文档在这里

回答by Greg Guida

Paul Johnstonhas implemented the following algorithms in javascript

Paul Johnston在 javascript 中实现了以下算法

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

MD5、RIPEMD-160、SHA-1、SHA-256 和 sha-512

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

你可以在这里找到源代码和一些例子:http: //pajhome.org.uk/crypt/md5/

I hope this is what you were looking for.

我希望这就是你要找的。

回答by Dipesh KC

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

当我需要简单,无需外部库,客户端散列我使用内置的浏览器atob()btoa()功能。

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

window.btoa() 从二进制数据的“字符串”创建一个 base-64 编码的 ASCII 字符串。

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.

window.atob() 函数对使用 base-64 编码的数据字符串进行解码。

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

http://caniuse.com/#search=btoaand http://caniuse.com/#search=atobshows it is hugely supported by the modern browsers

http://caniuse.com/#search=btoahttp://caniuse.com/#search=atob表明它受到现代浏览器的大力支持

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

示例取自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.

注意:上述解决方案没有外部库依赖项。如上所述,仅将其用于简单加密。如果您正在寻找安全的加密解决方案,请不要使用它。