algorithm - Alphanumeric hash function in plain Javascript - Stack Overflow

I am looking for a way to covert a given string into an alphanumeric hash. The code will be executed on

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

Share Improve this question edited May 9, 2018 at 7:15 Yash Sampat asked Apr 26, 2018 at 12:09 Yash SampatYash Sampat 30.6k12 gold badges99 silver badges122 bronze badges 5
  • 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
Add a ment  | 

1 Answer 1

Reset to default 5

Modern 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745304601a4621634.html

相关推荐

  • algorithm - Alphanumeric hash function in plain Javascript - Stack Overflow

    I am looking for a way to covert a given string into an alphanumeric hash. The code will be executed on

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信