99import java .security .NoSuchAlgorithmException ;
1010import java .util .Arrays ;
1111
12+ /**
13+ * A class which packs any byte input stream or array into a fixed-size output byte array using a {@link MessageDigest}.
14+ * This is useful when there is a need to sign or verify large files with a signature scheme that requires caching the input data (such as Ed25519).
15+ * The default {@link MessageDigest} is SHA-512, the default output size is 512 KiB.
16+ *
17+ * @author DevCybran
18+ *
19+ */
1220public class HashCondenser {
1321 public static final int DEFAULT_OUTPUT_SIZE = 512 *1024 ;
1422 private static final int LONG_SIZE = Long .SIZE /8 ;
@@ -17,18 +25,47 @@ public class HashCondenser {
1725 private final int digestLength ;
1826 private final int hashCount ;
1927
20- public static HashCondenser getInstance (MessageDigest md , int outputSize ) {
28+ /**
29+ * Returns a new instance using the given parameters.
30+ *
31+ * @param md a MessageDigest. Must support {@link MessageDigest#getDigestLength()}.
32+ * @param outputSize the size (in bytes) any input should be condensed to. Has to be greater than at least one digest length + 8.
33+ * @return
34+ * @throws IllegalArgumentException when the parameter conditions are not met
35+ */
36+ public static HashCondenser getInstance (MessageDigest md , int outputSize ) throws IllegalArgumentException {
2137 return new HashCondenser (md , outputSize );
2238 }
2339
24- public static HashCondenser getInstance (int outputSize ) throws NoSuchAlgorithmException {
40+ /**
41+ * Returns a new instance using the given output size and the SHA-512 digest.
42+ *
43+ * @param outputSize the size (in bytes) any input should be condensed to. Has to be at least 72.
44+ * @return
45+ * @throws IllegalArgumentException when outputSize is invalid
46+ * @throws NoSuchAlgorithmException when SHA-512 is not present on this machine
47+ */
48+ public static HashCondenser getInstance (int outputSize ) throws IllegalArgumentException , NoSuchAlgorithmException {
2549 return new HashCondenser (MessageDigest .getInstance ("SHA-512" ), outputSize );
2650 }
2751
28- public static HashCondenser getInstance (MessageDigest md ) {
52+ /**
53+ * Returns a new instance using the given MessageDigest and the default output size (512 KiB).
54+ *
55+ * @param md a MessageDigest. Must support {@link MessageDigest#getDigestLength()}.
56+ * @return
57+ * @throws IllegalArgumentException when the parameter conditions are not met
58+ */
59+ public static HashCondenser getInstance (MessageDigest md ) throws IllegalArgumentException {
2960 return new HashCondenser (md , DEFAULT_OUTPUT_SIZE );
3061 }
3162
63+ /**
64+ * Returns a new instance using the default parameters (512 KiB SHA-512).
65+ *
66+ * @return
67+ * @throws NoSuchAlgorithmException when SHA-512 is not present on this machine
68+ */
3269 public static HashCondenser getInstance () throws NoSuchAlgorithmException {
3370 return new HashCondenser (MessageDigest .getInstance ("SHA-512" ), DEFAULT_OUTPUT_SIZE );
3471 }
@@ -47,7 +84,7 @@ private HashCondenser(MessageDigest md, int outputSize) {
4784 *
4885 * @see #compute(InputStream, long)
4986 * @param data
50- * @return
87+ * @return the condensed version of the input
5188 */
5289 public byte [] compute (byte [] data ) {
5390 try {
@@ -60,12 +97,13 @@ public byte[] compute(byte[] data) {
6097
6198 /**
6299 * Computes a condensed version of the input (source).
63- * If sourceSize is not greater than this object's outputSize, then a 0-padded copy of the source will be returned.
100+ * If sourceSize and a specific overhead is not greater than this object's outputSize, then a 0-padded copy of the source will be returned.
64101 * Otherwise, a 0-padded concatenation of hashes for segments of the source will be returned.
102+ * Every result contains the original sourceSize and a operation mode indicator.
65103 *
66104 * @param source
67105 * @param sourceSize the exact number of bytes which can be read from source
68- * @return
106+ * @return the condensed version of the input
69107 * @throws IOException when source throws an IOException
70108 * @throws IllegalArgumentException if sourceSize happens not to be the same as source's size
71109 */
0 commit comments