|
45 | 45 | * <pre> |
46 | 46 | * import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_224; |
47 | 47 | * ... |
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(); |
50 | 50 | * </pre> |
51 | 51 | * </code> |
52 | 52 | * @see MessageDigestAlgorithms |
@@ -80,8 +80,7 @@ public static byte[] digest(final MessageDigest messageDigest, final byte[] data |
80 | 80 | * @param data |
81 | 81 | * Data to digest |
82 | 82 | * @return the digest |
83 | | - * @throws IOException |
84 | | - * On error reading from the stream |
| 83 | + * |
85 | 84 | * @since 1.11 |
86 | 85 | */ |
87 | 86 | public static byte[] digest(final MessageDigest messageDigest, final ByteBuffer data) { |
@@ -121,72 +120,6 @@ public static byte[] digest(final MessageDigest messageDigest, final InputStream |
121 | 120 | return updateDigest(messageDigest, data).digest(); |
122 | 121 | } |
123 | 122 |
|
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 | | - |
190 | 123 | /** |
191 | 124 | * Returns a <code>MessageDigest</code> for the given <code>algorithm</code>. |
192 | 125 | * |
@@ -1030,4 +963,142 @@ public static boolean isAvailable(String messageDigestAlgorithm) { |
1030 | 963 | return getDigest(messageDigestAlgorithm, null) != null; |
1031 | 964 | } |
1032 | 965 |
|
| 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 | + } |
1033 | 1104 | } |
0 commit comments