admin管理员组文章数量:1419168
I am looking for a way to covert a given string into an alphanumeric hash. The code will be executed on the client-side and must be entirely in vanilla JS or jQuery at the most.
Is there, however, also a non-cryptographic hash, i.e. just a string of alphanumerics that does not require crypto
and Promises
? I need both, i.e. a cryptographic as well as a non-cryptographic hash.
The second hash can be an ordinary string of alphanumerics, say 10 characters long. It should be recoverable, i.e. the same hash should be recreated always for a given string. It would be better if this second hash is not generated asynchronously (i.e. using Promises
). I intend to use it as a key for a boolean
in window.localStorage
(for many different strings).
Final answers:
Crypto JS
bcrypt.js
Fast low-collision non-crypto hash in JavaScript for Files
I am looking for a way to covert a given string into an alphanumeric hash. The code will be executed on the client-side and must be entirely in vanilla JS or jQuery at the most.
Is there, however, also a non-cryptographic hash, i.e. just a string of alphanumerics that does not require crypto
and Promises
? I need both, i.e. a cryptographic as well as a non-cryptographic hash.
The second hash can be an ordinary string of alphanumerics, say 10 characters long. It should be recoverable, i.e. the same hash should be recreated always for a given string. It would be better if this second hash is not generated asynchronously (i.e. using Promises
). I intend to use it as a key for a boolean
in window.localStorage
(for many different strings).
Final answers:
Crypto JS
bcrypt.js
Fast low-collision non-crypto hash in JavaScript for Files
- There's a built-in object: developer.mozilla/en-US/docs/Web/API/SubtleCrypto – Teemu Commented Apr 26, 2018 at 12:12
-
Something like
window.crypto.subtle.digest("SHA-256", new TextEncoder().encode("my string"))
should do. – Evk Commented Apr 26, 2018 at 12:15 - Note that according to that MDN link, Crypto is only guaranteed for ie11+. – Someone Commented Apr 26, 2018 at 12:16
- @Evk Do you mind using the answer box? ;) – Patrick Hofman Commented Apr 26, 2018 at 12:16
-
Does this require encryption or does mere encoding work? base64 is fairly simple via
btoa(string)
and available in IE10. – Fissure King Commented Apr 26, 2018 at 12:18
1 Answer
Reset to default 5Modern browsers provide cryptographic algorithms implementation via window.crypto
object. You can look at what "modern" means in this case by this link (at the bottom). If you are fine with supported browsers list, then you can reach your goal for example like this:
async function hash(target){
var buffer = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(target));
var chars = Array.prototype.map.call(new Uint8Array(buffer), ch => String.fromCharCode(ch)).join('');
return btoa(chars);
};
It will hash your string (bytes of its utf-8 encoding) with SHA-256 and then convert result to base64.
Note that if you don't need cryptographically strong hash (you didn't clarify the purpose) - then there might be better (faster) alternatives.
本文标签: algorithmAlphanumeric hash function in plain JavascriptStack Overflow
版权声明:本文标题:algorithm - Alphanumeric hash function in plain Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745304600a2652569.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论