Skip to content

Commit 0b683ac

Browse files
committed
Simplify
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1745140 13f79535-47bb-0310-9956-ffa450edef68
1 parent 184b53f commit 0b683ac

2 files changed

Lines changed: 103 additions & 76 deletions

File tree

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

Lines changed: 101 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
* <pre>
4545
* import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_224;
4646
* ...
47-
* byte [] digest = DigestUtils.with(SHA_224).update(dataToDigest).done();
48-
* String hdigest = DigestUtils.with(SHA_224).update(new File("pom.xml")).asHex();
47+
* byte [] digest = new DigestUtils(SHA_224).digest(dataToDigest);
48+
* String hdigest = new DigestUtils(SHA_224).digestAsHex(new File("pom.xml"));
4949
* </pre>
5050
* @see MessageDigestAlgorithms
5151
* @version $Id$
@@ -110,7 +110,7 @@ public static byte[] digest(final MessageDigest messageDigest, final File data)
110110
* @return the digest
111111
* @throws IOException
112112
* On error reading from the stream
113-
* @since 1.11 (was private previously)
113+
* @since 1.11 (was private)
114114
*/
115115
public static byte[] digest(final MessageDigest messageDigest, final InputStream data) throws IOException {
116116
return updateDigest(messageDigest, data).digest();
@@ -959,39 +959,40 @@ public static boolean isAvailable(String messageDigestAlgorithm) {
959959
return getDigest(messageDigestAlgorithm, null) != null;
960960
}
961961

962-
// Fluent interface
963-
964962
private final MessageDigest messageDigest;
965963

966964
// public to maintain binary compatibility
967965
public DigestUtils() {
968966
this.messageDigest = null;
969967
}
970968

971-
private DigestUtils(MessageDigest digest) {
972-
this.messageDigest = digest;
973-
}
974-
975969
/**
976-
* Returns a fluent instance for the digest algorithm.
977-
* Does not reset the digest before use.
978-
* @param digest the digest instance to use
979-
* @return the fluent instance
970+
* Creates an instance using the provided {@link MessageDigest} parameter.
971+
*
972+
* This can then be used to create digests using methods such as
973+
* {@link #digest(byte[])} and {@link #digestAsHex(File)}.
974+
*
975+
* @param digest the {@link MessageDigest} to use
980976
* @since 1.11
981977
*/
982-
public static DigestUtils with(MessageDigest digest) {
983-
return new DigestUtils(digest);
978+
public DigestUtils(MessageDigest digest) {
979+
this.messageDigest = digest;
984980
}
985981

986982
/**
987-
* Creates a {@link MessageDigest} and returns a fluent instance.
983+
* Creates an instance using the provided {@link MessageDigest} parameter.
984+
*
985+
* This can then be used to create digests using methods such as
986+
* {@link #digest(byte[])} and {@link #digestAsHex(File)}.
988987
*
989-
* @param name the name of digest algorithm to create, e.g. {@link MessageDigestAlgorithms#MD5}
990-
* @return the fluent instance
988+
* @param name the name of the {@link MessageDigest} to use
989+
* @see #getDigest(String)
990+
* @throws IllegalArgumentException
991+
* when a {@link NoSuchAlgorithmException} is caught.
991992
* @since 1.11
992993
*/
993-
public static DigestUtils with(String name) {
994-
return new DigestUtils(getDigest(name));
994+
public DigestUtils(String name) {
995+
this(getDigest(name));
995996
}
996997

997998
/**
@@ -1004,107 +1005,133 @@ public MessageDigest getMessageDigest() {
10041005
}
10051006

10061007
/**
1007-
* Completes the hash computation and returns the hash
1008-
* accumulated by one or more invocations of an update method.
1008+
* Reads through a byte array and returns the digest for the data.
10091009
*
1010-
* @return the hash as a byte array
1010+
* @param data
1011+
* Data to digest
1012+
* @return the digest
1013+
* @since 1.11
1014+
*/
1015+
public byte[] digest(final byte[] data) {
1016+
return updateDigest(messageDigest, data).digest();
1017+
}
1018+
1019+
/**
1020+
* Reads through a byte array and returns the digest for the data.
10111021
*
1022+
* @param data
1023+
* Data to digest treated as UTF-8 string
1024+
* @return the digest
10121025
* @since 1.11
10131026
*/
1014-
public byte[] done() {
1015-
return messageDigest.digest();
1027+
public byte[] digest(final String data) {
1028+
return updateDigest(messageDigest, data).digest();
10161029
}
10171030

10181031
/**
1019-
* Completes the hash computation and returns the hash
1020-
* accumulated by one or more invocations of an update method.
1032+
* Reads through a ByteBuffer and returns the digest for the data
10211033
*
1022-
* @return the hash as a hex String
1034+
* @param data
1035+
* Data to digest
1036+
* @return the digest
10231037
*
10241038
* @since 1.11
10251039
*/
1026-
public String asHex() {
1027-
return Hex.encodeHexString(messageDigest.digest());
1040+
public byte[] digest(final ByteBuffer data) {
1041+
return updateDigest(messageDigest, data).digest();
10281042
}
10291043

10301044
/**
1031-
* Updates the {@link MessageDigest} in the {@link DigestUtils} instance
1045+
* Reads through a File and returns the digest for the data
10321046
*
10331047
* @param data
1034-
* the data to update the {@link MessageDigest} with
1035-
* @return the updated {@link DigestUtils}
1048+
* Data to digest
1049+
* @return the digest
1050+
* @throws IOException
1051+
* On error reading from the stream
10361052
* @since 1.11
10371053
*/
1038-
public DigestUtils update(final byte[] data) {
1039-
messageDigest.update(data);
1040-
return this;
1054+
public byte[] digest(final File data) throws IOException {
1055+
return updateDigest(messageDigest, data).digest();
1056+
}
1057+
1058+
/**
1059+
* Reads through an InputStream and returns the digest for the data
1060+
*
1061+
* @param data
1062+
* Data to digest
1063+
* @return the digest
1064+
* @throws IOException
1065+
* On error reading from the stream
1066+
* @since 1.11
1067+
*/
1068+
public byte[] digest(final InputStream data) throws IOException {
1069+
return updateDigest(messageDigest, data).digest();
10411070
}
10421071

10431072
/**
1044-
* Updates the {@link MessageDigest} in the {@link DigestUtils} instance
1073+
* Reads through a byte array and returns the digest for the data.
10451074
*
10461075
* @param data
1047-
* the data to update the {@link MessageDigest} with
1048-
* @return the updated {@link DigestUtils}
1076+
* Data to digest
1077+
* @return the digest as a hex string
10491078
* @since 1.11
10501079
*/
1051-
public DigestUtils update(final ByteBuffer data) {
1052-
messageDigest.update(data);
1053-
return this;
1080+
public String digestAsHex(final byte[] data) {
1081+
return Hex.encodeHexString(digest(data));
10541082
}
10551083

10561084
/**
1057-
* Updates the {@link MessageDigest} in the {@link DigestUtils} instance
1085+
* Reads through a byte array and returns the digest for the data.
10581086
*
10591087
* @param data
1060-
* the data to update the {@link MessageDigest} with
1061-
* @return the updated {@link DigestUtils}
1088+
* Data to digest treated as UTF-8 string
1089+
* @return the digest as a hex string
10621090
* @since 1.11
10631091
*/
1064-
public DigestUtils update(final String data) {
1065-
messageDigest.update(StringUtils.getBytesUtf8(data));
1066-
return this;
1092+
public String digestAsHex(final String data) {
1093+
return Hex.encodeHexString(digest(data));
10671094
}
10681095

10691096
/**
1070-
* Updates the {@link MessageDigest} in the {@link DigestUtils} instance
1097+
* Reads through a ByteBuffer and returns the digest for the data
10711098
*
10721099
* @param data
1073-
* the data to update the {@link MessageDigest} with
1074-
* @return the updated {@link DigestUtils}
1075-
* @throws IOException
1076-
* If some I/O error occurs.
1100+
* Data to digest
1101+
* @return the digest as a hex string
1102+
*
10771103
* @since 1.11
10781104
*/
1079-
public DigestUtils update(final InputStream data) throws IOException {
1080-
final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
1081-
int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
1105+
public String digestAsHex(final ByteBuffer data) {
1106+
return Hex.encodeHexString(digest(data));
1107+
}
10821108

1083-
while (read > -1) {
1084-
messageDigest.update(buffer, 0, read);
1085-
read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
1086-
}
1087-
return this;
1109+
/**
1110+
* Reads through a File and returns the digest for the data
1111+
*
1112+
* @param data
1113+
* Data to digest
1114+
* @return the digest as a hex string
1115+
* @throws IOException
1116+
* On error reading from the stream
1117+
* @since 1.11
1118+
*/
1119+
public String digestAsHex(final File data) throws IOException {
1120+
return Hex.encodeHexString(digest(data));
10881121
}
10891122

10901123
/**
1091-
* Updates the {@link MessageDigest} in the {@link DigestUtils} instance
1124+
* Reads through an InputStream and returns the digest for the data
10921125
*
10931126
* @param data
1094-
* the data to update the {@link MessageDigest} with
1095-
* @return the updated {@link DigestUtils}
1127+
* Data to digest
1128+
* @return the digest as a hex string
10961129
* @throws IOException
1097-
* If some I/O error occurs.
1098-
* @throws SecurityException
1099-
* if a security manager exists and its {@code checkRead} method denies read access to the file.
1130+
* On error reading from the stream
11001131
* @since 1.11
11011132
*/
1102-
public DigestUtils update(final File data) throws IOException {
1103-
final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(data));
1104-
try {
1105-
return update(stream);
1106-
} finally {
1107-
stream.close();
1108-
}
1133+
public String digestAsHex(final InputStream data) throws IOException {
1134+
return Hex.encodeHexString(digest(data));
11091135
}
1136+
11101137
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,9 @@ public void testSha1UpdateWithString(){
259259
public void testSha224() throws IOException {
260260
assumeJava8();
261261
assertEquals("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
262-
DigestUtils.with(MessageDigestAlgorithms.SHA_224).update(("")).asHex());
262+
new DigestUtils(MessageDigestAlgorithms.SHA_224).digestAsHex(("")));
263263
assertEquals("730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525",
264-
DigestUtils.with(MessageDigestAlgorithms.SHA_224).update("The quick brown fox jumps over the lazy dog").asHex());
264+
new DigestUtils(MessageDigestAlgorithms.SHA_224).digestAsHex("The quick brown fox jumps over the lazy dog"));
265265

266266
// Examples from FIPS 180-4?
267267
}

0 commit comments

Comments
 (0)