Skip to content

Commit

Permalink
📝 Refactored CheckSum
Browse files Browse the repository at this point in the history
Added logging and lombok support
  • Loading branch information
github-actions[bot] committed Jan 4, 2022
1 parent d792e8f commit db409ac
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@

<spock.version>2.1-M2-groovy-3.0</spock.version>
<junit.version>5.8.2</junit.version>

<logback-classic.version>1.2.7</logback-classic.version>
<lombok.version>1.18.22</lombok.version>
</properties>

<dependencies>
Expand All @@ -70,6 +73,20 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback-classic.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>compile</scope>
</dependency>


<!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy -->
<!-- <dependency>-->
<!-- <groupId>org.codehaus.groovy</groupId>-->
Expand Down Expand Up @@ -549,6 +566,7 @@
<imageName>mychecksum-${project.version}</imageName>
<mainClass>org.rrajesh1979.demo.MyCheckSum</mainClass>
<buildArgs>
<buildArg>--allow-incomplete-classpath</buildArg>
<buildArg>--no-fallback</buildArg>
<buildArg>-H:+ReportExceptionStackTraces</buildArg>
<buildArg>--verbose</buildArg>
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/org/rrajesh1979/demo/MyCheckSum.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,38 @@
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();
}

}

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;
}
}
5 changes: 2 additions & 3 deletions src/test/java/org/rrajesh1979/demo/MyCheckSumTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down

0 comments on commit db409ac

Please sign in to comment.