原生 JavaScript 中是否有可用的单向哈希函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5111164/
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-23 15:42:01  来源:igfitidea点击:

Are there any one-way hashing functions available in native JavaScript?

javascripthash

提问by buley

I'd like to be able to create unique tokens* for users based on a hashed string. I know I could, for example, use a md5() library but as the purpose is not cryptographic I was wondering if there was anything I could use "out of the box." Are there any one-way hashing functions available in native JavaScript?

我希望能够基于散列字符串为用户创建唯一的令牌*。例如,我知道我可以使用 md5() 库,但由于目的不是加密,我想知道是否有任何我可以“开箱即用”的东西。原生 JavaScript 中是否有可用的单向哈希函数?

*I realize these won't be strictly unique. I'm ok with a small chance of hashing collision.

*我意识到这些不会是严格唯一的。我没问题,散列冲突的可能性很小。

采纳答案by Kae Verens

not that I'm aware of.

不是我知道的。

however, you could always use a JavaScript implementation of MD5

但是,您始终可以使用 MD5 的 JavaScript 实现

http://pajhome.org.uk/crypt/md5/

http://pajhome.org.uk/crypt/md5/

回答by Tero Niemi

JavaScript does not have native hashing, but there are many libraries.

JavaScript 没有原生散列,但有很多库。

I recommend crypto-js: https://code.google.com/p/crypto-js/

我推荐加密jshttps: //code.google.com/p/crypto-js/

For example, to use SHA1, you simply:

例如,要使用 SHA1,您只需:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/sha1.js"></script>
<script>
    var hash = CryptoJS.SHA1("Message");
</script>

回答by Tim Down

Nothing is available in native JavaScript. You could use something like Murmurhash. There's a JavaScript implementation here: https://github.com/garycourt/murmurhash-js. I haven't used it though so can't vouch for it.

本机 JavaScript 中没有可用的内容。你可以使用像Murmurhash这样的东西。这里有一个 JavaScript 实现:https: //github.com/garycourt/murmurhash-js。不过我没用过所以不能保证。

Update: now there are multiple Murmurhash3 implementations available in JavaScript. However, many of them have problems encoding strings to bytes and can produce different results compared to the reference C++ implementation. You can read an analysis on this here, the murmurhash3js-revisitedlibrary implements all three variants of the function and conforms to the reference.

更新:现在 JavaScript 中有多个 Murmurhash3 实现可用。但是,与参考 C++ 实现相比,它们中的许多都存在将字符串编码为字节的问题,并且会产生不同的结果。您可以在此处阅读对此的分析murmurhash3js-revisited库实现了该函数的所有三个变体并符合参考。

回答by Adam Grant

Over the horizon, this may be possible with the currently experimental Web Crypto API

在未来,这可能通过当前实验性的 Web Crypto API 实现

https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API

https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API

https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto

https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto

Granted, at the time of this writing it is unrealistic to use in a production environment and will likely be a moving target. However, come 5 years who knows?

诚然,在撰写本文时,在生产环境中使用它是不切实际的,并且可能是一个移动目标。然而,五年后谁知道呢?