From db409ac059ece4845e30326acfa1661279a2deba Mon Sep 17 00:00:00 2001 From: GitHub Action <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 4 Jan 2022 18:21:16 -0500 Subject: [PATCH] :pencil: Refactored CheckSum Added logging and lombok support --- pom.xml | 18 ++++++++++++++++++ .../java/org/rrajesh1979/demo/MyCheckSum.java | 18 ++++++++++++++---- .../org/rrajesh1979/demo/MyCheckSumTest.java | 5 ++--- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 97ce124..74ce6f8 100644 --- a/pom.xml +++ b/pom.xml @@ -59,6 +59,9 @@ 2.1-M2-groovy-3.0 5.8.2 + + 1.2.7 + 1.18.22 @@ -70,6 +73,20 @@ test + + ch.qos.logback + logback-classic + ${logback-classic.version} + + + + org.projectlombok + lombok + ${lombok.version} + compile + + + @@ -549,6 +566,7 @@ mychecksum-${project.version} org.rrajesh1979.demo.MyCheckSum + --allow-incomplete-classpath --no-fallback -H:+ReportExceptionStackTraces --verbose diff --git a/src/main/java/org/rrajesh1979/demo/MyCheckSum.java b/src/main/java/org/rrajesh1979/demo/MyCheckSum.java index 9220f54..3a2967c 100644 --- a/src/main/java/org/rrajesh1979/demo/MyCheckSum.java +++ b/src/main/java/org/rrajesh1979/demo/MyCheckSum.java @@ -23,17 +23,23 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import lombok.extern.slf4j.Slf4j; + +@Slf4j public class MyCheckSum { static String algorithm = "MD5"; public static void main(String[] args) { + log.info("Hello from MyCheckSum"); + String fileName = ""; if (args.length > 0) { fileName = args[0]; } try { - byte[] checkSum = getCheckSum(fileName); - System.out.printf("%0" + (checkSum.length * 2) + "x%n", new BigInteger(1, checkSum)); + String checkSum = getCheckSum(fileName); + // System.out.printf("%0" + (checkSum.length * 2) + "x%n", new BigInteger(1, checkSum)); + log.info("CheckSum of file {} is : {}", fileName, checkSum); } catch (IOException | NoSuchAlgorithmException e) { e.printStackTrace(); @@ -41,10 +47,14 @@ public static void main(String[] args) { } - public static byte[] getCheckSum(String fileName) throws IOException, NoSuchAlgorithmException { + public static String getCheckSum(String fileName) throws IOException, NoSuchAlgorithmException { File file = new File(fileName); byte[] fileContents = Files.readAllBytes(file.toPath()); byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents); - return digest; + + String checkSum; + checkSum = new BigInteger(digest).toString(16); + + return checkSum; } } diff --git a/src/test/java/org/rrajesh1979/demo/MyCheckSumTest.java b/src/test/java/org/rrajesh1979/demo/MyCheckSumTest.java index 8b19723..be8ac83 100644 --- a/src/test/java/org/rrajesh1979/demo/MyCheckSumTest.java +++ b/src/test/java/org/rrajesh1979/demo/MyCheckSumTest.java @@ -16,7 +16,6 @@ package org.rrajesh1979.demo; import java.io.*; -import java.math.BigInteger; import java.security.NoSuchAlgorithmException; import org.junit.jupiter.api.Test; @@ -33,8 +32,8 @@ void main() { void getCheckSum() throws IOException, NoSuchAlgorithmException { File tempFile = createTempDataFile(); - byte[] checkSumBytes = MyCheckSum.getCheckSum(tempFile.getAbsolutePath()); - String calculatedCheckSum = new BigInteger(checkSumBytes).toString(16); + String calculatedCheckSum = MyCheckSum.getCheckSum(tempFile.getAbsolutePath()); + // String calculatedCheckSum = new BigInteger(checkSumBytes).toString(16); tempFile.delete(); String expectedCheckSum = "764efa883dda1e11db47671c4a3bbd9e";