13
13
import java .security .NoSuchAlgorithmException ;
14
14
15
15
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
+
17
21
private static final int HASH_MD5_FILE_BUFFER_SIZE = 4096 ;
18
22
19
23
private static HashUtils ourInstance = new HashUtils ();
@@ -27,18 +31,119 @@ private HashUtils() {
27
31
28
32
@ NotNull
29
33
public String md5DigestString (@ NotNull String string ) throws NoSuchAlgorithmException {
30
- final MessageDigest messageDigest = MessageDigest .getInstance (HASH_MD5 );
34
+ final MessageDigest messageDigest = MessageDigest .getInstance (ALGORITHM_MD5 );
31
35
final byte [] digestBytes = messageDigest .digest (string .getBytes (StandardCharsets .UTF_8 ));
32
36
return DatatypeConverter .printHexBinary (digestBytes ).toLowerCase ();
33
37
}
34
38
35
39
@ NotNull
36
40
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 ());
38
142
39
143
try (InputStream is = Files .newInputStream (Paths .get (filePath ));
40
144
DigestInputStream digestInputStream = new DigestInputStream (is , messageDigest )) {
41
145
byte [] buffer = new byte [HASH_MD5_FILE_BUFFER_SIZE ];
146
+ //noinspection StatementWithEmptyBody
42
147
while (digestInputStream .read (buffer ) > 0 ) {
43
148
}
44
149
} catch (IOException e ) {
0 commit comments