Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 328
W.E.B-Developer Avatar asked Jan 18 '19 14:01

W.E.B-Developer


1 Answers

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.

like image 52
Louis Wasserman Avatar answered Nov 20 '22 03:11

Louis Wasserman