junxi
junxi

Reputation: 165

JavaScript equivalent to Guava's HashCode?

In Java, I get a hashCode using Google Guava:

HashFunction hashFunction = Hashing.md5();
Hasher hasher = hashFunction.newHasher();
hasher.putLong(arg);
HashCode hashCode = hasher.hash();
long asLong = hashCode.asLong();

Is there an equivalent to this in JavaScript?

Upvotes: 0

Views: 514

Answers (2)

Ryan Hanekamp
Ryan Hanekamp

Reputation: 628

Explicitly for MD5 hashes, then there is no NATIVE equivalent in Javascript. MD5 is weak enough to have been deprecated out of the SubtleCrypto library. SHA1 is still supported, but has also been cracked recently, so I would use SHA-2 (SHA-256, SHA-384, and SHA-512 are different sizes of this same algorithm, which can be confusing -- they're all SHA-2) for any security-related projects. If you just want this as a quick way to determine that one lump of data is different than another, then SHA-1 is sufficient.

Here's Mozilla's writeup of the SubtleCrypto.digest method to do this: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest

Upvotes: 1

Mindaugas Bernatavičius
Mindaugas Bernatavičius

Reputation: 3909

Google Guava is collection of libraries i.e. packaged extensions to the core language. There are custom implementations of the MD5 and other hash functions in JavaScript and most other languages. For an example in Javascript: http://pajhome.org.uk/crypt/md5/md5.html

So you just need to include them and you can use them in Javascript.

Upvotes: 0

Related Questions