This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit e5d7a7942a4049739904e40845fb5e6b083f3509
Author: Gary D. Gregory <garydgreg...@gmail.com>
AuthorDate: Sat Mar 15 15:22:48 2025 -0400

    Add HmacUtils.hmac[Hex](Path)
    
    - Add HmacUtils.hmacHex(Path)
    - Add HmacUtils.hmac(Path)
    - Next version will be 1.19.0
    - Add missing tests
---
 pom.xml                                            |  8 ++---
 src/changes/changes.xml                            |  4 ++-
 .../org/apache/commons/codec/digest/HmacUtils.java | 36 +++++++++++++++++++---
 .../commons/codec/digest/HmacAlgorithmsTest.java   | 33 +++++++++++++++++++-
 4 files changed, 70 insertions(+), 11 deletions(-)

diff --git a/pom.xml b/pom.xml
index e2787f39..2343d1c0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@ limitations under the License.
   </parent>
   <groupId>commons-codec</groupId>
   <artifactId>commons-codec</artifactId>
-  <version>1.18.1-SNAPSHOT</version>
+  <version>1.19.0-SNAPSHOT</version>
   <name>Apache Commons Codec</name>
   <inceptionYear>2002</inceptionYear>
   <description>
@@ -93,9 +93,9 @@ limitations under the License.
     
<checkstyle.header.file>${basedir}/src/conf/checkstyle-header.txt</checkstyle.header.file>
     
<checkstyle.config.file>${basedir}/src/conf/checkstyle.xml</checkstyle.config.file>
     <!-- Commons Release Plugin -->
-    <commons.release.version>1.18.0</commons.release.version>
-    <commons.bc.version>1.17.1</commons.bc.version>
-    <commons.release.next>1.18.1</commons.release.next>
+    <commons.release.version>1.19.0</commons.release.version>
+    <commons.bc.version>1.18.0</commons.bc.version>
+    <commons.release.next>1.19.1</commons.release.next>
     <commons.rc.version>RC1</commons.rc.version>
     <commons.release.isDistModule>true</commons.release.isDistModule>
     
<commons.distSvnStagingUrl>scm:svn:https://dist.apache.org/repos/dist/dev/commons/${commons.componentid}</commons.distSvnStagingUrl>
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 26257f40..d4ea2a0b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -43,11 +43,13 @@ The <action> type attribute can be add,update,fix,remove.
     <author>Apache Commons Developers</author>
   </properties>
   <body>
-    <release version="1.18.1" date="YYYY-MM-DD" description="This is a feature 
and maintenance release. Java 8 or later is required.">
+    <release version="1.19.0" date="YYYY-MM-DD" description="This is a feature 
and maintenance release. Java 8 or later is required.">
       <!-- FIX -->
       <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>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Refactor 
DigestUtils.updateDigest(MessageDigest, File) to use NIO.</action>
       <!-- ADD -->
+      <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
HmacUtils.hmac(Path).</action>      
+      <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
HmacUtils.hmacHex(Path).</action>      
       <!-- UPDATE -->
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump 
org.apache.commons:commons-parent from 79 to 81.</action>
     </release>
diff --git a/src/main/java/org/apache/commons/codec/digest/HmacUtils.java 
b/src/main/java/org/apache/commons/codec/digest/HmacUtils.java
index 90dc337d..89bceb6d 100644
--- a/src/main/java/org/apache/commons/codec/digest/HmacUtils.java
+++ b/src/main/java/org/apache/commons/codec/digest/HmacUtils.java
@@ -19,10 +19,11 @@ package org.apache.commons.codec.digest;
 
 import java.io.BufferedInputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.ByteBuffer;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.security.InvalidKeyException;
 import java.security.Key;
 import java.security.NoSuchAlgorithmException;
@@ -971,9 +972,7 @@ public final class HmacUtils {
      * @since 1.11
      */
     public byte[] hmac(final File valueToDigest) throws IOException {
-        try (BufferedInputStream stream = new BufferedInputStream(new 
FileInputStream(valueToDigest))) {
-            return hmac(stream);
-        }
+        return hmac(valueToDigest.toPath());
     }
 
     /**
@@ -992,13 +991,27 @@ public final class HmacUtils {
     public byte[] hmac(final InputStream valueToDigest) throws IOException {
         final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
         int read;
-
         while ((read = valueToDigest.read(buffer, 0, STREAM_BUFFER_LENGTH)) > 
-1) {
             mac.update(buffer, 0, read);
         }
         return mac.doFinal();
     }
 
+    /**
+     * Returns the digest for the file.
+     *
+     * @param valueToDigest the path to use
+     * @return the digest
+     * @throws IOException
+     *             If an I/O error occurs.
+     * @since 1.19.0
+     */
+    public byte[] hmac(final Path valueToDigest) throws IOException {
+        try (BufferedInputStream stream = new 
BufferedInputStream(Files.newInputStream(valueToDigest))) {
+            return hmac(stream);
+        }
+    }
+
     /**
      * Returns the digest for the input data.
      *
@@ -1062,6 +1075,19 @@ public final class HmacUtils {
         return Hex.encodeHexString(hmac(valueToDigest));
     }
 
+    /**
+     * Returns the digest for the path.
+     *
+     * @param valueToDigest the path to use
+     * @return the digest as a hexadecimal String
+     * @throws IOException
+     *             If an I/O error occurs.
+     * @since 1.19.0
+     */
+    public String hmacHex(final Path valueToDigest) throws IOException {
+        return Hex.encodeHexString(hmac(valueToDigest));
+    }
+
     /**
      * Returns the digest for the input data.
      *
diff --git 
a/src/test/java/org/apache/commons/codec/digest/HmacAlgorithmsTest.java 
b/src/test/java/org/apache/commons/codec/digest/HmacAlgorithmsTest.java
index 722b2e1d..43f6a92a 100644
--- a/src/test/java/org/apache/commons/codec/digest/HmacAlgorithmsTest.java
+++ b/src/test/java/org/apache/commons/codec/digest/HmacAlgorithmsTest.java
@@ -25,6 +25,9 @@ import static org.junit.jupiter.api.Assumptions.assumeTrue;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
 import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -36,7 +39,9 @@ import javax.crypto.Mac;
 import org.apache.commons.lang3.JavaVersion;
 import org.apache.commons.lang3.SystemUtils;
 import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.io.TempDir;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.Arguments;
 import org.junit.jupiter.params.provider.MethodSource;
@@ -87,6 +92,11 @@ public class HmacAlgorithmsTest {
 
     private static final byte[] EMPTY_BYTE_ARRAY = {};
 
+    @TempDir
+    static Path TempDir;
+
+    static Path TempFile;
+
     // TODO HMAC_SHA_224
     public static Stream<Arguments> data() {
         List<Arguments> list = Arrays.asList(
@@ -104,8 +114,13 @@ public class HmacAlgorithmsTest {
         return list.stream();
     }
 
-    private DigestUtilsTest digestUtilsTest;
+    @BeforeAll
+    public static void init() throws IOException {
+        TempFile = 
Files.createFile(TempDir.resolve(HmacAlgorithmsTest.class.getSimpleName()));
+        Files.write(TempFile, STANDARD_PHRASE_BYTES, 
StandardOpenOption.CREATE);
+    }
 
+    private DigestUtilsTest digestUtilsTest;
     @BeforeEach
     public void setUp() throws Exception {
         digestUtilsTest = new DigestUtilsTest();
@@ -210,6 +225,22 @@ public class HmacAlgorithmsTest {
         assertEquals(standardResultString, new HmacUtils(hmacAlgorithm, 
STANDARD_KEY_BYTES).hmacHex(STANDARD_PHRASE_BYTES));
     }
 
+    @ParameterizedTest
+    @MethodSource("data")
+    public void testMacHexFile(final HmacAlgorithms hmacAlgorithm, final 
byte[] standardResultBytes, final String standardResultString)
+            throws IOException {
+        assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
+        assertEquals(standardResultString, new HmacUtils(hmacAlgorithm, 
STANDARD_KEY_BYTES).hmacHex(TempFile.toFile()));
+    }
+
+    @ParameterizedTest
+    @MethodSource("data")
+    public void testMacHexPath(final HmacAlgorithms hmacAlgorithm, final 
byte[] standardResultBytes, final String standardResultString)
+            throws IOException {
+        assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
+        assertEquals(standardResultString, new HmacUtils(hmacAlgorithm, 
STANDARD_KEY_BYTES).hmacHex(TempFile));
+    }
+
     @ParameterizedTest
     @MethodSource("data")
     public void testMacHexInputStream(final HmacAlgorithms hmacAlgorithm, 
final byte[] standardResultBytes, final String standardResultString)

Reply via email to