tobrien     2003/10/04 09:28:53

  Added:       codec/src/test/org/apache/commons/codec/digest
                        DigestUtilsTest.java
               codec/src/java/org/apache/commons/codec/digest
                        DigestUtils.java
  Log:
  Added the DigestUtils class which provides static utility
  methods for creating MD5 and SHA digests.  This is
  a contribution from both Dave Dribin and David Graham.
  
  Revision  Changes    Path
  1.1                  
jakarta-commons/codec/src/test/org/apache/commons/codec/digest/DigestUtilsTest.java
  
  Index: DigestUtilsTest.java
  ===================================================================
  /* 
   * ====================================================================
   * 
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.codec.digest;
  
  import junit.framework.TestCase;
  
  /**
   * Tests Digest methods.
   *
   * @author Dave Dribin
   * @author David Graham
   */
  public class DigestUtilsTest extends TestCase {
  
        public void testMd5Hex() {
                // Examples from RFC 1321
                assertEquals("d41d8cd98f00b204e9800998ecf8427e", 
DigestUtils.md5Hex(""));
  
                assertEquals("0cc175b9c0f1b6a831c399e269772661", 
DigestUtils.md5Hex("a"));
  
                assertEquals("900150983cd24fb0d6963f7d28e17f72", 
DigestUtils.md5Hex("abc"));
  
                assertEquals(
                        "f96b697d7cb7938d525a2f31aaf161d0",
                        DigestUtils.md5Hex("message digest"));
  
                assertEquals(
                        "c3fcd3d76192e4007dfb496cca67e13b",
                        DigestUtils.md5Hex("abcdefghijklmnopqrstuvwxyz"));
  
                assertEquals(
                        "d174ab98d277d9f5a5611c2c9f419d9f",
                        DigestUtils.md5Hex(
                                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                        + "abcdefghijklmnopqrstuvwxyz"
                                        + "0123456789"));
  
                assertEquals(
                        "57edf4a22be3c955ac49da2e2107b67a",
                        DigestUtils.md5Hex(
                                "1234567890123456789012345678901234567890"
                                        + "1234567890123456789012345678901234567890"));
        }
  
        /**
         * An MD5 hash converted to hex should always be 32 characters.
         */
        public void testMD5HexLength() {
                String hashMe = "this is some string that is longer than 32 
characters";
                String hash = DigestUtils.md5Hex(hashMe.getBytes());
                assertEquals(32, hash.length());
  
                hashMe = "length < 32";
                hash = DigestUtils.md5Hex(hashMe.getBytes());
                assertEquals(32, hash.length());
        }
  
        /**
         * An MD5 hash should always be a 16 element byte[].
         */
        public void testMD5Length() {
                String hashMe = "this is some string that is longer than 16 
characters";
                byte[] hash = DigestUtils.md5(hashMe.getBytes());
                assertEquals(16, hash.length);
  
                hashMe = "length < 16";
                hash = DigestUtils.md5(hashMe.getBytes());
                assertEquals(16, hash.length);
        }
  
        public void testShaHex() {
                // Examples from FIPS 180-1
                assertEquals(
                        "a9993e364706816aba3e25717850c26c9cd0d89d",
                        DigestUtils.shaHex("abc"));
  
                assertEquals(
                        "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
                        DigestUtils.shaHex(
                                "abcdbcdecdefdefgefghfghighij"
                                        + "hijkijkljklmklmnlmnomnopnopq"));
        }
  
  }
  
  
  
  1.1                  
jakarta-commons/codec/src/java/org/apache/commons/codec/digest/DigestUtils.java
  
  Index: DigestUtils.java
  ===================================================================
  /* 
   * ====================================================================
   * 
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.codec.digest;
  
  import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;
  
  import org.apache.commons.codec.binary.Hex;
  
  /**
   * Operations to simplifiy common <code>MessageDigest</code> tasks.  This
   * class is thread safe.
   *
   * @author Dave Dribin
   * @author David Graham
   */
  public class DigestUtils {
  
        /**
         * Returns an MD5 MessageDigest.
         *
         * @return An MD5 digest instance.
         */
        private static MessageDigest getMd5Digest() {
                try {
                        return MessageDigest.getInstance("MD5");
  
                } catch (NoSuchAlgorithmException e) {
                        throw new InternalError(e.getMessage());
                }
        }
  
        /**
         * Returns an SHA digest.
         *
         * @return An SHA digest instance.
         */
        private static MessageDigest getShaDigest() {
                try {
                        return MessageDigest.getInstance("SHA");
  
                } catch (NoSuchAlgorithmException e) {
                        throw new InternalError(e.getMessage());
                }
        }
  
        /**
         * Calculates the MD5 digest and returns the value as a 16 element 
         * <code>byte[]</code>.
         *
         * @param data Data to digest
         * @return MD5 digest
         */
        public static byte[] md5(byte[] data) {
                return getMd5Digest().digest(data);
        }
  
        /**
         * Calculates the MD5 digest and returns the value as a 16 element 
         * <code>byte[]</code>.
         *
         * @param data Data to digest
         * @return MD5 digest
         */
        public static byte[] md5(String data) {
                return md5(data.getBytes());
        }
  
        /**
         * Calculates the MD5 digest and returns the value as a 32 character 
         * hex string.
         *
         * @param data Data to digest
         * @return MD5 digest as a hex string
         */
        public static String md5Hex(byte[] data) {
                return new String(Hex.encodeHex(md5(data)));
        }
  
        /**
         * Calculates the MD5 digest and returns the value as a 32 character 
         * hex string.
         *
         * @param data Data to digest
         * @return MD5 digest as a hex string
         */
        public static String md5Hex(String data) {
                return new String(Hex.encodeHex(md5(data)));
        }
  
        /**
         * Calculates the SHA digest and returns the value as a 
         * <code>byte[]</code>.
         *
         * @param data Data to digest
         * @return SHA digest
         */
        public static byte[] sha(byte[] data) {
                return getShaDigest().digest(data);
        }
  
        /**
         * Calculates the SHA digest and returns the value as a 
         * <code>byte[]</code>.
         *
         * @param data Data to digest
         * @return SHA digest
         */
        public static byte[] sha(String data) {
                return sha(data.getBytes());
        }
  
        /**
       * Calculates the SHA digest and returns the value as a hex string.
       *
       * @param data Data to digest
       * @return SHA digest as a hex string
       */
        public static String shaHex(byte[] data) {
                return new String(Hex.encodeHex(sha(data)));
        }
  
        /**
         * Calculates the SHA digest and returns the value as a hex string.
         *
         * @param data Data to digest
         * @return SHA digest as a hex string
         */
        public static String shaHex(String data) {
                return new String(Hex.encodeHex(sha(data)));
        }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to