Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashing issue between guava versions

Tags:

hash

sha256

guava

I was using guava 14 to do String hashing like so:

Hashing.sha256().newHasher().putString("String").hash().toString();

=>

4d1ca6dce72e20ce214b706168340683bb6b571a7c977c1a9fe029a1cc1c4d06

just upgraded to guava16,

calling this function: Hashing.sha256().newHasher().putString("String", Charsets.UTF-8).hash().toString() gives me a different result.

=>

b2ef230e7f4f315a28cdcc863028da31f7110f3209feb76e76fed0f37b3d8580

I suspect that the old version was using default charset, but switching Charsets on guava16 doesn't give me the same result as in guava14. What did I do wrong here?

like image 305
user1402784 Avatar asked Feb 14 '23 00:02

user1402784


2 Answers

As stated in the docs of Guava 15, the replacement for the old putString(String) method is putUnencodedChars.

like image 116
Louis Wasserman Avatar answered Mar 07 '23 13:03

Louis Wasserman


As Louis said, the replacement is Hasher.putUnencodedChars(). Or, you can use the shortcuts on the HashFunction interface:

Hashing.sha256().hashUnencodedChars("String").toString();
like image 24
Kurt Alfred Kluever Avatar answered Mar 07 '23 13:03

Kurt Alfred Kluever