Skip to content

Commit 52cfbfd

Browse files
committed
Simplify; merge update() and doFinal() methods so code looks more like original static methods.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1745063 13f79535-47bb-0310-9956-ffa450edef68
1 parent cff360a commit 52cfbfd

1 file changed

Lines changed: 94 additions & 60 deletions

File tree

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

Lines changed: 94 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@
4343
* Sample usage:
4444
* <pre>
4545
* 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!
4747
* 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);
4949
* // Mac re-use
5050
* 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"));
5353
* </pre>
5454
* @since 1.10
5555
* @version $Id$
@@ -898,119 +898,153 @@ public static HmacUtils use(final HmacAlgorithms algorithm, final byte[] key) {
898898
}
899899

900900
/**
901-
* Updates the stored {@link Mac} with the value.
901+
* Returns the digest for the input data.
902902
*
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.
908907
* @since 1.11
909908
*/
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);
913911
}
914912

915913
/**
916-
* Updates the stored {@link Mac} with the value.
914+
* Returns the digest for the input data.
917915
*
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.
923933
* @since 1.11
924934
*/
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) {
926962
mac.update(valueToDigest);
927-
return this;
963+
return mac.doFinal();
928964
}
929965

930966
/**
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.
938973
* @since 1.11
939974
*/
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));
943977
}
944978

945979
/**
946-
* Updates the stored {@link Mac} with the value.
980+
* Returns the digest for the stream.
947981
*
948982
* @param valueToDigest
949-
* the value to update the {@link Mac} with
983+
* the data to use
950984
* <p>
951985
* The InputStream must not be null and will not be closed
952986
* </p>
953-
* @return the updated instance
987+
* @return the digest
954988
* @throws IOException
955989
* If an I/O error occurs.
956-
* @throws IllegalStateException
957-
* If the Mac was not initialized
958990
* @since 1.11
959991
*/
960-
public HmacUtils update(final InputStream valueToDigest) throws IOException {
992+
public byte[] hmac(InputStream valueToDigest) throws IOException {
961993
final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
962994
int read;
963995

964996
while ((read = valueToDigest.read(buffer, 0, STREAM_BUFFER_LENGTH) ) > -1) {
965997
mac.update(buffer, 0, read);
966998
}
967-
return this;
999+
return mac.doFinal();
9681000
}
9691001

9701002
/**
971-
* Updates the stored {@link Mac} with the value.
1003+
* Returns the digest for the stream.
9721004
*
9731005
* @param valueToDigest
974-
* the value to update the {@link Mac} with
1006+
* the data to use
9751007
* <p>
9761008
* The InputStream must not be null and will not be closed
9771009
* </p>
978-
* @return the updated instance
1010+
* @return the digest as a hex String
9791011
* @throws IOException
9801012
* If an I/O error occurs.
981-
* @throws IllegalStateException
982-
* If the Mac was not initialized
9831013
* @since 1.11
9841014
*/
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));
9921017
}
9931018

9941019
/**
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.
9971021
*
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.
9991026
* @since 1.11
10001027
*/
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+
}
10031035
}
10041036

10051037
/**
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.
10081039
*
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.
10101044
* @since 1.11
10111045
*/
1012-
public String doFinalHex() {
1013-
return Hex.encodeHexString(mac.doFinal());
1046+
public String hmacHex(File valueToDigest) throws IOException {
1047+
return Hex.encodeHexString(hmac(valueToDigest));
10141048
}
10151049

10161050
}

0 commit comments

Comments
 (0)