/*
 * Copyright (C) 2011 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 com.google.common.collect.ImmutableSet;

import junit.framework.TestCase;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 * Tests for the MessageDigestHashFunction.
 *
 * @author Kurt Alfred Kluever
 */
public class MessageDigestHashFunctionTest extends TestCase {
  private static final ImmutableSet<String> INPUTS = ImmutableSet.of("", "Z", "foobar");
  private static final ImmutableSet<String> ALGORITHMS = ImmutableSet.of(
        "MD5", "SHA1", "SHA-1", "SHA-256", "SHA-512");

  public void testHashing() {
    for (String stringToTest : INPUTS) {
      for (String algorithmToTest : ALGORITHMS) {
        assertMessageDigestHashing(HashTestUtils.ascii(stringToTest), algorithmToTest);
      }
    }
  }

  public void testToString() {
    assertEquals("Hashing.md5()", Hashing.md5().toString());
    assertEquals("Hashing.sha1()", Hashing.sha1().toString());
    assertEquals("Hashing.sha256()", Hashing.sha256().toString());
    assertEquals("Hashing.sha512()", Hashing.sha512().toString());
  }

  private static void assertMessageDigestHashing(byte[] input, String algorithmName) {
    try {
      MessageDigest digest = MessageDigest.getInstance(algorithmName);
      assertEquals(
          HashCodes.fromBytes(digest.digest(input)),
          new MessageDigestHashFunction(algorithmName, algorithmName).hashBytes(input));
      for (int bytes = 4; bytes <= digest.getDigestLength(); bytes++) {
        assertEquals(
            HashCodes.fromBytes(Arrays.copyOf(digest.digest(input), bytes)),
            new MessageDigestHashFunction(algorithmName, bytes, algorithmName).hashBytes(input));
      }
      try {
        int maxSize = digest.getDigestLength();
        new MessageDigestHashFunction(algorithmName, maxSize + 1, algorithmName);
        fail();
      } catch (IllegalArgumentException expected) {
      }
    } catch (NoSuchAlgorithmException nsae) {
      throw new AssertionError(nsae);
    }
  }
}

@@ -628,16 +628,63 @@
 .hash;%0A%0A
+import com.google.common.collect.ImmutableMap;%0A
 import c
@@ -722,16 +722,16 @@
 bleSet;%0A
-
 %0Aimport 
@@ -1102,16 +1102,369 @@
 oobar%22);
+%0A%0A  // From %22How Provider Implementations Are Requested and Supplied%22 from%0A  // http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html%0A  //  - Some providers may choose to also include alias names.%0A  //  - For example, the %22SHA-1%22 algorithm might be referred to as %22SHA1%22.%0A  //  - The algorithm name is not case-sensitive.
 %0A  priva
@@ -1492,102 +1492,527 @@
 able
-Set
+Map
 %3CString
-%3E ALGORITHMS = ImmutableSet.of(%0A        %22MD5%22, %22SHA1%22, %22SHA-1%22, %22SHA-256%22, %22SHA-512%22
+, HashFunction%3E ALGORITHMS =%0A      new ImmutableMap.Builder%3CString, HashFunction%3E()%0A          .put(%22MD5%22, Hashing.md5())%0A          .put(%22SHA%22, Hashing.sha1()) // Not the official name, but still works%0A          .put(%22SHA1%22, Hashing.sha1()) // Not the official name, but still works%0A          .put(%22sHa-1%22, Hashing.sha1()) // Not the official name, but still works%0A          .put(%22SHA-1%22, Hashing.sha1())%0A          .put(%22SHA-256%22, Hashing.sha256())%0A          .put(%22SHA-512%22, Hashing.sha512())%0A          .build(
 );%0A%0A
@@ -2128,16 +2128,25 @@
 GORITHMS
+.keySet()
 ) %7B%0A    
@@ -2797,53 +2797,23 @@
     
-new MessageDigestHashFunction(algorithmName, 
+ALGORITHMS.get(
 algo

Add tests for common aliases of SHA1. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=45427688