aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java')
-rw-r--r--guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java58
1 files changed, 50 insertions, 8 deletions
diff --git a/guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java b/guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java
index 7433332..2a625d6 100644
--- a/guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java
+++ b/guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java
@@ -16,6 +16,8 @@
package com.google.common.hash;
+import com.google.common.base.Charsets;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.jdk5backport.Arrays;
@@ -23,7 +25,6 @@ import junit.framework.TestCase;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
-
/**
* Tests for the MessageDigestHashFunction.
*
@@ -31,17 +32,59 @@ import java.security.NoSuchAlgorithmException;
*/
public class MessageDigestHashFunctionTest extends TestCase {
private static final ImmutableSet<String> INPUTS = ImmutableSet.of("", "Z", "foobar");
- private static final ImmutableSet<String> ALGORITHMS = ImmutableSet.of(
- "MD5", "SHA1", "SHA-1", "SHA-256", "SHA-512");
+
+ // From "How Provider Implementations Are Requested and Supplied" from
+ // http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html
+ // - Some providers may choose to also include alias names.
+ // - For example, the "SHA-1" algorithm might be referred to as "SHA1".
+ // - The algorithm name is not case-sensitive.
+ private static final ImmutableMap<String, HashFunction> ALGORITHMS =
+ new ImmutableMap.Builder<String, HashFunction>()
+ .put("MD5", Hashing.md5())
+ .put("SHA", Hashing.sha1()) // Not the official name, but still works
+ .put("SHA1", Hashing.sha1()) // Not the official name, but still works
+ .put("sHa-1", Hashing.sha1()) // Not the official name, but still works
+ .put("SHA-1", Hashing.sha1())
+ .put("SHA-256", Hashing.sha256())
+ .put("SHA-512", Hashing.sha512())
+ .build();
public void testHashing() {
for (String stringToTest : INPUTS) {
- for (String algorithmToTest : ALGORITHMS) {
+ for (String algorithmToTest : ALGORITHMS.keySet()) {
assertMessageDigestHashing(HashTestUtils.ascii(stringToTest), algorithmToTest);
}
}
}
+ public void testPutAfterHash() {
+ Hasher sha1 = Hashing.sha1().newHasher();
+
+ assertEquals("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
+ sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8)
+ .hash()
+ .toString());
+ try {
+ sha1.putInt(42);
+ fail();
+ } catch (IllegalStateException expected) {
+ }
+ }
+
+ public void testHashTwice() {
+ Hasher sha1 = Hashing.sha1().newHasher();
+
+ assertEquals("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
+ sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8)
+ .hash()
+ .toString());
+ try {
+ sha1.hash();
+ fail();
+ } catch (IllegalStateException expected) {
+ }
+ }
+
public void testToString() {
assertEquals("Hashing.md5()", Hashing.md5().toString());
assertEquals("Hashing.sha1()", Hashing.sha1().toString());
@@ -53,11 +96,11 @@ public class MessageDigestHashFunctionTest extends TestCase {
try {
MessageDigest digest = MessageDigest.getInstance(algorithmName);
assertEquals(
- HashCodes.fromBytes(digest.digest(input)),
- new MessageDigestHashFunction(algorithmName, algorithmName).hashBytes(input));
+ HashCode.fromBytes(digest.digest(input)),
+ ALGORITHMS.get(algorithmName).hashBytes(input));
for (int bytes = 4; bytes <= digest.getDigestLength(); bytes++) {
assertEquals(
- HashCodes.fromBytes(Arrays.copyOf(digest.digest(input), bytes)),
+ HashCode.fromBytes(Arrays.copyOf(digest.digest(input), bytes)),
new MessageDigestHashFunction(algorithmName, bytes, algorithmName).hashBytes(input));
}
try {
@@ -70,5 +113,4 @@ public class MessageDigestHashFunctionTest extends TestCase {
throw new AssertionError(nsae);
}
}
-
}