Author: lindner
Date: Sat Jun 19 00:41:04 2010
New Revision: 956165

URL: http://svn.apache.org/viewvc?rev=956165&view=rev
Log:
faster, better and tested HashUtil code

Added:
    
shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/HashUtilTest.java
Modified:
    
shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/HashUtil.java

Modified: 
shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/HashUtil.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/HashUtil.java?rev=956165&r1=956164&r2=956165&view=diff
==============================================================================
--- 
shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/HashUtil.java
 (original)
+++ 
shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/HashUtil.java
 Sat Jun 19 00:41:04 2010
@@ -19,6 +19,8 @@
 
 package org.apache.shindig.common.util;
 
+import com.google.common.base.Preconditions;
+
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 
@@ -26,32 +28,39 @@ import java.security.NoSuchAlgorithmExce
  * Routines for producing hashes.
  */
 public final class HashUtil {
+  private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
+
   private HashUtil() {}
   /**
-   * Produces a checksum for the given input data.
+   * Produces a checksum for the given input data. Currently uses a hexified
+   * message digest.
    *
    * @param data
    * @return The checksum.
    */
   public static String checksum(byte[] data) {
-    byte[] hash = getMessageDigest().digest(data);
+    byte[] hashBytes = 
getMessageDigest().digest(Preconditions.checkNotNull(data));
+    char[] hex = new char[2 * hashBytes.length];
+
     // Convert to hex. possibly change to base64 in the future for smaller
     // signatures.
-    StringBuilder hexString = new StringBuilder(hash.length * 2 + 2);
-    for (byte b : hash) {
-      hexString.append(Integer.toHexString(0xFF & b));
+
+    int offset = 0;
+    for (byte b : hashBytes) {
+      hex[offset++] = HEX_CHARS[(b & 0xF0) >>> 4]; // upper 4 bits
+      hex[offset++] = HEX_CHARS[(b & 0x0F)];       // lower 4 bits
     }
-    return hexString.toString();
+    return new String(hex);
   }
 
   /**
-   * Produces a raw checksum for the given input data.
+   * Produces a raw checksum for the given input data.  Currently uses a 
message digest
    *
    * @param data
    * @return The checksum.
    */
   public static String rawChecksum(byte[] data) {
-    return new String(getMessageDigest().digest(data));
+    return new 
String(getMessageDigest().digest(Preconditions.checkNotNull(data)));
   }
 
   private static MessageDigest getMessageDigest() {

Added: 
shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/HashUtilTest.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/HashUtilTest.java?rev=956165&view=auto
==============================================================================
--- 
shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/HashUtilTest.java
 (added)
+++ 
shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/HashUtilTest.java
 Sat Jun 19 00:41:04 2010
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.shindig.common.util;
+
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+public class HashUtilTest {
+  @Test
+  public void testChecksum() throws Exception {
+    byte[] data = new byte[] {Byte.MAX_VALUE, Byte.MIN_VALUE};
+    assertEquals("d41d8cd98f00b204e9800998ecf8427e", HashUtil.checksum(new 
byte[]{}));
+    assertEquals("a494a8690b72391b13d3cbe27edb5c58", HashUtil.checksum(new 
byte[]{Byte.MAX_VALUE, Byte.MIN_VALUE}));
+  }
+
+  @Test(expected = NullPointerException.class)
+  public void testChecksumNPE() {
+    HashUtil.checksum(null);
+  }
+
+  @Test(expected = NullPointerException.class)
+  public void testRawChecksumNPE() {
+    HashUtil.rawChecksum(null);
+  }
+
+  @Test
+  public void testRawChecksum() throws Exception {
+    assertEquals("Ô\u001DŒÙ\u0000²\u0004é€\t˜ìøB~", HashUtil.rawChecksum(new 
byte[]{}));
+    assertEquals("¤”¨i\u000Br9\u001B\u0013ÓËâ~Û\\X", HashUtil.rawChecksum(new 
byte[]{Byte.MAX_VALUE, Byte.MIN_VALUE}));
+  }
+}


Reply via email to