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 org.junit.Assert.assertThrows; 20 21 import com.google.common.base.Charsets; 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", Charsets.UTF_8) 68 .hash() 69 .toString()); 70 assertThrows(IllegalStateException.class, () -> sha1.putInt(42)); 71 } 72 testHashTwice()73 public void testHashTwice() { 74 Hasher sha1 = Hashing.sha1().newHasher(); 75 76 assertEquals( 77 "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", 78 sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8) 79 .hash() 80 .toString()); 81 assertThrows(IllegalStateException.class, () -> sha1.hash()); 82 } 83 testToString()84 public void testToString() { 85 assertEquals("Hashing.md5()", Hashing.md5().toString()); 86 assertEquals("Hashing.sha1()", Hashing.sha1().toString()); 87 assertEquals("Hashing.sha256()", Hashing.sha256().toString()); 88 assertEquals("Hashing.sha512()", Hashing.sha512().toString()); 89 } 90 assertMessageDigestHashing(byte[] input, String algorithmName)91 private static void assertMessageDigestHashing(byte[] input, String algorithmName) { 92 try { 93 MessageDigest digest = MessageDigest.getInstance(algorithmName); 94 assertEquals( 95 HashCode.fromBytes(digest.digest(input)), ALGORITHMS.get(algorithmName).hashBytes(input)); 96 for (int bytes = 4; bytes <= digest.getDigestLength(); bytes++) { 97 assertEquals( 98 HashCode.fromBytes(Arrays.copyOf(digest.digest(input), bytes)), 99 new MessageDigestHashFunction(algorithmName, bytes, algorithmName).hashBytes(input)); 100 } 101 try { 102 int maxSize = digest.getDigestLength(); 103 new MessageDigestHashFunction(algorithmName, maxSize + 1, algorithmName); 104 fail(); 105 } catch (IllegalArgumentException expected) { 106 } 107 } catch (NoSuchAlgorithmException nsae) { 108 throw new AssertionError(nsae); 109 } 110 } 111 } 112