SW360AttachmentUtils { public static String calculateHash(Path file, MessageDigest digest) { try (DigestInputStream din = new DigestInputStream(Files.newInputStream(file), digest)) { IOUtils.copy(din, NullOutputStream.NULL_OUTPUT_STREAM); } catch (IOException e) { throw new SW360ClientException("Could not calculate hash for file " + file, e); } return toHexString(digest.digest()); } private  SW360AttachmentUtils(); static CompletableFuture<AttachmentUploadResult<T>> uploadAttachments(SW360AttachmentAwareClient<T> client, AttachmentUploadRequest<T> uploadRequest,
                      Function<? super T, Set<SW360SparseAttachment>> getAttachmentsFunc); static CompletableFuture<Optional<Path>> downloadAttachment(SW360AttachmentAwareClient<? extends T> client, T entity, SW360SparseAttachment attachment,
                       Path downloadPath); static AttachmentDownloadProcessor defaultAttachmentDownloadProcessor(SW360SparseAttachment attachment,
                                                                                 Path downloadPath); static AttachmentDownloadProcessor defaultAttachmentDownloadProcessor(String fileName, Path downloadPath); static String calculateHash(Path file, MessageDigest digest); static String calculateSha1Hash(Path file);  }
@Test public void testCalculateHash() throws NoSuchAlgorithmException, URISyntaxException { final String expectedMd5 = "457b4178ee4d1129603a1a6465c8e9c1"; MessageDigest digest = MessageDigest.getInstance("md5"); String hash = SW360AttachmentUtils.calculateHash(testFile(), digest); assertThat(hash).isEqualTo(expectedMd5); }
@Test(expected = SW360ClientException.class) public void testCalculateHashException() throws NoSuchAlgorithmException { Path file = Paths.get("non/existing/file.txt"); MessageDigest digest = MessageDigest.getInstance("md5"); SW360AttachmentUtils.calculateHash(file, digest); }