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 import java.security.MessageDigest; 23 import java.security.NoSuchAlgorithmException; 24 import java.util.Arrays; 25 import junit.framework.TestCase; 26 27 /** 28 * Tests for the MessageDigestHashFunction. 29 * 30 * @author Kurt Alfred Kluever 31 */ 32 public class MessageDigestHashFunctionTest extends TestCase { 33 private static final ImmutableSet<String> INPUTS = ImmutableSet.of("", "Z", "foobar"); 34 35 // From "How Provider Implementations Are Requested and Supplied" from 36 // http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html 37 // - Some providers may choose to also include alias names. 38 // - For example, the "SHA-1" algorithm might be referred to as "SHA1". 39 // - The algorithm name is not case-sensitive. 40 private static final ImmutableMap<String, HashFunction> ALGORITHMS = 41 new ImmutableMap.Builder<String, HashFunction>() 42 .put("MD5", Hashing.md5()) 43 .put("SHA", Hashing.sha1()) // Not the official name, but still works 44 .put("SHA1", Hashing.sha1()) // Not the official name, but still works 45 .put("sHa-1", Hashing.sha1()) // Not the official name, but still works 46 .put("SHA-1", Hashing.sha1()) 47 .put("SHA-256", Hashing.sha256()) 48 .put("SHA-384", Hashing.sha384()) 49 .put("SHA-512", Hashing.sha512()) 50 .build(); 51 testHashing()52 public void testHashing() { 53 for (String stringToTest : INPUTS) { 54 for (String algorithmToTest : ALGORITHMS.keySet()) { 55 assertMessageDigestHashing(HashTestUtils.ascii(stringToTest), algorithmToTest); 56 } 57 } 58 } 59 testPutAfterHash()60 public void testPutAfterHash() { 61 Hasher sha1 = Hashing.sha1().newHasher(); 62 63 assertEquals( 64 "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 testHashTwice()75 public void testHashTwice() { 76 Hasher sha1 = Hashing.sha1().newHasher(); 77 78 assertEquals( 79 "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", 80 sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8) 81 .hash() 82 .toString()); 83 try { 84 sha1.hash(); 85 fail(); 86 } catch (IllegalStateException expected) { 87 } 88 } 89 testToString()90 public void testToString() { 91 assertEquals("Hashing.md5()", Hashing.md5().toString()); 92 assertEquals("Hashing.sha1()", Hashing.sha1().toString()); 93 assertEquals("Hashing.sha256()", Hashing.sha256().toString()); 94 assertEquals("Hashing.sha512()", Hashing.sha512().toString()); 95 } 96 assertMessageDigestHashing(byte[] input, String algorithmName)97 private static void assertMessageDigestHashing(byte[] input, String algorithmName) { 98 try { 99 MessageDigest digest = MessageDigest.getInstance(algorithmName); 100 assertEquals( 101 HashCode.fromBytes(digest.digest(input)), 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