|
35 | 35 | * However the MessageDigest instances it creates generally won't be. |
36 | 36 | * <p> |
37 | 37 | * The {@link MessageDigestAlgorithms} class provides constants for standard |
38 | | - * digest algorithms that can be used with the {@link #getDigest(String)} method. |
| 38 | + * digest algorithms that can be used with the {@link #getDigest(String)} method |
| 39 | + * and other methods that require the Digest algorithm name. |
| 40 | + * <p> |
| 41 | + * Note: the class has short-hand methods for all the algorithms present as standard in Java 6. |
| 42 | + * This approach requires lots of methods for each algorithm, and quickly becomes unwieldy. |
| 43 | + * The following code works with all algorithms: |
| 44 | + * <code> |
| 45 | + * <pre> |
| 46 | + * import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_224; |
| 47 | + * ... |
| 48 | + * byte [] digest = DigestUtils.digest(SHA_224, dataToDigest); |
| 49 | + * </pre> |
| 50 | + * </code> |
39 | 51 | * @see MessageDigestAlgorithms |
40 | 52 | * @version $Id$ |
41 | 53 | */ |
@@ -108,6 +120,72 @@ public static byte[] digest(final MessageDigest messageDigest, final InputStream |
108 | 120 | return updateDigest(messageDigest, data).digest(); |
109 | 121 | } |
110 | 122 |
|
| 123 | + /** |
| 124 | + * Reads through a byte array and returns the digest for the data. |
| 125 | + * |
| 126 | + * @param digestName |
| 127 | + * The name of the algorithm to use (e.g. MessageDigestAlgoriths.MD5 or "MD5") |
| 128 | + * @param data |
| 129 | + * Data to digest |
| 130 | + * @return the digest |
| 131 | + * @throws IOException |
| 132 | + * On error reading from the stream |
| 133 | + * @since 1.11 |
| 134 | + */ |
| 135 | + public static byte[] digest(final String digestName, final byte[] data) { |
| 136 | + return digest(getDigest(digestName), data); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Reads through a ByteBuffer and returns the digest for the data |
| 141 | + * |
| 142 | + * @param digestName |
| 143 | + * The name of the algorithm to use (e.g. MessageDigestAlgoriths.MD5 or "MD5") |
| 144 | + * @param data |
| 145 | + * Data to digest |
| 146 | + * @return the digest |
| 147 | + * @throws IOException |
| 148 | + * On error reading from the stream |
| 149 | + * @since 1.11 |
| 150 | + */ |
| 151 | + public static byte[] digest(final String digestName, final ByteBuffer data) { |
| 152 | + MessageDigest messageDigest = getDigest(digestName); |
| 153 | + messageDigest .update(data); |
| 154 | + return messageDigest.digest(); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Reads through a File and returns the digest for the data |
| 159 | + * |
| 160 | + * @param digestName |
| 161 | + * The name of the algorithm to use (e.g. MessageDigestAlgoriths.MD5 or "MD5") |
| 162 | + * @param data |
| 163 | + * Data to digest |
| 164 | + * @return the digest |
| 165 | + * @throws IOException |
| 166 | + * On error reading from the stream |
| 167 | + * @since 1.11 |
| 168 | + */ |
| 169 | + public static byte[] digest(final String digestName, final File data) throws IOException { |
| 170 | + return updateDigest(getDigest(digestName), data).digest(); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Reads through an InputStream and returns the digest for the data |
| 175 | + * |
| 176 | + * @param digestName |
| 177 | + * The name of the algorithm to use (e.g. MessageDigestAlgoriths.MD5 or "MD5") |
| 178 | + * @param data |
| 179 | + * Data to digest |
| 180 | + * @return the digest |
| 181 | + * @throws IOException |
| 182 | + * On error reading from the stream |
| 183 | + * @since 1.11 |
| 184 | + */ |
| 185 | + public static byte[] digest(final String digestName, final InputStream data) throws IOException { |
| 186 | + return updateDigest(getDigest(digestName), data).digest(); |
| 187 | + } |
| 188 | + |
111 | 189 | /** |
112 | 190 | * Returns a <code>MessageDigest</code> for the given <code>algorithm</code>. |
113 | 191 | * |
@@ -195,23 +273,6 @@ public static MessageDigest getSha1Digest() { |
195 | 273 | return getDigest(MessageDigestAlgorithms.SHA_1); |
196 | 274 | } |
197 | 275 |
|
198 | | - /** |
199 | | - * Returns an SHA-224 digest. |
200 | | - * <p> |
201 | | - * Java 8 only. |
202 | | - * </p> |
203 | | - * |
204 | | - * @return An SHA-224 digest instance. |
205 | | - * @throws IllegalArgumentException |
206 | | - * when a {@link NoSuchAlgorithmException} is caught on Java 7 and older, SHA-224 is a built-in |
207 | | - * algorithm on Java 8 |
208 | | - * @see MessageDigestAlgorithms#SHA_224 |
209 | | - * @since 1.11 |
210 | | - */ |
211 | | - public static MessageDigest getSha224Digest() { |
212 | | - return getDigest(MessageDigestAlgorithms.SHA_224); |
213 | | - } |
214 | | - |
215 | 276 | /** |
216 | 277 | * Returns an SHA-256 digest. |
217 | 278 | * <p> |
@@ -694,171 +755,6 @@ public static String sha1Hex(final String data) { |
694 | 755 | return Hex.encodeHexString(sha1(data)); |
695 | 756 | } |
696 | 757 |
|
697 | | - /** |
698 | | - * Calculates the SHA-224 digest and returns the value as a <code>byte[]</code>. |
699 | | - * <p> |
700 | | - * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0. |
701 | | - * </p> |
702 | | - * |
703 | | - * @param data |
704 | | - * Data to digest |
705 | | - * @return SHA-224 digest |
706 | | - * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0. |
707 | | - * @since 1.11 |
708 | | - */ |
709 | | - public static byte[] sha224(final byte[] data) { |
710 | | - return digest(getSha224Digest(), data); |
711 | | - } |
712 | | - |
713 | | - /** |
714 | | - * Calculates the SHA-224 digest and returns the value as a <code>byte[]</code>. |
715 | | - * |
716 | | - * @param data |
717 | | - * Data to digest |
718 | | - * @return SHA-224 digest |
719 | | - * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0. |
720 | | - * @since 1.11 |
721 | | - */ |
722 | | - public static byte[] sha224(final ByteBuffer data) { |
723 | | - return digest(getSha224Digest(), data); |
724 | | - } |
725 | | - |
726 | | - /** |
727 | | - * Calculates the SHA-224 digest and returns the value as a <code>byte[]</code>. |
728 | | - * <p> |
729 | | - * Throws a <code>RuntimeException</code> on JRE versions prior to 1.8.0. |
730 | | - * </p> |
731 | | - * |
732 | | - * @param data |
733 | | - * File to digest |
734 | | - * @return SHA-224 digest |
735 | | - * @throws IOException |
736 | | - * On error reading from the stream |
737 | | - * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0. |
738 | | - * @since 1.11 |
739 | | - */ |
740 | | - public static byte[] sha224(final File data) throws IOException { |
741 | | - return digest(getSha224Digest(), data); |
742 | | - } |
743 | | - |
744 | | - /** |
745 | | - * Calculates the SHA-224 digest and returns the value as a <code>byte[]</code>. |
746 | | - * <p> |
747 | | - * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.8.0. |
748 | | - * </p> |
749 | | - * |
750 | | - * @param data |
751 | | - * Data to digest |
752 | | - * @return SHA-224 digest |
753 | | - * @throws IOException |
754 | | - * On error reading from the stream |
755 | | - * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0. |
756 | | - * @since 1.11 |
757 | | - */ |
758 | | - public static byte[] sha224(final InputStream data) throws IOException { |
759 | | - return digest(getSha224Digest(), data); |
760 | | - } |
761 | | - |
762 | | - /** |
763 | | - * Calculates the SHA-224 digest and returns the value as a <code>byte[]</code>. |
764 | | - * <p> |
765 | | - * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.8.0. |
766 | | - * </p> |
767 | | - * |
768 | | - * @param data |
769 | | - * Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} |
770 | | - * @return SHA-224 digest |
771 | | - * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0. |
772 | | - * @since 1.11 |
773 | | - */ |
774 | | - public static byte[] sha224(final String data) { |
775 | | - return sha224(StringUtils.getBytesUtf8(data)); |
776 | | - } |
777 | | - |
778 | | - /** |
779 | | - * Calculates the SHA-224 digest and returns the value as a hex string. |
780 | | - * <p> |
781 | | - * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.8.0. |
782 | | - * </p> |
783 | | - * |
784 | | - * @param data |
785 | | - * Data to digest |
786 | | - * @return SHA-224 digest as a hex string |
787 | | - * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0. |
788 | | - * @since 1.11 |
789 | | - */ |
790 | | - public static String sha224Hex(final byte[] data) { |
791 | | - return Hex.encodeHexString(sha224(data)); |
792 | | - } |
793 | | - |
794 | | - /** |
795 | | - * Calculates the SHA-224 digest and returns the value as a hex string. |
796 | | - * <p> |
797 | | - * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.8.0. |
798 | | - * </p> |
799 | | - * |
800 | | - * @param data |
801 | | - * Data to digest |
802 | | - * @return SHA-224 digest as a hex string |
803 | | - * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0. |
804 | | - * @since 1.11 |
805 | | - */ |
806 | | - public static String sha224Hex(final ByteBuffer data) { |
807 | | - return Hex.encodeHexString(sha224(data)); |
808 | | - } |
809 | | - |
810 | | - /** |
811 | | - * Calculates the SHA-224 digest and returns the value as a hex string. |
812 | | - * <p> |
813 | | - * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.8.0. |
814 | | - * </p> |
815 | | - * |
816 | | - * @param data |
817 | | - * Data to digest |
818 | | - * @return SHA-224 digest as a hex string |
819 | | - * @throws IOException |
820 | | - * On error reading from the stream |
821 | | - * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0. |
822 | | - * @since 1.11 |
823 | | - */ |
824 | | - public static String sha224Hex(final File data) throws IOException { |
825 | | - return Hex.encodeHexString(sha224(data)); |
826 | | - } |
827 | | - |
828 | | - /** |
829 | | - * Calculates the SHA-224 digest and returns the value as a hex string. |
830 | | - * <p> |
831 | | - * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.8.0. |
832 | | - * </p> |
833 | | - * |
834 | | - * @param data |
835 | | - * Data to digest |
836 | | - * @return SHA-224 digest as a hex string |
837 | | - * @throws IOException |
838 | | - * On error reading from the stream |
839 | | - * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0. |
840 | | - * @since 1.11 |
841 | | - */ |
842 | | - public static String sha224Hex(final InputStream data) throws IOException { |
843 | | - return Hex.encodeHexString(sha224(data)); |
844 | | - } |
845 | | - |
846 | | - /** |
847 | | - * Calculates the SHA-224 digest and returns the value as a hex string. |
848 | | - * <p> |
849 | | - * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.8.0. |
850 | | - * </p> |
851 | | - * |
852 | | - * @param data |
853 | | - * Data to digest |
854 | | - * @return SHA-224 digest as a hex string |
855 | | - * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0. |
856 | | - * @since 1.11 |
857 | | - */ |
858 | | - public static String sha224Hex(final String data) { |
859 | | - return Hex.encodeHexString(sha224(data)); |
860 | | - } |
861 | | - |
862 | 758 | /** |
863 | 759 | * Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>. |
864 | 760 | * <p> |
|
0 commit comments