1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.codec.digest;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import java.security.NoSuchAlgorithmException;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.List;
26
27 import javax.crypto.Mac;
28
29 import org.apache.commons.lang3.JavaVersion;
30 import org.apache.commons.lang3.SystemUtils;
31 import org.junit.After;
32 import org.junit.Assert;
33 import org.junit.Assume;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.Parameterized;
38 import org.junit.runners.Parameterized.Parameters;
39
40
41
42
43
44
45 @RunWith(Parameterized.class)
46 public class HmacAlgorithmsTest {
47
48 static final String STANDARD_KEY_STRING = "key";
49
50 static final byte[] STANDARD_KEY_BYTES = STANDARD_KEY_STRING.getBytes();
51
52 static final byte[] STANDARD_MD5_RESULT_BYTES = new byte[] { -128, 7, 7, 19, 70, 62, 119, 73, -71, 12, 45, -62, 73,
53 17, -30, 117 };
54
55 static final String STANDARD_MD5_RESULT_STRING = "80070713463e7749b90c2dc24911e275";
56
57 static final String STANDARD_PHRASE_STRING = "The quick brown fox jumps over the lazy dog";
58
59 static final byte[] STANDARD_PHRASE_BYTES = STANDARD_PHRASE_STRING.getBytes();
60
61 static final byte[] STANDARD_SHA1_RESULT_BYTES = new byte[] { -34, 124, -101, -123, -72, -73, -118, -90, -68, -118,
62 122, 54, -9, 10, -112, 112, 28, -99, -76, -39 };
63
64 static final String STANDARD_SHA1_RESULT_STRING = "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9";
65
66 static final byte[] STANDARD_SHA224_RESULT_BYTES = new byte[] { -120, -1, -117, 84, 103, 93, 57, -72, -9, 35, 34,
67 -26, 95, -7, 69, -59, 45, -106, 55, -103, -120, -83, -94, 86, 57, 116, 126, 105 };
68
69 static final String STANDARD_SHA224_RESULT_STRING = "88ff8b54675d39b8f72322e65ff945c52d96379988ada25639747e69";
70
71 static final byte[] STANDARD_SHA256_RESULT_BYTES = new byte[] { -9, -68, -125, -12, 48, 83, -124, 36, -79, 50, -104,
72 -26, -86, 111, -79, 67, -17, 77, 89, -95, 73, 70, 23, 89, -105, 71, -99, -68, 45, 26, 60, -40 };
73
74 static final String STANDARD_SHA256_RESULT_STRING = "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8";
75
76 static final byte[] STANDARD_SHA384_RESULT_BYTES = new byte[] { -41, -12, 114, 126, 44, 11, 57, -82, 15, 30, 64,
77 -52, -106, -10, 2, 66, -43, -73, -128, 24, 65, -50, -90, -4, 89, 44, 93, 62, 26, -27, 7, 0, 88, 42, -106,
78 -49, 53, -31, -27, 84, -103, 95, -28, -32, 51, -127, -62, 55 };
79
80 static final String STANDARD_SHA384_RESULT_STRING = "D7F4727E2C0B39AE0F1E40CC96F60242D5B7801841CEA6FC592C5D3E1AE50700582A96CF35E1E554995FE4E03381C237"
81 .toLowerCase();
82
83 static final byte[] STANDARD_SHA512_RESULT_BYTES = new byte[] { -76, 42, -16, -112, 87, -70, -63, -30, -44, 23, 8,
84 -28, -118, -112, 46, 9, -75, -1, 127, 18, -85, 66, -118, 79, -24, 102, 83, -57, 61, -46, 72, -5, -126, -7,
85 72, -91, 73, -9, -73, -111, -91, -76, 25, 21, -18, 77, 30, -61, -109, 83, 87, -28, -30, 49, 114, 80, -48,
86 55, 42, -6, 46, -66, -21, 58 };
87
88 static final String STANDARD_SHA512_RESULT_STRING = "B42AF09057BAC1E2D41708E48A902E09B5FF7F12AB428A4FE86653C73DD248FB82F948A549F7B791A5B41915EE4D1EC3935357E4E2317250D0372AFA2EBEEB3A"
89 .toLowerCase();
90
91 private static final byte[] EMPTY_BYTE_ARRAY = new byte[] {};
92
93
94 @Parameters(name = "{0}")
95 public static Collection<Object[]> data() {
96 List<Object[]> list = Arrays.asList(
97
98 new Object[][] { { HmacAlgorithms.HMAC_MD5, STANDARD_MD5_RESULT_BYTES, STANDARD_MD5_RESULT_STRING },
99 { HmacAlgorithms.HMAC_SHA_1, STANDARD_SHA1_RESULT_BYTES, STANDARD_SHA1_RESULT_STRING },
100 { HmacAlgorithms.HMAC_SHA_256, STANDARD_SHA256_RESULT_BYTES, STANDARD_SHA256_RESULT_STRING },
101 { HmacAlgorithms.HMAC_SHA_384, STANDARD_SHA384_RESULT_BYTES, STANDARD_SHA384_RESULT_STRING },
102 { HmacAlgorithms.HMAC_SHA_512, STANDARD_SHA512_RESULT_BYTES, STANDARD_SHA512_RESULT_STRING } });
103
104 if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8)) {
105 list = new ArrayList<Object[]>(list);
106 list.add(new Object[] {HmacAlgorithms.HMAC_SHA_224, STANDARD_SHA224_RESULT_BYTES, STANDARD_SHA224_RESULT_STRING});
107 }
108 return list;
109 }
110
111 private DigestUtilsTest digestUtilsTest;
112
113 private final HmacAlgorithms hmacAlgorithm;
114
115 private final byte[] standardResultBytes;
116 private final String standardResultString;
117
118 public HmacAlgorithmsTest(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) {
119 Assume.assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
120 this.hmacAlgorithm = hmacAlgorithm;
121 this.standardResultBytes = standardResultBytes;
122 this.standardResultString = standardResultString;
123 }
124
125 @Before
126 public void setUp() throws Exception {
127 digestUtilsTest = new DigestUtilsTest();
128 digestUtilsTest.setUp();
129 }
130
131 @After
132 public void tearDown() throws Exception {
133 digestUtilsTest.tearDown();
134 digestUtilsTest = null;
135 }
136
137 @Test
138 public void testAlgorithm() throws IOException, NoSuchAlgorithmException {
139 final String algorithm = hmacAlgorithm.getName();
140 Assert.assertNotNull(algorithm);
141 Assert.assertFalse(algorithm.isEmpty());
142 Assume.assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
143 Mac.getInstance(algorithm);
144 }
145
146 @Test(expected = IllegalArgumentException.class)
147 public void testGetHmacEmptyKey() {
148 HmacUtils.getInitializedMac(hmacAlgorithm, EMPTY_BYTE_ARRAY);
149 }
150
151 @Test(expected = IllegalArgumentException.class)
152 public void testGetHmacNullKey() {
153 HmacUtils.getInitializedMac(hmacAlgorithm, null);
154 }
155
156 @Test(expected = IllegalArgumentException.class)
157 public void testHmacFailByteArray() throws IOException {
158 new HmacUtils(hmacAlgorithm, (byte[]) null).hmac(STANDARD_PHRASE_BYTES);
159 }
160
161 @Test(expected = IllegalArgumentException.class)
162 public void testHmacFailInputStream() throws IOException {
163 new HmacUtils(hmacAlgorithm, (byte[]) null).hmac(new ByteArrayInputStream(STANDARD_PHRASE_BYTES));
164 }
165
166 @Test(expected = IllegalArgumentException.class)
167 public void testHmacFailString() throws IOException {
168 new HmacUtils(hmacAlgorithm, (String) null).hmac(STANDARD_PHRASE_STRING);
169 }
170
171 @Test(expected = IllegalArgumentException.class)
172 public void testHmacHexFailByteArray() throws IOException {
173 new HmacUtils(hmacAlgorithm, (byte[]) null).hmac(STANDARD_PHRASE_BYTES);
174 }
175
176 @Test(expected = IllegalArgumentException.class)
177 public void testHmacHexFailInputStream() throws IOException {
178 new HmacUtils(hmacAlgorithm, (byte[]) null).hmac(new ByteArrayInputStream(STANDARD_PHRASE_BYTES));
179 }
180
181 @Test(expected = IllegalArgumentException.class)
182 public void testHmacHexFailString() throws IOException {
183 new HmacUtils(hmacAlgorithm, (String) null).hmac(STANDARD_PHRASE_STRING);
184 }
185
186 @Test
187 public void testInitializedMac() throws IOException {
188 final Mac mac = HmacUtils.getInitializedMac(hmacAlgorithm, STANDARD_KEY_BYTES);
189 final Mac mac2 = HmacUtils.getInitializedMac(hmacAlgorithm.getName(), STANDARD_KEY_BYTES);
190 Assert.assertArrayEquals(standardResultBytes, HmacUtils.updateHmac(mac, STANDARD_PHRASE_STRING).doFinal());
191 Assert.assertArrayEquals(standardResultBytes, HmacUtils.updateHmac(mac2, STANDARD_PHRASE_STRING).doFinal());
192 }
193
194 @Test
195 public void testMacByteArary() throws IOException {
196 Assert.assertArrayEquals(standardResultBytes, new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmac(STANDARD_PHRASE_BYTES));
197 }
198
199 @Test
200 public void testMacHexByteArray() throws IOException {
201 Assert.assertEquals(standardResultString, new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmacHex(STANDARD_PHRASE_BYTES));
202 }
203
204 @Test
205 public void testMacHexInputStream() throws IOException {
206 Assert.assertEquals(standardResultString,
207 new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmacHex(new ByteArrayInputStream(STANDARD_PHRASE_BYTES)));
208 }
209
210 @Test
211 public void testMacHexString() throws IOException {
212 Assert.assertEquals(standardResultString, new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmacHex(STANDARD_PHRASE_STRING));
213 }
214
215 @Test
216 public void testMacInputStream() throws IOException {
217 Assert.assertArrayEquals(standardResultBytes,
218 new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmac(new ByteArrayInputStream(STANDARD_PHRASE_BYTES)));
219 }
220
221 @Test
222 public void testMacString() throws IOException {
223 Assert.assertArrayEquals(standardResultBytes, new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmac(STANDARD_PHRASE_STRING));
224 }
225
226 }