Skip to content

Commit a7f5fc5

Browse files
committed
Add more Hash Algorithms
- Add HashAlgorithms Enum - Add more HashUtilsTests: SHA1, SHA256, SHA512 - add hash.txt file
1 parent 8a3ace2 commit a7f5fc5

File tree

5 files changed

+253
-5
lines changed

5 files changed

+253
-5
lines changed

.circleci/config.yml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ jobs:
77
- master
88
docker:
99
- image: circleci/openjdk:8-jdk
10+
# - image: circleci/openjdk:11-jdk
1011
# - image: circleci/openjdk:8-jdk-browsers
1112
steps:
1213
- checkout
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package co.tunjos.crypto.hash;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
public enum HashAlgorithms {
6+
MD5("MD5"),
7+
8+
SHA1("SHA-1"),
9+
10+
SHA2224("SHA-224"),
11+
SHA256("SHA-256"),
12+
SHA384("SHA-384"),
13+
SHA512("SHA-512"),
14+
15+
// Only Available from JDK 9+
16+
SHA512_224("SHA-512/224"),
17+
SHA512_256("SHA-512/256"),
18+
19+
// Only Available from JDK 9+
20+
SHA3_224("SHA3-224"),
21+
SHA3_256("SHA3-256"),
22+
SHA3_384("SHA3-384"),
23+
SHA3_512("SHA3-512");
24+
25+
private final String algorithm;
26+
27+
HashAlgorithms(@NotNull final String algorithm) {
28+
this.algorithm = algorithm;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return algorithm;
34+
}
35+
}

src/main/java/co/tunjos/crypto/hash/HashUtils.java

+108-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
import java.security.NoSuchAlgorithmException;
1414

1515
public class HashUtils {
16-
private static final String HASH_MD5 = "MD5";
16+
private static final String ALGORITHM_MD5 = "MD5";
17+
private static final String ALGORITHM_SHA1 = "SHA-1";
18+
private static final String ALGORITHM_SHA256 = "SHA-256";
19+
private static final String ALGORITHM_SHA512 = "SHA-512";
20+
1721
private static final int HASH_MD5_FILE_BUFFER_SIZE = 4096;
1822

1923
private static HashUtils ourInstance = new HashUtils();
@@ -27,18 +31,119 @@ private HashUtils() {
2731

2832
@NotNull
2933
public String md5DigestString(@NotNull String string) throws NoSuchAlgorithmException {
30-
final MessageDigest messageDigest = MessageDigest.getInstance(HASH_MD5);
34+
final MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM_MD5);
3135
final byte[] digestBytes = messageDigest.digest(string.getBytes(StandardCharsets.UTF_8));
3236
return DatatypeConverter.printHexBinary(digestBytes).toLowerCase();
3337
}
3438

3539
@NotNull
3640
public String md5DigestFile(@NotNull String filePath) throws NoSuchAlgorithmException, IOException {
37-
final MessageDigest messageDigest = MessageDigest.getInstance(HASH_MD5);
41+
final MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM_MD5);
42+
43+
try (InputStream is = Files.newInputStream(Paths.get(filePath));
44+
DigestInputStream digestInputStream = new DigestInputStream(is, messageDigest)) {
45+
byte[] buffer = new byte[HASH_MD5_FILE_BUFFER_SIZE];
46+
//noinspection StatementWithEmptyBody
47+
while (digestInputStream.read(buffer) > 0) {
48+
}
49+
} catch (IOException e) {
50+
throw e;
51+
}
52+
53+
byte[] digestBytes = messageDigest.digest();
54+
return DatatypeConverter.printHexBinary(digestBytes).toLowerCase();
55+
}
56+
57+
@NotNull
58+
public String sha1DigestString(@NotNull String string) throws NoSuchAlgorithmException {
59+
final MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM_SHA1);
60+
final byte[] digestBytes = messageDigest.digest(string.getBytes(StandardCharsets.UTF_8));
61+
return DatatypeConverter.printHexBinary(digestBytes).toLowerCase();
62+
}
63+
64+
@NotNull
65+
public String sha1DigestFile(@NotNull String filePath) throws NoSuchAlgorithmException, IOException {
66+
final MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM_SHA1);
67+
68+
try (InputStream is = Files.newInputStream(Paths.get(filePath));
69+
DigestInputStream digestInputStream = new DigestInputStream(is, messageDigest)) {
70+
byte[] buffer = new byte[HASH_MD5_FILE_BUFFER_SIZE];
71+
//noinspection StatementWithEmptyBody
72+
while (digestInputStream.read(buffer) > 0) {
73+
}
74+
} catch (IOException e) {
75+
throw e;
76+
}
77+
78+
byte[] digestBytes = messageDigest.digest();
79+
return DatatypeConverter.printHexBinary(digestBytes).toLowerCase();
80+
}
81+
82+
@NotNull
83+
public String sha256DigestString(@NotNull String string) throws NoSuchAlgorithmException {
84+
final MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM_SHA256);
85+
final byte[] digestBytes = messageDigest.digest(string.getBytes(StandardCharsets.UTF_8));
86+
return DatatypeConverter.printHexBinary(digestBytes).toLowerCase();
87+
}
88+
89+
@NotNull
90+
public String sha256DigestFile(@NotNull String filePath) throws NoSuchAlgorithmException, IOException {
91+
final MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM_SHA256);
92+
93+
try (InputStream is = Files.newInputStream(Paths.get(filePath));
94+
DigestInputStream digestInputStream = new DigestInputStream(is, messageDigest)) {
95+
byte[] buffer = new byte[HASH_MD5_FILE_BUFFER_SIZE];
96+
//noinspection StatementWithEmptyBody
97+
while (digestInputStream.read(buffer) > 0) {
98+
}
99+
} catch (IOException e) {
100+
throw e;
101+
}
102+
103+
byte[] digestBytes = messageDigest.digest();
104+
return DatatypeConverter.printHexBinary(digestBytes).toLowerCase();
105+
}
106+
107+
@NotNull
108+
public String sha512DigestString(@NotNull String string) throws NoSuchAlgorithmException {
109+
final MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM_SHA512);
110+
final byte[] digestBytes = messageDigest.digest(string.getBytes(StandardCharsets.UTF_8));
111+
return DatatypeConverter.printHexBinary(digestBytes).toLowerCase();
112+
}
113+
114+
@NotNull
115+
public String sha512DigestFile(@NotNull String filePath) throws NoSuchAlgorithmException, IOException {
116+
final MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM_SHA512);
117+
118+
try (InputStream is = Files.newInputStream(Paths.get(filePath));
119+
DigestInputStream digestInputStream = new DigestInputStream(is, messageDigest)) {
120+
byte[] buffer = new byte[HASH_MD5_FILE_BUFFER_SIZE];
121+
//noinspection StatementWithEmptyBody
122+
while (digestInputStream.read(buffer) > 0) {
123+
}
124+
} catch (IOException e) {
125+
throw e;
126+
}
127+
128+
byte[] digestBytes = messageDigest.digest();
129+
return DatatypeConverter.printHexBinary(digestBytes).toLowerCase();
130+
}
131+
132+
@NotNull
133+
public String shaDigestString(@NotNull String string, @NotNull HashAlgorithms algorithm) throws NoSuchAlgorithmException {
134+
final MessageDigest messageDigest = MessageDigest.getInstance(algorithm.toString());
135+
final byte[] digestBytes = messageDigest.digest(string.getBytes(StandardCharsets.UTF_8));
136+
return DatatypeConverter.printHexBinary(digestBytes).toLowerCase();
137+
}
138+
139+
@NotNull
140+
public String shaDigestFile(@NotNull String filePath, @NotNull HashAlgorithms algorithm) throws NoSuchAlgorithmException, IOException {
141+
final MessageDigest messageDigest = MessageDigest.getInstance(algorithm.toString());
38142

39143
try (InputStream is = Files.newInputStream(Paths.get(filePath));
40144
DigestInputStream digestInputStream = new DigestInputStream(is, messageDigest)) {
41145
byte[] buffer = new byte[HASH_MD5_FILE_BUFFER_SIZE];
146+
//noinspection StatementWithEmptyBody
42147
while (digestInputStream.read(buffer) > 0) {
43148
}
44149
} catch (IOException e) {

src/test/java/co/tunjos/crypto/hash/HashUtilsTest.java

+103-2
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,119 @@ void md5DigestString() {
2727

2828
@Test
2929
void md5DigestFile() {
30-
final String test = "src/test/resources/test.txt";
30+
final String filepath = "src/test/resources/test.txt";
3131
HashUtils hashUtils = HashUtils.getInstance();
3232

3333
final String hash;
3434

3535
try {
36-
hash = hashUtils.md5DigestFile(test);
36+
hash = hashUtils.md5DigestFile(filepath);
3737
} catch (NoSuchAlgorithmException | IOException e) {
3838
e.printStackTrace();
3939
assert false;
4040
return;
4141
}
4242
assertEquals("d8e8fca2dc0f896fd7cb4cb0031ba249", hash);
4343
}
44+
45+
@Test
46+
void sha1DigestString() {
47+
final String test = "test";
48+
HashUtils hashUtils = HashUtils.getInstance();
49+
50+
final String hash;
51+
try {
52+
hash = hashUtils.sha1DigestString(test);
53+
} catch (NoSuchAlgorithmException e) {
54+
e.printStackTrace();
55+
assert false;
56+
return;
57+
}
58+
assertEquals("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", hash);
59+
}
60+
61+
@Test
62+
void sha1DigestFile() {
63+
final String filepath = "src/test/resources/test.txt";
64+
HashUtils hashUtils = HashUtils.getInstance();
65+
66+
final String hash;
67+
68+
try {
69+
hash = hashUtils.sha1DigestFile(filepath);
70+
} catch (NoSuchAlgorithmException | IOException e) {
71+
e.printStackTrace();
72+
assert false;
73+
return;
74+
}
75+
assertEquals("4e1243bd22c66e76c2ba9eddc1f91394e57f9f83", hash);
76+
}
77+
78+
@Test
79+
void sha256DigestString() {
80+
final String test = "test";
81+
HashUtils hashUtils = HashUtils.getInstance();
82+
83+
final String hash;
84+
try {
85+
hash = hashUtils.sha256DigestString(test);
86+
} catch (NoSuchAlgorithmException e) {
87+
e.printStackTrace();
88+
assert false;
89+
return;
90+
}
91+
assertEquals("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", hash);
92+
}
93+
94+
@Test
95+
void sha256DigestFile() {
96+
final String filepath = "src/test/resources/test.txt";
97+
HashUtils hashUtils = HashUtils.getInstance();
98+
99+
final String hash;
100+
101+
try {
102+
hash = hashUtils.sha256DigestFile(filepath);
103+
} catch (NoSuchAlgorithmException | IOException e) {
104+
e.printStackTrace();
105+
assert false;
106+
return;
107+
}
108+
assertEquals("f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2", hash);
109+
}
110+
111+
@Test
112+
void sha512DigestString() {
113+
final String test = "test";
114+
HashUtils hashUtils = HashUtils.getInstance();
115+
116+
final String hash;
117+
try {
118+
hash = hashUtils.sha512DigestString(test);
119+
} catch (NoSuchAlgorithmException e) {
120+
e.printStackTrace();
121+
assert false;
122+
return;
123+
}
124+
assertEquals("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887" +
125+
"fd67b143732c304cc5fa9ad8e6f57f50028a8ff", hash);
126+
}
127+
128+
@Test
129+
void sha512DigestFile() {
130+
final String filepath = "src/test/resources/test.txt";
131+
HashUtils hashUtils = HashUtils.getInstance();
132+
133+
final String hash;
134+
135+
try {
136+
hash = hashUtils.sha512DigestFile(filepath);
137+
} catch (NoSuchAlgorithmException | IOException e) {
138+
e.printStackTrace();
139+
assert false;
140+
return;
141+
}
142+
assertEquals("0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea125dc5e92be17c" +
143+
"98f9a0f85ca9d5f595db2012f7cc3571945c123", hash);
144+
}
44145
}

src/test/resources/hash.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
test.txt
2+
3+
MD5:d8e8fca2dc0f896fd7cb4cb0031ba249
4+
SHA1:4e1243bd22c66e76c2ba9eddc1f91394e57f9f83
5+
SHA256:f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2
6+
SHA512:0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea125dc5e92be17c98f9a0f85ca9d5f595db2012f7cc3571945c123

0 commit comments

Comments
 (0)