Skip to content

Commit e5d7a79

Browse files
committed
Add HmacUtils.hmac[Hex](Path)
- Add HmacUtils.hmacHex(Path) - Add HmacUtils.hmac(Path) - Next version will be 1.19.0 - Add missing tests
1 parent 1848f8f commit e5d7a79

File tree

4 files changed

+70
-11
lines changed

4 files changed

+70
-11
lines changed

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ limitations under the License.
2828
</parent>
2929
<groupId>commons-codec</groupId>
3030
<artifactId>commons-codec</artifactId>
31-
<version>1.18.1-SNAPSHOT</version>
31+
<version>1.19.0-SNAPSHOT</version>
3232
<name>Apache Commons Codec</name>
3333
<inceptionYear>2002</inceptionYear>
3434
<description>
@@ -93,9 +93,9 @@ limitations under the License.
9393
<checkstyle.header.file>${basedir}/src/conf/checkstyle-header.txt</checkstyle.header.file>
9494
<checkstyle.config.file>${basedir}/src/conf/checkstyle.xml</checkstyle.config.file>
9595
<!-- Commons Release Plugin -->
96-
<commons.release.version>1.18.0</commons.release.version>
97-
<commons.bc.version>1.17.1</commons.bc.version>
98-
<commons.release.next>1.18.1</commons.release.next>
96+
<commons.release.version>1.19.0</commons.release.version>
97+
<commons.bc.version>1.18.0</commons.bc.version>
98+
<commons.release.next>1.19.1</commons.release.next>
9999
<commons.rc.version>RC1</commons.rc.version>
100100
<commons.release.isDistModule>true</commons.release.isDistModule>
101101
<commons.distSvnStagingUrl>scm:svn:https://dist.apache.org/repos/dist/dev/commons/${commons.componentid}</commons.distSvnStagingUrl>

src/changes/changes.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ The <action> type attribute can be add,update,fix,remove.
4343
<author>Apache Commons Developers</author>
4444
</properties>
4545
<body>
46-
<release version="1.18.1" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
46+
<release version="1.19.0" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
4747
<!-- FIX -->
4848
<action type="fix" dev="ggregory" due-to="Gary Gregory">Remove -nouses directive from maven-bundle-plugin. OSGi package imports now state 'uses' definitions for package imports, this doesn't affect JPMS (from org.apache.commons:commons-parent:80).</action>
4949
<action type="fix" dev="ggregory" due-to="Gary Gregory">Refactor DigestUtils.updateDigest(MessageDigest, File) to use NIO.</action>
5050
<!-- ADD -->
51+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add HmacUtils.hmac(Path).</action>
52+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add HmacUtils.hmacHex(Path).</action>
5153
<!-- UPDATE -->
5254
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 79 to 81.</action>
5355
</release>

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919

2020
import java.io.BufferedInputStream;
2121
import java.io.File;
22-
import java.io.FileInputStream;
2322
import java.io.IOException;
2423
import java.io.InputStream;
2524
import java.nio.ByteBuffer;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
2627
import java.security.InvalidKeyException;
2728
import java.security.Key;
2829
import java.security.NoSuchAlgorithmException;
@@ -971,9 +972,7 @@ public byte[] hmac(final ByteBuffer valueToDigest) {
971972
* @since 1.11
972973
*/
973974
public byte[] hmac(final File valueToDigest) throws IOException {
974-
try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(valueToDigest))) {
975-
return hmac(stream);
976-
}
975+
return hmac(valueToDigest.toPath());
977976
}
978977

979978
/**
@@ -992,13 +991,27 @@ public byte[] hmac(final File valueToDigest) throws IOException {
992991
public byte[] hmac(final InputStream valueToDigest) throws IOException {
993992
final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
994993
int read;
995-
996994
while ((read = valueToDigest.read(buffer, 0, STREAM_BUFFER_LENGTH)) > -1) {
997995
mac.update(buffer, 0, read);
998996
}
999997
return mac.doFinal();
1000998
}
1001999

1000+
/**
1001+
* Returns the digest for the file.
1002+
*
1003+
* @param valueToDigest the path to use
1004+
* @return the digest
1005+
* @throws IOException
1006+
* If an I/O error occurs.
1007+
* @since 1.19.0
1008+
*/
1009+
public byte[] hmac(final Path valueToDigest) throws IOException {
1010+
try (BufferedInputStream stream = new BufferedInputStream(Files.newInputStream(valueToDigest))) {
1011+
return hmac(stream);
1012+
}
1013+
}
1014+
10021015
/**
10031016
* Returns the digest for the input data.
10041017
*
@@ -1062,6 +1075,19 @@ public String hmacHex(final InputStream valueToDigest) throws IOException {
10621075
return Hex.encodeHexString(hmac(valueToDigest));
10631076
}
10641077

1078+
/**
1079+
* Returns the digest for the path.
1080+
*
1081+
* @param valueToDigest the path to use
1082+
* @return the digest as a hexadecimal String
1083+
* @throws IOException
1084+
* If an I/O error occurs.
1085+
* @since 1.19.0
1086+
*/
1087+
public String hmacHex(final Path valueToDigest) throws IOException {
1088+
return Hex.encodeHexString(hmac(valueToDigest));
1089+
}
1090+
10651091
/**
10661092
* Returns the digest for the input data.
10671093
*

src/test/java/org/apache/commons/codec/digest/HmacAlgorithmsTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
import java.io.ByteArrayInputStream;
2727
import java.io.IOException;
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
30+
import java.nio.file.StandardOpenOption;
2831
import java.security.NoSuchAlgorithmException;
2932
import java.util.ArrayList;
3033
import java.util.Arrays;
@@ -36,7 +39,9 @@
3639
import org.apache.commons.lang3.JavaVersion;
3740
import org.apache.commons.lang3.SystemUtils;
3841
import org.junit.jupiter.api.AfterEach;
42+
import org.junit.jupiter.api.BeforeAll;
3943
import org.junit.jupiter.api.BeforeEach;
44+
import org.junit.jupiter.api.io.TempDir;
4045
import org.junit.jupiter.params.ParameterizedTest;
4146
import org.junit.jupiter.params.provider.Arguments;
4247
import org.junit.jupiter.params.provider.MethodSource;
@@ -87,6 +92,11 @@ public class HmacAlgorithmsTest {
8792

8893
private static final byte[] EMPTY_BYTE_ARRAY = {};
8994

95+
@TempDir
96+
static Path TempDir;
97+
98+
static Path TempFile;
99+
90100
// TODO HMAC_SHA_224
91101
public static Stream<Arguments> data() {
92102
List<Arguments> list = Arrays.asList(
@@ -104,8 +114,13 @@ public static Stream<Arguments> data() {
104114
return list.stream();
105115
}
106116

107-
private DigestUtilsTest digestUtilsTest;
117+
@BeforeAll
118+
public static void init() throws IOException {
119+
TempFile = Files.createFile(TempDir.resolve(HmacAlgorithmsTest.class.getSimpleName()));
120+
Files.write(TempFile, STANDARD_PHRASE_BYTES, StandardOpenOption.CREATE);
121+
}
108122

123+
private DigestUtilsTest digestUtilsTest;
109124
@BeforeEach
110125
public void setUp() throws Exception {
111126
digestUtilsTest = new DigestUtilsTest();
@@ -210,6 +225,22 @@ public void testMacHexByteArray(final HmacAlgorithms hmacAlgorithm, final byte[]
210225
assertEquals(standardResultString, new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmacHex(STANDARD_PHRASE_BYTES));
211226
}
212227

228+
@ParameterizedTest
229+
@MethodSource("data")
230+
public void testMacHexFile(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString)
231+
throws IOException {
232+
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
233+
assertEquals(standardResultString, new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmacHex(TempFile.toFile()));
234+
}
235+
236+
@ParameterizedTest
237+
@MethodSource("data")
238+
public void testMacHexPath(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString)
239+
throws IOException {
240+
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
241+
assertEquals(standardResultString, new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmacHex(TempFile));
242+
}
243+
213244
@ParameterizedTest
214245
@MethodSource("data")
215246
public void testMacHexInputStream(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString)

0 commit comments

Comments
 (0)