• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static java.nio.charset.StandardCharsets.UTF_8;
20 import static org.junit.Assert.assertThrows;
21 
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.ImmutableSet;
24 import java.security.MessageDigest;
25 import java.security.NoSuchAlgorithmException;
26 import java.util.Arrays;
27 import junit.framework.TestCase;
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-384", Hashing.sha384())
51           .put("SHA-512", Hashing.sha512())
52           .build();
53 
testHashing()54   public void testHashing() {
55     for (String stringToTest : INPUTS) {
56       for (String algorithmToTest : ALGORITHMS.keySet()) {
57         assertMessageDigestHashing(HashTestUtils.ascii(stringToTest), algorithmToTest);
58       }
59     }
60   }
61 
testPutAfterHash()62   public void testPutAfterHash() {
63     Hasher sha1 = Hashing.sha1().newHasher();
64 
65     assertEquals(
66         "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
67         sha1.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString());
68     assertThrows(IllegalStateException.class, () -> sha1.putInt(42));
69   }
70 
testHashTwice()71   public void testHashTwice() {
72     Hasher sha1 = Hashing.sha1().newHasher();
73 
74     assertEquals(
75         "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
76         sha1.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString());
77     assertThrows(IllegalStateException.class, () -> sha1.hash());
78   }
79 
testToString()80   public void testToString() {
81     assertEquals("Hashing.md5()", Hashing.md5().toString());
82     assertEquals("Hashing.sha1()", Hashing.sha1().toString());
83     assertEquals("Hashing.sha256()", Hashing.sha256().toString());
84     assertEquals("Hashing.sha512()", Hashing.sha512().toString());
85   }
86 
assertMessageDigestHashing(byte[] input, String algorithmName)87   private static void assertMessageDigestHashing(byte[] input, String algorithmName) {
88     try {
89       MessageDigest digest = MessageDigest.getInstance(algorithmName);
90       assertEquals(
91           HashCode.fromBytes(digest.digest(input)), ALGORITHMS.get(algorithmName).hashBytes(input));
92       for (int bytes = 4; bytes <= digest.getDigestLength(); bytes++) {
93         assertEquals(
94             HashCode.fromBytes(Arrays.copyOf(digest.digest(input), bytes)),
95             new MessageDigestHashFunction(algorithmName, bytes, algorithmName).hashBytes(input));
96       }
97       try {
98         int maxSize = digest.getDigestLength();
99         new MessageDigestHashFunction(algorithmName, maxSize + 1, algorithmName);
100         fail();
101       } catch (IllegalArgumentException expected) {
102       }
103     } catch (NoSuchAlgorithmException nsae) {
104       throw new AssertionError(nsae);
105     }
106   }
107 }
108