aboutsummaryrefslogtreecommitdiff
path: root/guava-tests/test/com/google/common/hash/MacHashFunctionTest.java
blob: 40c1f349e256aa1a46ea7a03d939f75d0b18ba61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * Copyright (C) 2015 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.hash;

import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.io.BaseEncoding.base16;
import static org.junit.Assert.assertThrows;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Table;
import com.google.common.testing.NullPointerTester;
import java.security.Key;
import java.util.Arrays;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import junit.framework.TestCase;
import org.checkerframework.checker.nullness.qual.Nullable;
import sun.security.jca.ProviderList;
import sun.security.jca.Providers;

/**
 * Tests for the MacHashFunction.
 *
 * @author Kurt Alfred Kluever
 */
public class MacHashFunctionTest extends TestCase {

  private static final ImmutableSet<String> INPUTS = ImmutableSet.of("", "Z", "foobar");

  private static final SecretKey MD5_KEY =
      new SecretKeySpec("secret key".getBytes(UTF_8), "HmacMD5");
  private static final SecretKey SHA1_KEY =
      new SecretKeySpec("secret key".getBytes(UTF_8), "HmacSHA1");
  private static final SecretKey SHA256_KEY =
      new SecretKeySpec("secret key".getBytes(UTF_8), "HmacSHA256");
  private static final SecretKey SHA512_KEY =
      new SecretKeySpec("secret key".getBytes(UTF_8), "HmacSHA512");

  // From http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Mac
  private static final ImmutableTable<String, SecretKey, HashFunction> ALGORITHMS =
      new ImmutableTable.Builder<String, SecretKey, HashFunction>()
          .put("HmacMD5", MD5_KEY, Hashing.hmacMd5(MD5_KEY))
          .put("HmacSHA1", SHA1_KEY, Hashing.hmacSha1(SHA1_KEY))
          .put("HmacSHA256", SHA256_KEY, Hashing.hmacSha256(SHA256_KEY))
          .put("HmacSHA512", SHA512_KEY, Hashing.hmacSha512(SHA512_KEY))
          .build();

  public void testNulls() {
    NullPointerTester tester =
        new NullPointerTester().setDefault(String.class, "HmacMD5").setDefault(Key.class, MD5_KEY);
    tester.testAllPublicConstructors(MacHashFunction.class);
    tester.testAllPublicInstanceMethods(new MacHashFunction("HmacMD5", MD5_KEY, "toString"));
  }

  public void testHashing() throws Exception {
    for (String stringToTest : INPUTS) {
      for (Table.Cell<String, SecretKey, HashFunction> cell : ALGORITHMS.cellSet()) {
        String algorithm = cell.getRowKey();
        SecretKey key = cell.getColumnKey();
        HashFunction hashFunc = cell.getValue();
        assertMacHashing(HashTestUtils.ascii(stringToTest), algorithm, key, hashFunc);
      }
    }
  }

  @AndroidIncompatible // sun.security
  public void testNoProviders() {
    ProviderList providers = Providers.getProviderList();
    Providers.setProviderList(ProviderList.newList());
    try {
      Hashing.hmacMd5(MD5_KEY);
      fail("expected ISE");
    } catch (IllegalStateException expected) {
    } finally {
      Providers.setProviderList(providers);
    }
  }

  public void testMultipleUpdates() throws Exception {
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(SHA1_KEY);
    mac.update("hello".getBytes(UTF_8));
    mac.update("world".getBytes(UTF_8));

    assertEquals(
        HashCode.fromBytes(mac.doFinal()),
        Hashing.hmacSha1(SHA1_KEY)
            .newHasher()
            .putString("hello", UTF_8)
            .putString("world", UTF_8)
            .hash());
  }

  public void testMultipleUpdatesDoFinal() throws Exception {
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(SHA1_KEY);
    mac.update("hello".getBytes(UTF_8));
    mac.update("world".getBytes(UTF_8));

    assertEquals(
        HashCode.fromBytes(mac.doFinal("!!!".getBytes(UTF_8))),
        Hashing.hmacSha1(SHA1_KEY)
            .newHasher()
            .putString("hello", UTF_8)
            .putString("world", UTF_8)
            .putString("!!!", UTF_8)
            .hash());
  }

  public void testCustomKey() throws Exception {
    SecretKey customKey =
        new SecretKey() {
          @Override
          public String getAlgorithm() {
            return "HmacMD5";
          }

          @Override
          public byte[] getEncoded() {
            return new byte[8];
          }

          @Override
          public String getFormat() {
            return "RAW";
          }
        };
    assertEquals(
        "ad262969c53bc16032f160081c4a07a0",
        Hashing.hmacMd5(customKey)
            .hashString("The quick brown fox jumps over the lazy dog", UTF_8)
            .toString());
  }

  public void testBadKey_emptyKey() throws Exception {
    SecretKey badKey =
        new SecretKey() {
          @Override
          public String getAlgorithm() {
            return "HmacMD5";
          }

          @Override
          public byte @Nullable [] getEncoded() {
            return null;
          }

          @Override
          public String getFormat() {
            return "RAW";
          }
        };
    try {
      Hashing.hmacMd5(badKey);
      fail();
    } catch (IllegalArgumentException expected) {
    } catch (NullPointerException toleratedOnAndroid) {
      // TODO(cpovirk): In an ideal world, we'd check here that we're running on Android.
    }
  }

  public void testEmptyInputs() throws Exception {
    String knownOutput = "8cbf764cbe2e4623d99a41354adfd390";

    Mac mac = Mac.getInstance("HmacMD5");
    mac.init(MD5_KEY);
    assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString());
    assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).newHasher().hash().toString());
  }

  public void testEmptyInputs_mixedAlgorithms() throws Exception {
    String knownOutput = "8cbf764cbe2e4623d99a41354adfd390";

    Mac mac = Mac.getInstance("HmacMD5");
    mac.init(SHA1_KEY);
    assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString());
    assertEquals(knownOutput, Hashing.hmacMd5(SHA1_KEY).newHasher().hash().toString());
  }

  public void testKnownInputs() throws Exception {
    String knownOutput = "9753980fe94daa8ecaa82216519393a9";
    String input = "The quick brown fox jumps over the lazy dog";

    Mac mac = Mac.getInstance("HmacMD5");
    mac.init(MD5_KEY);
    mac.update(input.getBytes(UTF_8));
    assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString());
    assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal(input.getBytes(UTF_8))).toString());
    assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).hashString(input, UTF_8).toString());
    assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).hashBytes(input.getBytes(UTF_8)).toString());
  }

  public void testKnownInputs_mixedAlgorithms() throws Exception {
    String knownOutput = "9753980fe94daa8ecaa82216519393a9";
    String input = "The quick brown fox jumps over the lazy dog";

    Mac mac = Mac.getInstance("HmacMD5");
    mac.init(SHA1_KEY);
    mac.update(input.getBytes(UTF_8));
    assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString());
    assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal(input.getBytes(UTF_8))).toString());
    assertEquals(knownOutput, Hashing.hmacMd5(SHA1_KEY).hashString(input, UTF_8).toString());
    assertEquals(
        knownOutput, Hashing.hmacMd5(SHA1_KEY).hashBytes(input.getBytes(UTF_8)).toString());
  }

  public void testPutAfterHash() {
    Hasher hasher = Hashing.hmacMd5(MD5_KEY).newHasher();

    assertEquals(
        "9753980fe94daa8ecaa82216519393a9",
        hasher.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString());
    assertThrows(IllegalStateException.class, () -> hasher.putInt(42));
  }

  public void testHashTwice() {
    Hasher hasher = Hashing.hmacMd5(MD5_KEY).newHasher();

    assertEquals(
        "9753980fe94daa8ecaa82216519393a9",
        hasher.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString());
    assertThrows(IllegalStateException.class, () -> hasher.hash());
  }

  public void testToString() {
    byte[] keyData = "secret key".getBytes(UTF_8);

    assertEquals(
        "Hashing.hmacMd5(Key[algorithm=HmacMD5, format=RAW])", Hashing.hmacMd5(MD5_KEY).toString());
    assertEquals(
        "Hashing.hmacMd5(Key[algorithm=HmacMD5, format=RAW])", Hashing.hmacMd5(keyData).toString());

    assertEquals(
        "Hashing.hmacSha1(Key[algorithm=HmacSHA1, format=RAW])",
        Hashing.hmacSha1(SHA1_KEY).toString());
    assertEquals(
        "Hashing.hmacSha1(Key[algorithm=HmacSHA1, format=RAW])",
        Hashing.hmacSha1(keyData).toString());

    assertEquals(
        "Hashing.hmacSha256(Key[algorithm=HmacSHA256, format=RAW])",
        Hashing.hmacSha256(SHA256_KEY).toString());
    assertEquals(
        "Hashing.hmacSha256(Key[algorithm=HmacSHA256, format=RAW])",
        Hashing.hmacSha256(keyData).toString());

    assertEquals(
        "Hashing.hmacSha512(Key[algorithm=HmacSHA512, format=RAW])",
        Hashing.hmacSha512(SHA512_KEY).toString());
    assertEquals(
        "Hashing.hmacSha512(Key[algorithm=HmacSHA512, format=RAW])",
        Hashing.hmacSha512(keyData).toString());
  }

  private static void assertMacHashing(
      byte[] input, String algorithm, SecretKey key, HashFunction hashFunc) throws Exception {
    Mac mac = Mac.getInstance(algorithm);
    mac.init(key);
    mac.update(input);

    assertEquals(HashCode.fromBytes(mac.doFinal()), hashFunc.hashBytes(input));
    assertEquals(HashCode.fromBytes(mac.doFinal(input)), hashFunc.hashBytes(input));
  }

  // Tests from RFC2022: https://tools.ietf.org/html/rfc2202

  public void testRfc2202_hmacSha1_case1() {
    byte[] key = fillByteArray(20, 0x0b);
    String data = "Hi There";

    checkSha1("b617318655057264e28bc0b6fb378c8ef146be00", key, data);
  }

  public void testRfc2202_hmacSha1_case2() {
    byte[] key = "Jefe".getBytes(UTF_8);
    String data = "what do ya want for nothing?";

    checkSha1("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79", key, data);
  }

  public void testRfc2202_hmacSha1_case3() {
    byte[] key = fillByteArray(20, 0xaa);
    byte[] data = fillByteArray(50, 0xdd);

    checkSha1("125d7342b9ac11cd91a39af48aa17b4f63f175d3", key, data);
  }

  public void testRfc2202_hmacSha1_case4() {
    byte[] key = base16().lowerCase().decode("0102030405060708090a0b0c0d0e0f10111213141516171819");
    byte[] data = fillByteArray(50, 0xcd);

    checkSha1("4c9007f4026250c6bc8414f9bf50c86c2d7235da", key, data);
  }

  public void testRfc2202_hmacSha1_case5() {
    byte[] key = fillByteArray(20, 0x0c);
    String data = "Test With Truncation";

    checkSha1("4c1a03424b55e07fe7f27be1d58bb9324a9a5a04", key, data);
  }

  public void testRfc2202_hmacSha1_case6() {
    byte[] key = fillByteArray(80, 0xaa);
    String data = "Test Using Larger Than Block-Size Key - Hash Key First";

    checkSha1("aa4ae5e15272d00e95705637ce8a3b55ed402112", key, data);
  }

  public void testRfc2202_hmacSha1_case7() {
    byte[] key = fillByteArray(80, 0xaa);
    String data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";

    checkSha1("e8e99d0f45237d786d6bbaa7965c7808bbff1a91", key, data);
  }

  public void testRfc2202_hmacMd5_case1() {
    byte[] key = fillByteArray(16, 0x0b);
    String data = "Hi There";

    checkMd5("9294727a3638bb1c13f48ef8158bfc9d", key, data);
  }

  public void testRfc2202_hmacMd5_case2() {
    byte[] key = "Jefe".getBytes(UTF_8);
    String data = "what do ya want for nothing?";

    checkMd5("750c783e6ab0b503eaa86e310a5db738", key, data);
  }

  public void testRfc2202_hmacMd5_case3() {
    byte[] key = fillByteArray(16, 0xaa);
    byte[] data = fillByteArray(50, 0xdd);

    checkMd5("56be34521d144c88dbb8c733f0e8b3f6", key, data);
  }

  public void testRfc2202_hmacMd5_case4() {
    byte[] key = base16().lowerCase().decode("0102030405060708090a0b0c0d0e0f10111213141516171819");
    byte[] data = fillByteArray(50, 0xcd);

    checkMd5("697eaf0aca3a3aea3a75164746ffaa79", key, data);
  }

  public void testRfc2202_hmacMd5_case5() {
    byte[] key = fillByteArray(16, 0x0c);
    String data = "Test With Truncation";

    checkMd5("56461ef2342edc00f9bab995690efd4c", key, data);
  }

  public void testRfc2202_hmacMd5_case6() {
    byte[] key = fillByteArray(80, 0xaa);
    String data = "Test Using Larger Than Block-Size Key - Hash Key First";

    checkMd5("6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd", key, data);
  }

  public void testRfc2202_hmacMd5_case7() {
    byte[] key = fillByteArray(80, 0xaa);
    String data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";

    checkMd5("6f630fad67cda0ee1fb1f562db3aa53e", key, data);
  }

  private static void checkSha1(String expected, byte[] key, String data) {
    checkSha1(expected, key, data.getBytes(UTF_8));
  }

  private static void checkSha1(String expected, byte[] key, byte[] data) {
    checkHmac(expected, Hashing.hmacSha1(key), data);
  }

  private static void checkMd5(String expected, byte[] key, String data) {
    checkMd5(expected, key, data.getBytes(UTF_8));
  }

  private static void checkMd5(String expected, byte[] key, byte[] data) {
    checkHmac(expected, Hashing.hmacMd5(key), data);
  }

  private static void checkHmac(String expected, HashFunction hashFunc, byte[] data) {
    assertEquals(HashCode.fromString(expected), hashFunc.hashBytes(data));
  }

  private static byte[] fillByteArray(int size, int toFillWith) {
    byte[] array = new byte[size];
    Arrays.fill(array, (byte) toFillWith);
    return array;
  }
}