Skip to content

Commit 346009f

Browse files
committed
CODEC-220 Fluent interface for DigestUtils
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1744467 13f79535-47bb-0310-9956-ffa450edef68
1 parent f054152 commit 346009f

2 files changed

Lines changed: 145 additions & 73 deletions

File tree

src/main/java/org/apache/commons/codec/digest/DigestUtils.java

Lines changed: 141 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
* <pre>
4646
* import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_224;
4747
* ...
48-
* byte [] digest = DigestUtils.digest(SHA_224, dataToDigest);
49-
* byte [] pommed = DigestUtils.digest(SHA_224, new File("pom.xml"));
48+
* byte [] digest = DigestUtils.with(SHA_224).update(dataToDigest).done();
49+
* String hdigest = DigestUtils.with(SHA_224).update(new File("pom.xml")).asHex();
5050
* </pre>
5151
* </code>
5252
* @see MessageDigestAlgorithms
@@ -80,8 +80,7 @@ public static byte[] digest(final MessageDigest messageDigest, final byte[] data
8080
* @param data
8181
* Data to digest
8282
* @return the digest
83-
* @throws IOException
84-
* On error reading from the stream
83+
*
8584
* @since 1.11
8685
*/
8786
public static byte[] digest(final MessageDigest messageDigest, final ByteBuffer data) {
@@ -121,72 +120,6 @@ public static byte[] digest(final MessageDigest messageDigest, final InputStream
121120
return updateDigest(messageDigest, data).digest();
122121
}
123122

124-
/**
125-
* Reads through a byte array and returns the digest for the data.
126-
*
127-
* @param digestName
128-
* The name of the algorithm to use (e.g. MessageDigestAlgoriths.MD5 or "MD5")
129-
* @param data
130-
* Data to digest
131-
* @return the digest
132-
* @throws IOException
133-
* On error reading from the stream
134-
* @since 1.11
135-
*/
136-
public static byte[] digest(final String digestName, final byte[] data) {
137-
return digest(getDigest(digestName), data);
138-
}
139-
140-
/**
141-
* Reads through a ByteBuffer and returns the digest for the data
142-
*
143-
* @param digestName
144-
* The name of the algorithm to use (e.g. MessageDigestAlgoriths.MD5 or "MD5")
145-
* @param data
146-
* Data to digest
147-
* @return the digest
148-
* @throws IOException
149-
* On error reading from the stream
150-
* @since 1.11
151-
*/
152-
public static byte[] digest(final String digestName, final ByteBuffer data) {
153-
MessageDigest messageDigest = getDigest(digestName);
154-
messageDigest .update(data);
155-
return messageDigest.digest();
156-
}
157-
158-
/**
159-
* Reads through a File and returns the digest for the data
160-
*
161-
* @param digestName
162-
* The name of the algorithm to use (e.g. MessageDigestAlgoriths.MD5 or "MD5")
163-
* @param data
164-
* Data to digest
165-
* @return the digest
166-
* @throws IOException
167-
* On error reading from the stream
168-
* @since 1.11
169-
*/
170-
public static byte[] digest(final String digestName, final File data) throws IOException {
171-
return updateDigest(getDigest(digestName), data).digest();
172-
}
173-
174-
/**
175-
* Reads through an InputStream and returns the digest for the data
176-
*
177-
* @param digestName
178-
* The name of the algorithm to use (e.g. MessageDigestAlgoriths.MD5 or "MD5")
179-
* @param data
180-
* Data to digest
181-
* @return the digest
182-
* @throws IOException
183-
* On error reading from the stream
184-
* @since 1.11
185-
*/
186-
public static byte[] digest(final String digestName, final InputStream data) throws IOException {
187-
return updateDigest(getDigest(digestName), data).digest();
188-
}
189-
190123
/**
191124
* Returns a <code>MessageDigest</code> for the given <code>algorithm</code>.
192125
*
@@ -1030,4 +963,142 @@ public static boolean isAvailable(String messageDigestAlgorithm) {
1030963
return getDigest(messageDigestAlgorithm, null) != null;
1031964
}
1032965

966+
// Fluent interface
967+
968+
private final MessageDigest messageDigest;
969+
970+
DigestUtils() {
971+
this.messageDigest = null;
972+
}
973+
974+
private DigestUtils(MessageDigest digest) {
975+
this.messageDigest = digest;
976+
}
977+
978+
/**
979+
* Returns a fluent instance for the digest algorithm.
980+
* Does not reset the digest before use.
981+
* @param digest the digest instance to use
982+
* @return
983+
*/
984+
public static DigestUtils with(MessageDigest digest) {
985+
return new DigestUtils(digest);
986+
}
987+
988+
/**
989+
* Creates a {@link MessageDigest} and returns a fluent instance.
990+
*
991+
* @param name the name of digest algorithm to create, e.g. {@link MessageDigestAlgorithms#MD5}
992+
* @return
993+
*/
994+
public static DigestUtils with(String name) {
995+
return new DigestUtils(getDigest(name));
996+
}
997+
998+
/**
999+
* Returns the message digest instance.
1000+
* @return the message digest instance
1001+
*/
1002+
public MessageDigest getMessageDigest() {
1003+
return messageDigest;
1004+
}
1005+
1006+
/**
1007+
* Completes the hash computation and returns the hash
1008+
* accumulated by one or more invocations of an update method.
1009+
*
1010+
* @return the hash as a byte array
1011+
*
1012+
* @since 1.11
1013+
*/
1014+
public byte[] done() {
1015+
return messageDigest.digest();
1016+
}
1017+
1018+
/**
1019+
* Completes the hash computation and returns the hash
1020+
* accumulated by one or more invocations of an update method.
1021+
*
1022+
* @return the hash as a hex String
1023+
*
1024+
* @since 1.11
1025+
*/
1026+
public String asHex() {
1027+
return Hex.encodeHexString(messageDigest.digest());
1028+
}
1029+
1030+
/**
1031+
* Updates the {@link MessageDigest} in the {@link DigestUtils} instance
1032+
*
1033+
* @param valueToDigest
1034+
* the value to update the {@link MessageDigest} with
1035+
* @return the updated {@link DigestUtils}
1036+
* @since 1.11
1037+
*/
1038+
public DigestUtils update(final byte[] data) throws IOException {
1039+
messageDigest.update(data);
1040+
return this;
1041+
}
1042+
1043+
/**
1044+
* Updates the {@link MessageDigest} in the {@link DigestUtils} instance
1045+
*
1046+
* @param valueToDigest
1047+
* the value to update the {@link MessageDigest} with
1048+
* @return the updated {@link DigestUtils}
1049+
* @since 1.11
1050+
*/
1051+
public DigestUtils update(final ByteBuffer data) throws IOException {
1052+
messageDigest.update(data);
1053+
return this;
1054+
}
1055+
1056+
/**
1057+
* Updates the {@link MessageDigest} in the {@link DigestUtils} instance
1058+
*
1059+
* @param valueToDigest
1060+
* the value to update the {@link MessageDigest} with
1061+
* @return the updated {@link DigestUtils}
1062+
* @since 1.11
1063+
*/
1064+
public DigestUtils update(final String data) throws IOException {
1065+
messageDigest.update(StringUtils.getBytesUtf8(data));
1066+
return this;
1067+
}
1068+
1069+
/**
1070+
* Updates the {@link MessageDigest} in the {@link DigestUtils} instance
1071+
*
1072+
* @param valueToDigest
1073+
* the value to update the {@link MessageDigest} with
1074+
* @return the updated {@link DigestUtils}
1075+
* @since 1.11
1076+
*/
1077+
public DigestUtils update(final InputStream data) throws IOException {
1078+
final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
1079+
int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
1080+
1081+
while (read > -1) {
1082+
messageDigest.update(buffer, 0, read);
1083+
read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
1084+
}
1085+
return this;
1086+
}
1087+
1088+
/**
1089+
* Updates the {@link MessageDigest} in the {@link DigestUtils} instance
1090+
*
1091+
* @param valueToDigest
1092+
* the value to update the {@link MessageDigest} with
1093+
* @return the updated {@link DigestUtils}
1094+
* @since 1.11
1095+
*/
1096+
public DigestUtils update(final File data) throws IOException {
1097+
final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(data));
1098+
try {
1099+
return update(stream);
1100+
} finally {
1101+
stream.close();
1102+
}
1103+
}
10331104
}

src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,11 @@ public void testSha1UpdateWithString(){
263263

264264
@Test
265265
public void testSha224() throws IOException {
266-
assumeJava8(); assertEquals("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
267-
Hex.encodeHexString(DigestUtils.digest(MessageDigestAlgorithms.SHA_224,StringUtils.getBytesUtf8(""))));
266+
assumeJava8();
267+
assertEquals("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
268+
DigestUtils.with(MessageDigestAlgorithms.SHA_224).update(("")).asHex());
268269
assertEquals("730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525",
269-
Hex.encodeHexString(DigestUtils.digest(MessageDigestAlgorithms.SHA_224,StringUtils.getBytesUtf8("The quick brown fox jumps over the lazy dog"))));
270+
DigestUtils.with(MessageDigestAlgorithms.SHA_224).update("The quick brown fox jumps over the lazy dog").asHex());
270271

271272
// Examples from FIPS 180-4?
272273
}

0 commit comments

Comments
 (0)