简体   繁体   中英

is there a javascript counterpart to php's native hash() function?

I am converting a php script to java script and all is well except I can't find a simple js function that takes a string and outputs a SHA-512 hash hex string like php's hash() function.

This is a really corny thing that takes user's typed in text and uses the hash to generate "random art" so I am not really concerned with the security aspects of the hash function

I found:

 crypto.subtle.digest('SHA-512', stringvar);

but that doesn't produce the nice hex string I expected. I found a further loop to convert the digest to a hex string but it's ugly and I was hoping there was just a simple function I am missing.

Thanks!

不,没有本机解决方案,只有简单的Google研究指向cryptoJS或此链接

This is the Javascript implementation of Java's String.hashCode() method

String.prototype.hashCode = function() {
  var hash = 0, i, chr;
  if (this.length === 0) return hash;
  for (i = 0; i < this.length; i++) {
    chr   = this.charCodeAt(i);
    hash  = ((hash << 5) - hash) + chr;
    hash |= 0; // Convert to 32bit integer
  }
 return hash;
};

But if that doesn't help you, you have this library that hashes with SHA-512, SHA-384, SHA-512/224, SHA-512/256 in JS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM