Is there a way to get the HashCode of an InputStream in Java,
I am trying to upload a picture using the <p:fileUpload/> from PrimeFaces, converting it into a HashCode and comparing it to another picture.
At the moment I'm trying this:
public void save(FileUploadEvent event) throws IOException {
HashCode hashCode = null;
HashCode hashCodeCompare = null;
hashCode = Files.asByteSource(new File(event.toString())).hash(Hashing.murmur3_128(50));
hashCodeCompare = Files.asByteSource(new File(FilePathOfFileToCompare)).hash(Hashing.murmur3_128(50));
boolean hashTrueFalse;
if(hashCode.equals(hashCodeCompare)) {
System.out.println("true");
} else{
System.out.println("false");
}
try (InputStream input = event.getFile().getInputstream()) {
String imageName = generateFileName() + "." + fileExtensions(event.getFile().getFileName());
String imageLink = PICTURE_DESTINATION + "\\" + imageName;
Picture picture = new Picture();
picture.setPictureUrl(imageLink);
pictureService.createOrUpdate(picture);
personForm.getCurrentPersonDTO().setPictureDTO(pictureMapper.toDTO(picture));
} catch (IOException e) {
e.printStackTrace();
}
}
Is there any way to turn the InputStream into a hashcode?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With