Home | History | Annotate | Download | only in hash
      1 /*
      2  * Copyright (C) 2011 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.hash;
     18 
     19 import com.google.common.base.Charsets;
     20 import com.google.common.collect.ImmutableMap;
     21 import com.google.common.collect.ImmutableSet;
     22 
     23 import junit.framework.TestCase;
     24 
     25 import java.security.MessageDigest;
     26 import java.security.NoSuchAlgorithmException;
     27 import java.util.Arrays;
     28 
     29 /**
     30  * Tests for the MessageDigestHashFunction.
     31  *
     32  * @author Kurt Alfred Kluever
     33  */
     34 public class MessageDigestHashFunctionTest extends TestCase {
     35   private static final ImmutableSet<String> INPUTS = ImmutableSet.of("", "Z", "foobar");
     36 
     37   // From "How Provider Implementations Are Requested and Supplied" from
     38   // http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html
     39   //  - Some providers may choose to also include alias names.
     40   //  - For example, the "SHA-1" algorithm might be referred to as "SHA1".
     41   //  - The algorithm name is not case-sensitive.
     42   private static final ImmutableMap<String, HashFunction> ALGORITHMS =
     43       new ImmutableMap.Builder<String, HashFunction>()
     44           .put("MD5", Hashing.md5())
     45           .put("SHA", Hashing.sha1()) // Not the official name, but still works
     46           .put("SHA1", Hashing.sha1()) // Not the official name, but still works
     47           .put("sHa-1", Hashing.sha1()) // Not the official name, but still works
     48           .put("SHA-1", Hashing.sha1())
     49           .put("SHA-256", Hashing.sha256())
     50           .put("SHA-512", Hashing.sha512())
     51           .build();
     52 
     53   public void testHashing() {
     54     for (String stringToTest : INPUTS) {
     55       for (String algorithmToTest : ALGORITHMS.keySet()) {
     56         assertMessageDigestHashing(HashTestUtils.ascii(stringToTest), algorithmToTest);
     57       }
     58     }
     59   }
     60 
     61   public void testPutAfterHash() {
     62     Hasher sha1 = Hashing.sha1().newHasher();
     63 
     64     assertEquals("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
     65         sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8)
     66             .hash()
     67             .toString());
     68     try {
     69       sha1.putInt(42);
     70       fail();
     71     } catch (IllegalStateException expected) {
     72     }
     73   }
     74 
     75   public void testHashTwice() {
     76     Hasher sha1 = Hashing.sha1().newHasher();
     77 
     78     assertEquals("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
     79         sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8)
     80             .hash()
     81             .toString());
     82     try {
     83       sha1.hash();
     84       fail();
     85     } catch (IllegalStateException expected) {
     86     }
     87   }
     88 
     89   public void testToString() {
     90     assertEquals("Hashing.md5()", Hashing.md5().toString());
     91     assertEquals("Hashing.sha1()", Hashing.sha1().toString());
     92     assertEquals("Hashing.sha256()", Hashing.sha256().toString());
     93     assertEquals("Hashing.sha512()", Hashing.sha512().toString());
     94   }
     95 
     96   private static void assertMessageDigestHashing(byte[] input, String algorithmName) {
     97     try {
     98       MessageDigest digest = MessageDigest.getInstance(algorithmName);
     99       assertEquals(
    100           HashCode.fromBytes(digest.digest(input)),
    101           ALGORITHMS.get(algorithmName).hashBytes(input));
    102       for (int bytes = 4; bytes <= digest.getDigestLength(); bytes++) {
    103         assertEquals(
    104             HashCode.fromBytes(Arrays.copyOf(digest.digest(input), bytes)),
    105             new MessageDigestHashFunction(algorithmName, bytes, algorithmName).hashBytes(input));
    106       }
    107       try {
    108         int maxSize = digest.getDigestLength();
    109         new MessageDigestHashFunction(algorithmName, maxSize + 1, algorithmName);
    110         fail();
    111       } catch (IllegalArgumentException expected) {
    112       }
    113     } catch (NoSuchAlgorithmException nsae) {
    114       throw new AssertionError(nsae);
    115     }
    116   }
    117 }
    118