|
43 | 43 | * Sample usage: |
44 | 44 | * <pre> |
45 | 45 | * import static HmacAlgorithms.*; |
46 | | - * byte[] key = {1,2,3,4}; // don't use this! |
| 46 | + * byte[] key = {1,2,3,4}; // don't use this actual key! |
47 | 47 | * String valueToDigest = "The quick brown fox jumps over the lazy dog"; |
48 | | - * byte[] hmac = HmacUtils.use(HMAC_SHA_224, key).update(valueToDigest).doFinal(); |
| 48 | + * byte[] hmac = HmacUtils.use(HMAC_SHA_224, key).hmac(valueToDigest); |
49 | 49 | * // Mac re-use |
50 | 50 | * HmacUtils hm1 = HmacUtils.use("HmacAlgoName", key); // use a valid name here! |
51 | | - * String hexPom = hm1.update(new File("pom.xml")).doFinalHex(); |
52 | | - * String hexNot = hm1.update(new File("NOTICE.txt")).doFinalHex(); |
| 51 | + * String hexPom = hm1.hmacHex(new File("pom.xml")); |
| 52 | + * String hexNot = hm1.hmacHex(new File("NOTICE.txt")); |
53 | 53 | * </pre> |
54 | 54 | * @since 1.10 |
55 | 55 | * @version $Id$ |
@@ -898,119 +898,153 @@ public static HmacUtils use(final HmacAlgorithms algorithm, final byte[] key) { |
898 | 898 | } |
899 | 899 |
|
900 | 900 | /** |
901 | | - * Updates the stored {@link Mac} with the value. |
| 901 | + * Returns the digest for the input data. |
902 | 902 | * |
903 | | - * @param valueToDigest |
904 | | - * the value to update the {@link Mac} with (maybe null or empty) |
905 | | - * @return the updated instance |
906 | | - * @throws IllegalStateException |
907 | | - * if the Mac was not initialized |
| 903 | + * @param valueToDigest the input to use |
| 904 | + * @return the digest as a byte[] |
| 905 | + * @throws IOException |
| 906 | + * If an I/O error occurs. |
908 | 907 | * @since 1.11 |
909 | 908 | */ |
910 | | - public HmacUtils update(final byte[] valueToDigest) { |
911 | | - mac.update(valueToDigest); |
912 | | - return this; |
| 909 | + public byte[] hmac(byte[] valueToDigest) { |
| 910 | + return mac.doFinal(valueToDigest); |
913 | 911 | } |
914 | 912 |
|
915 | 913 | /** |
916 | | - * Updates the stored {@link Mac} with the value. |
| 914 | + * Returns the digest for the input data. |
917 | 915 | * |
918 | | - * @param valueToDigest |
919 | | - * the value to update the {@link Mac} with (maybe null or empty) |
920 | | - * @return the updated instance |
921 | | - * @throws IllegalStateException |
922 | | - * if the Mac was not initialized |
| 916 | + * @param valueToDigest the input to use |
| 917 | + * @return the digest as a hex String |
| 918 | + * @throws IOException |
| 919 | + * If an I/O error occurs. |
| 920 | + * @since 1.11 |
| 921 | + */ |
| 922 | + public String hmacHex(byte[] valueToDigest) { |
| 923 | + return Hex.encodeHexString(hmac(valueToDigest)); |
| 924 | + } |
| 925 | + |
| 926 | + /** |
| 927 | + * Returns the digest for the input data. |
| 928 | + * |
| 929 | + * @param valueToDigest the input to use, treated as UTF-8 |
| 930 | + * @return the digest as a byte[] |
| 931 | + * @throws IOException |
| 932 | + * If an I/O error occurs. |
923 | 933 | * @since 1.11 |
924 | 934 | */ |
925 | | - public HmacUtils update(final ByteBuffer valueToDigest) { |
| 935 | + public byte[] hmac(String valueToDigest) { |
| 936 | + return mac.doFinal(StringUtils.getBytesUtf8(valueToDigest)); |
| 937 | + } |
| 938 | + |
| 939 | + /** |
| 940 | + * Returns the digest for the input data. |
| 941 | + * |
| 942 | + * @param valueToDigest the input to use, treated as UTF-8 |
| 943 | + * @return the digest as a hex String |
| 944 | + * @throws IOException |
| 945 | + * If an I/O error occurs. |
| 946 | + * @since 1.11 |
| 947 | + */ |
| 948 | + public String hmacHex(String valueToDigest) { |
| 949 | + return Hex.encodeHexString(hmac(valueToDigest)); |
| 950 | + } |
| 951 | + |
| 952 | + /** |
| 953 | + * Returns the digest for the input data. |
| 954 | + * |
| 955 | + * @param valueToDigest the input to use |
| 956 | + * @return the digest as a byte[] |
| 957 | + * @throws IOException |
| 958 | + * If an I/O error occurs. |
| 959 | + * @since 1.11 |
| 960 | + */ |
| 961 | + public byte[] hmac(ByteBuffer valueToDigest) { |
926 | 962 | mac.update(valueToDigest); |
927 | | - return this; |
| 963 | + return mac.doFinal(); |
928 | 964 | } |
929 | 965 |
|
930 | 966 | /** |
931 | | - * Updates the stored {@link Mac} with the value. |
932 | | - * String is converted to bytes using the UTF-8 charset. |
933 | | - * @param valueToDigest |
934 | | - * the value to update the {@link Mac} with. |
935 | | - * @return the updated instance |
936 | | - * @throws IllegalStateException |
937 | | - * if the Mac was not initialized |
| 967 | + * Returns the digest for the input data. |
| 968 | + * |
| 969 | + * @param valueToDigest the input to use |
| 970 | + * @return the digest as a hex String |
| 971 | + * @throws IOException |
| 972 | + * If an I/O error occurs. |
938 | 973 | * @since 1.11 |
939 | 974 | */ |
940 | | - public HmacUtils update(final String valueToDigest) { |
941 | | - mac.update(StringUtils.getBytesUtf8(valueToDigest)); |
942 | | - return this; |
| 975 | + public String hmacHex(ByteBuffer valueToDigest) { |
| 976 | + return Hex.encodeHexString(hmac(valueToDigest)); |
943 | 977 | } |
944 | 978 |
|
945 | 979 | /** |
946 | | - * Updates the stored {@link Mac} with the value. |
| 980 | + * Returns the digest for the stream. |
947 | 981 | * |
948 | 982 | * @param valueToDigest |
949 | | - * the value to update the {@link Mac} with |
| 983 | + * the data to use |
950 | 984 | * <p> |
951 | 985 | * The InputStream must not be null and will not be closed |
952 | 986 | * </p> |
953 | | - * @return the updated instance |
| 987 | + * @return the digest |
954 | 988 | * @throws IOException |
955 | 989 | * If an I/O error occurs. |
956 | | - * @throws IllegalStateException |
957 | | - * If the Mac was not initialized |
958 | 990 | * @since 1.11 |
959 | 991 | */ |
960 | | - public HmacUtils update(final InputStream valueToDigest) throws IOException { |
| 992 | + public byte[] hmac(InputStream valueToDigest) throws IOException { |
961 | 993 | final byte[] buffer = new byte[STREAM_BUFFER_LENGTH]; |
962 | 994 | int read; |
963 | 995 |
|
964 | 996 | while ((read = valueToDigest.read(buffer, 0, STREAM_BUFFER_LENGTH) ) > -1) { |
965 | 997 | mac.update(buffer, 0, read); |
966 | 998 | } |
967 | | - return this; |
| 999 | + return mac.doFinal(); |
968 | 1000 | } |
969 | 1001 |
|
970 | 1002 | /** |
971 | | - * Updates the stored {@link Mac} with the value. |
| 1003 | + * Returns the digest for the stream. |
972 | 1004 | * |
973 | 1005 | * @param valueToDigest |
974 | | - * the value to update the {@link Mac} with |
| 1006 | + * the data to use |
975 | 1007 | * <p> |
976 | 1008 | * The InputStream must not be null and will not be closed |
977 | 1009 | * </p> |
978 | | - * @return the updated instance |
| 1010 | + * @return the digest as a hex String |
979 | 1011 | * @throws IOException |
980 | 1012 | * If an I/O error occurs. |
981 | | - * @throws IllegalStateException |
982 | | - * If the Mac was not initialized |
983 | 1013 | * @since 1.11 |
984 | 1014 | */ |
985 | | - public HmacUtils update(final File valueToDigest) throws IOException { |
986 | | - final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(valueToDigest)); |
987 | | - try { |
988 | | - return update(stream); |
989 | | - } finally { |
990 | | - stream.close(); |
991 | | - } |
| 1015 | + public String hmacHex(InputStream valueToDigest) throws IOException { |
| 1016 | + return Hex.encodeHexString(hmac(valueToDigest)); |
992 | 1017 | } |
993 | 1018 |
|
994 | 1019 | /** |
995 | | - * Finishes the MAC operation and returns the result. |
996 | | - * The Mac can be re-used to produce further results from the same key. |
| 1020 | + * Returns the digest for the file. |
997 | 1021 | * |
998 | | - * @return the result as a byte array |
| 1022 | + * @param valueToDigest the file to use |
| 1023 | + * @return the digest |
| 1024 | + * @throws IOException |
| 1025 | + * If an I/O error occurs. |
999 | 1026 | * @since 1.11 |
1000 | 1027 | */ |
1001 | | - public byte[] doFinal() { |
1002 | | - return mac.doFinal(); |
| 1028 | + public byte[] hmac(final File valueToDigest) throws IOException { |
| 1029 | + final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(valueToDigest)); |
| 1030 | + try { |
| 1031 | + return hmac(stream); |
| 1032 | + } finally { |
| 1033 | + stream.close(); |
| 1034 | + } |
1003 | 1035 | } |
1004 | 1036 |
|
1005 | 1037 | /** |
1006 | | - * Finishes the MAC operation and returns the result. |
1007 | | - * The Mac can be re-used to produce further results from the same key. |
| 1038 | + * Returns the digest for the file. |
1008 | 1039 | * |
1009 | | - * @return the result as a Hex String |
| 1040 | + * @param valueToDigest the file to use |
| 1041 | + * @return the digest as a hex String |
| 1042 | + * @throws IOException |
| 1043 | + * If an I/O error occurs. |
1010 | 1044 | * @since 1.11 |
1011 | 1045 | */ |
1012 | | - public String doFinalHex() { |
1013 | | - return Hex.encodeHexString(mac.doFinal()); |
| 1046 | + public String hmacHex(File valueToDigest) throws IOException { |
| 1047 | + return Hex.encodeHexString(hmac(valueToDigest)); |
1014 | 1048 | } |
1015 | 1049 |
|
1016 | 1050 | } |
0 commit comments