@Test public void test_simple_sha256_calculation_works() throws Exception{ SharedKernelTestFileSupport testfileSupport = SharedKernelTestFileSupport.getTestfileSupport(); String absolutePath = testfileSupport.createFileFromResourcePath("zipfile_contains_only_test1.txt.zip").getAbsolutePath(); String checksum = serviceToTest.createChecksum(absolutePath); assertEquals("59060b6b4e8d137596dc01ec15d5da1ab4c4ad0d756c780ed88225f082ae87b7",checksum); assertEquals("59060b6b4e8d137596dc01ec15d5da1ab4c4ad0d756c780ed88225f082ae87b7",checksum); }
public String createChecksum(String filepath){ notNull(filepath, "filepath may not be null"); MessageDigest md; String algorithm = "SHA-256"; try { md = MessageDigest.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("Algorithm not supported:"+algorithm); } try (DigestInputStream dis = new DigestInputStream(new FileInputStream(filepath), md)) { while (dis.read() != -1) ; md = dis.getMessageDigest(); }catch(IOException e) { return null; } StringBuilder result = new StringBuilder(); for (byte b : md.digest()) { result.append(String.format("%02x", b)); } return result.toString(); }
FileChecksumSHA256Service { public String createChecksum(String filepath){ notNull(filepath, "filepath may not be null"); MessageDigest md; String algorithm = "SHA-256"; try { md = MessageDigest.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("Algorithm not supported:"+algorithm); } try (DigestInputStream dis = new DigestInputStream(new FileInputStream(filepath), md)) { while (dis.read() != -1) ; md = dis.getMessageDigest(); }catch(IOException e) { return null; } StringBuilder result = new StringBuilder(); for (byte b : md.digest()) { result.append(String.format("%02x", b)); } return result.toString(); } }
FileChecksumSHA256Service { public String createChecksum(String filepath){ notNull(filepath, "filepath may not be null"); MessageDigest md; String algorithm = "SHA-256"; try { md = MessageDigest.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("Algorithm not supported:"+algorithm); } try (DigestInputStream dis = new DigestInputStream(new FileInputStream(filepath), md)) { while (dis.read() != -1) ; md = dis.getMessageDigest(); }catch(IOException e) { return null; } StringBuilder result = new StringBuilder(); for (byte b : md.digest()) { result.append(String.format("%02x", b)); } return result.toString(); }  }
FileChecksumSHA256Service { public String createChecksum(String filepath){ notNull(filepath, "filepath may not be null"); MessageDigest md; String algorithm = "SHA-256"; try { md = MessageDigest.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("Algorithm not supported:"+algorithm); } try (DigestInputStream dis = new DigestInputStream(new FileInputStream(filepath), md)) { while (dis.read() != -1) ; md = dis.getMessageDigest(); }catch(IOException e) { return null; } StringBuilder result = new StringBuilder(); for (byte b : md.digest()) { result.append(String.format("%02x", b)); } return result.toString(); }  boolean hasCorrectChecksum(String checkSum, String filepath); String createChecksum(String filepath); }
FileChecksumSHA256Service { public String createChecksum(String filepath){ notNull(filepath, "filepath may not be null"); MessageDigest md; String algorithm = "SHA-256"; try { md = MessageDigest.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("Algorithm not supported:"+algorithm); } try (DigestInputStream dis = new DigestInputStream(new FileInputStream(filepath), md)) { while (dis.read() != -1) ; md = dis.getMessageDigest(); }catch(IOException e) { return null; } StringBuilder result = new StringBuilder(); for (byte b : md.digest()) { result.append(String.format("%02x", b)); } return result.toString(); }  boolean hasCorrectChecksum(String checkSum, String filepath); String createChecksum(String filepath);  }