1 // Copyright 2011 Google Inc. All Rights Reserved. 2 3 package com.google.common.hash; 4 5 import java.security.MessageDigest; 6 import java.security.NoSuchAlgorithmException; 7 8 import junit.framework.TestCase; 9 10 /** 11 * Tests for the MessageDigestHashFunction. 12 * 13 * @author kak (at) google.com (Kurt Alfred Kluever) 14 */ 15 public class MessageDigestHashFunctionTest extends TestCase { 16 public void testMd5Hashing() throws Exception { 17 assertMessageDigestHashing(HashTestUtils.ascii(""), "MD5"); 18 assertMessageDigestHashing(HashTestUtils.ascii("Z"), "MD5"); 19 assertMessageDigestHashing(HashTestUtils.ascii("foobar"), "MD5"); 20 } 21 22 public void testSha1Hashing() throws Exception { 23 assertMessageDigestHashing(HashTestUtils.ascii(""), "SHA1"); 24 assertMessageDigestHashing(HashTestUtils.ascii("Z"), "SHA1"); 25 assertMessageDigestHashing(HashTestUtils.ascii("foobar"), "SHA1"); 26 } 27 28 private static void assertMessageDigestHashing(byte[] input, String algorithmName) 29 throws NoSuchAlgorithmException { 30 HashTestUtils.assertEqualHashes( 31 MessageDigest.getInstance(algorithmName).digest(input), 32 new MessageDigestHashFunction(algorithmName).hashBytes(input).asBytes()); 33 } 34 } 35