Chintan
Chintan

Reputation: 81

Is Guava's HashFunction threadsafe?

Is HashFunction in Guava library Threadsafe?

static HashFunction hashFunction = Hashing.sha256();

private static String getHashCredentials(String String) {
    return hashFunction.newHasher()
        .putString(String, Charsets.UTF_8).hash()
        .toString();
}

Upvotes: 1

Views: 950

Answers (1)

Grzegorz Rożniecki
Grzegorz Rożniecki

Reputation: 28015

Yes, if you're using built-in HashFunctions, they're pure function -- see the documentation page for HashFunction:

A hash function is a collision-averse pure function that maps an arbitrary block of data to a number called a hash code.

Unpacking this definition:

  • (...)
  • pure function: the value produced must depend only on the input bytes, in the order they appear. Input data is never modified. HashFunction instances should always be stateless, and therefore thread-safe.

Bear in mind that because HashFunction is an interface, you could create stateful and non-thread-safe implementation, but it would break the contract.

Upvotes: 3

Related Questions