aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java
blob: a70466e73a444ebb7463044889281211fb8969c8 (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
// Copyright 2011 Google Inc. All Rights Reserved.

package com.google.common.hash;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import junit.framework.TestCase;

/**
 * Tests for the MessageDigestHashFunction.
 *
 * @author kak@google.com (Kurt Alfred Kluever)
 */
public class MessageDigestHashFunctionTest extends TestCase {
  public void testMd5Hashing() throws Exception {
    assertMessageDigestHashing(HashTestUtils.ascii(""), "MD5");
    assertMessageDigestHashing(HashTestUtils.ascii("Z"), "MD5");
    assertMessageDigestHashing(HashTestUtils.ascii("foobar"), "MD5");
  }

  public void testSha1Hashing() throws Exception {
    assertMessageDigestHashing(HashTestUtils.ascii(""), "SHA1");
    assertMessageDigestHashing(HashTestUtils.ascii("Z"), "SHA1");
    assertMessageDigestHashing(HashTestUtils.ascii("foobar"), "SHA1");
  }

  private static void assertMessageDigestHashing(byte[] input, String algorithmName) 
      throws NoSuchAlgorithmException {
    HashTestUtils.assertEqualHashes(
        MessageDigest.getInstance(algorithmName).digest(input),
        new MessageDigestHashFunction(algorithmName).hashBytes(input).asBytes());
  }
}