View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.codec.digest;
18  
19  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.IOException;
25  
26  import javax.crypto.Mac;
27  import javax.crypto.spec.SecretKeySpec;
28  
29  import org.apache.commons.codec.binary.Hex;
30  import org.apache.commons.codec.binary.StringUtils;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Tests HmacUtils methods.
35   */
36  public class HmacUtilsTest {
37  
38      @SuppressWarnings("deprecation") // most of the static methods are deprecated
39      @Test
40      public void testEmptyKey() {
41          assertThrows(IllegalArgumentException.class, () -> HmacUtils.getHmacMd5(new byte[] {}));
42      }
43  
44      @SuppressWarnings("deprecation") // most of the static methods are deprecated
45      @Test
46      public void testGetHMac() {
47          assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES,
48           HmacUtils.getHmacMd5(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
49          assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_BYTES,
50           HmacUtils.getHmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
51          assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_BYTES,
52           HmacUtils.getHmacSha256(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
53          assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_BYTES,
54           HmacUtils.getHmacSha384(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
55          assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_BYTES,
56                  HmacUtils.getHmacSha512(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
57      }
58  
59      @SuppressWarnings("deprecation") // most of the static methods are deprecated
60      @Test
61      public void testHmacMd5Hex() throws IOException {
62          assertEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_STRING,
63                  HmacUtils.hmacMd5Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, "The quick brown fox jumps over the lazy dog"));
64          assertEquals("750c783e6ab0b503eaa86e310a5db738", HmacUtils.hmacMd5Hex("Jefe", "what do ya want for nothing?"));
65          assertEquals(
66                  "750c783e6ab0b503eaa86e310a5db738",
67                  HmacUtils.hmacMd5Hex("Jefe".getBytes(),
68                          new ByteArrayInputStream("what do ya want for nothing?".getBytes())));
69      }
70  
71      @SuppressWarnings("deprecation") // most of the static methods are deprecated
72      @Test
73      public void testHmacSha1Hex() throws IOException {
74          assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, HmacUtils.hmacSha1Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
75          assertEquals("f42bb0eeb018ebbd4597ae7213711ec60760843f", HmacUtils.hmacSha1Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, ""));
76          assertEquals("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
77                  HmacUtils.hmacSha1Hex("Jefe", "what do ya want for nothing?"));
78          assertEquals(
79                  "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
80                  HmacUtils.hmacSha1Hex("Jefe".getBytes(),
81                          new ByteArrayInputStream("what do ya want for nothing?".getBytes())));
82      }
83  
84      @SuppressWarnings("deprecation") // most of the static methods are deprecated
85      @Test
86      public void testHmacSha1UpdateWithByteArray() {
87          final Mac mac = HmacUtils.getHmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES);
88          HmacUtils.updateHmac(mac, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES);
89          assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, Hex.encodeHexString(mac.doFinal()));
90          HmacUtils.updateHmac(mac, "".getBytes());
91          assertEquals("f42bb0eeb018ebbd4597ae7213711ec60760843f", Hex.encodeHexString(mac.doFinal()));
92      }
93  
94      @SuppressWarnings("deprecation") // most of the static methods are deprecated
95      @Test
96      public void testHmacSha1UpdateWithInputStream() throws IOException {
97          final Mac mac = HmacUtils.getHmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES);
98          HmacUtils.updateHmac(mac, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
99          assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, Hex.encodeHexString(mac.doFinal()));
100         HmacUtils.updateHmac(mac, new ByteArrayInputStream("".getBytes()));
101         assertEquals("f42bb0eeb018ebbd4597ae7213711ec60760843f", Hex.encodeHexString(mac.doFinal()));
102     }
103 
104     @SuppressWarnings("deprecation") // most of the static methods are deprecated
105     @Test
106     public void testHmacSha1UpdateWithString() {
107         final Mac mac = HmacUtils.getHmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES);
108         HmacUtils.updateHmac(mac, HmacAlgorithmsTest.STANDARD_PHRASE_STRING);
109         assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, Hex.encodeHexString(mac.doFinal()));
110         HmacUtils.updateHmac(mac, "");
111         assertEquals("f42bb0eeb018ebbd4597ae7213711ec60760843f", Hex.encodeHexString(mac.doFinal()));
112     }
113 
114     @Test
115     public void testInitializedMac() {
116         final Mac md5Mac = HmacUtils.getInitializedMac(HmacAlgorithms.HMAC_MD5, HmacAlgorithmsTest.STANDARD_KEY_BYTES);
117         final Mac md5Mac2 = HmacUtils.getInitializedMac("HmacMD5", HmacAlgorithmsTest.STANDARD_KEY_BYTES);
118         assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES, HmacUtils.updateHmac(md5Mac, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)
119                 .doFinal());
120         assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES, HmacUtils.updateHmac(md5Mac2, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)
121                 .doFinal());
122     }
123 
124     @Test
125     public void testInitializedMacNullAlgo() {
126         assertThrows(IllegalArgumentException.class, () -> HmacUtils.getInitializedMac((String) null, HmacAlgorithmsTest.STANDARD_KEY_BYTES));
127     }
128 
129     @Test
130     public void testInitializedMacNullKey() {
131         assertThrows(IllegalArgumentException.class, () -> HmacUtils.getInitializedMac(HmacAlgorithms.HMAC_MD5, null));
132     }
133 
134     @Test
135     public void testInternalNoSuchAlgorithmException() {
136         assertThrows(IllegalArgumentException.class, () -> HmacUtils.getInitializedMac("Bogus Bogus", StringUtils.getBytesUtf8("akey")));
137     }
138 
139     @SuppressWarnings("deprecation") // most of the static methods are deprecated
140     @Test
141     public void testMd5HMac() throws IOException {
142         assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES,
143                 HmacUtils.hmacMd5(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
144         assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES,
145                 HmacUtils.hmacMd5(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)));
146         assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES,
147                 HmacUtils.hmacMd5(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
148         assertEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_STRING, HmacUtils.hmacMd5Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
149         assertEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_STRING,
150                 HmacUtils.hmacMd5Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)));
151         assertEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_STRING,
152                 HmacUtils.hmacMd5Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
153     }
154 
155     @SuppressWarnings("deprecation") // most of the static methods are deprecated
156     @Test
157     public void testMd5HMacFail() {
158         assertThrows(IllegalArgumentException.class, () -> HmacUtils.hmacMd5((byte[]) null, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
159     }
160 
161     @SuppressWarnings("deprecation") // most of the static methods are deprecated
162     @Test
163     public void testNullKey() {
164         assertThrows(IllegalArgumentException.class, () -> HmacUtils.getHmacMd5(null));
165     }
166 
167     @Test
168     public void testSecretKeySpecAllowsEmptyKeys() {
169         assertThrows(IllegalArgumentException.class, () -> new SecretKeySpec(new byte[] {}, "HmacMD5"));
170     }
171 
172     @SuppressWarnings("deprecation") // most of the static methods are deprecated
173     @Test
174     public void testSha1HMac() throws IOException {
175         assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_BYTES,
176                 HmacUtils.hmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
177         assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_BYTES,
178                 HmacUtils.hmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)));
179         assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_BYTES,
180                 HmacUtils.hmacSha1(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
181         assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING,
182                 HmacUtils.hmacSha1Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
183         assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING,
184                 HmacUtils.hmacSha1Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)));
185         assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING,
186                 HmacUtils.hmacSha1Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
187     }
188 
189     @SuppressWarnings("deprecation") // most of the static methods are deprecated
190     @Test
191     public void testSha1HMacFail() {
192         assertThrows(IllegalArgumentException.class, () -> HmacUtils.hmacSha1((byte[]) null, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
193     }
194 
195     @SuppressWarnings("deprecation") // most of the static methods are deprecated
196     @Test
197     public void testSha256HMac() throws IOException {
198         assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_BYTES,
199                 HmacUtils.hmacSha256(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
200         assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_BYTES,
201                 HmacUtils.hmacSha256(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)));
202         assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_BYTES,
203                 HmacUtils.hmacSha256(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
204         assertEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_STRING,
205                 HmacUtils.hmacSha256Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
206         assertEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_STRING,
207                 HmacUtils.hmacSha256Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)));
208         assertEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_STRING,
209                 HmacUtils.hmacSha256Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
210     }
211 
212     @SuppressWarnings("deprecation") // most of the static methods are deprecated
213     @Test
214     public void testSha256HMacFail() {
215         assertThrows(IllegalArgumentException.class, () -> HmacUtils.hmacSha256((byte[]) null, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
216     }
217 
218     @SuppressWarnings("deprecation") // most of the static methods are deprecated
219     @Test
220     public void testSha384HMac() throws IOException {
221         assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_BYTES,
222                 HmacUtils.hmacSha384(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
223         assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_BYTES,
224                 HmacUtils.hmacSha384(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)));
225         assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_BYTES,
226                 HmacUtils.hmacSha384(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
227         assertEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_STRING,
228                 HmacUtils.hmacSha384Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
229         assertEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_STRING,
230                 HmacUtils.hmacSha384Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)));
231         assertEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_STRING,
232                 HmacUtils.hmacSha384Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
233     }
234 
235     @SuppressWarnings("deprecation") // most of the static methods are deprecated
236     @Test
237     public void testSha384HMacFail() {
238         assertThrows(IllegalArgumentException.class, () -> HmacUtils.hmacSha384((byte[]) null, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
239     }
240 
241     @SuppressWarnings("deprecation") // most of the static methods are deprecated
242     @Test
243     public void testSha512HMac() throws IOException {
244         assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_BYTES,
245                 HmacUtils.hmacSha512(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
246         assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_BYTES,
247                 HmacUtils.hmacSha512(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)));
248         assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_BYTES,
249                 HmacUtils.hmacSha512(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
250         assertEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_STRING,
251                 HmacUtils.hmacSha512Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
252         assertEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_STRING,
253                 HmacUtils.hmacSha512Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)));
254         assertEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_STRING,
255                 HmacUtils.hmacSha512Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
256     }
257 
258     @SuppressWarnings("deprecation") // most of the static methods are deprecated
259     @Test
260     public void testSha512HMacFail() {
261         assertThrows(IllegalArgumentException.class, () -> HmacUtils.hmacSha512((byte[]) null, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
262     }
263 }