Skip to content

Commit

Permalink
🍻 Basic CheckSum program without
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jan 3, 2022
1 parent ca03145 commit 514e216
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@
</licenses>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<artifactsDir>target</artifactsDir>
<executable-suffix/>
</properties>

</project>
33 changes: 33 additions & 0 deletions src/main/java/org/rrajesh1979/demo/CheckSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.rrajesh1979.demo;

import java.io.File;
import java.math.BigInteger;
import java.io.IOException;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class CheckSum {
static String algorithm = "MD5";

public static void main(String[] args) {
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));
} catch (IOException | NoSuchAlgorithmException e) {
e.printStackTrace();
}

}

public static byte[] 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;
}
}
1 change: 1 addition & 0 deletions src/main/resources/testdata/TestFile-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!

0 comments on commit 514e216

Please sign in to comment.