Is there any way to get the hashcode of an InputStream using Guava?

What you want to do is ByteStreams.copy(input, Funnels.asOutputStream(hasher)) where hasher is acquired from e.g. Hashing.sha256().newHasher(). Then, call hasher.hash() to get the resulting HashCode.


You have to read the InputStream if you are going to calculate a hash on the bytes it contains. First read the InputSteam to a byte[].

With Guava use ByteStreams:

InputStream in = ...;
byte[] bytes = ByteStreams.toByteArray(in);

An alternative popular way to do this is to use Commons IO:

InputStream in = ...;
byte[] bytes = IOUtils.toByteArray(in);

Then you can call Arrays.hashCode() on the byte array:

int hash = java.util.Arrays.hashCode(bytes);

However you might consider using SHA256 as your hash function instead as you are less likely to have a collision:

MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] sha256Hash = digest.digest(bytes);

If you don't want to read the entire stream to an in memory byte array you can calculate the hash as the InputStream is being read by someone else. For example you might want to stream the InputStream to disk to into a db. Guava provides a class that wraps an InputStream that does this for you HashingInputStream:

First wrap your InputStream with a HashinInputStream

HashingInputStream hin = new HashingInputStream(Hashing.sha256(), in);

Then let that HashingInputStream be read in any way you like

while(hin.read() != -1);

Then get the hash from the HashingInputStream

byte[] sha256Hash = hin.hash().asBytes();