tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@hyperreal.org
Subject cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/util HexUtils.java LocalStrings.properties
Date Sun, 24 Oct 1999 00:08:04 GMT
craigmcc    99/10/23 17:08:04

  Modified:    src/share/org/apache/tomcat/security/file
                        FileRealmDatabase.java FileRealmTool.java
               src/share/org/apache/tomcat/util LocalStrings.properties
  Added:       src/share/org/apache/tomcat/util HexUtils.java
  Log:
  Migrate common hexadecimal digit string <--> byte array conversion
  processing into a new utility module.
  
  Revision  Changes    Path
  1.4       +9 -76     jakarta-tomcat/src/share/org/apache/tomcat/security/file/FileRealmDatabase.java
  
  Index: FileRealmDatabase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/security/file/FileRealmDatabase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FileRealmDatabase.java	1999/10/23 22:30:17	1.3
  +++ FileRealmDatabase.java	1999/10/24 00:08:02	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/security/file/FileRealmDatabase.java,v
1.3 1999/10/23 22:30:17 craigmcc Exp $
  - * $Revision: 1.3 $
  - * $Date: 1999/10/23 22:30:17 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/security/file/FileRealmDatabase.java,v
1.4 1999/10/24 00:08:02 craigmcc Exp $
  + * $Revision: 1.4 $
  + * $Date: 1999/10/24 00:08:02 $
    *
    * ====================================================================
    *
  @@ -72,6 +72,7 @@
   import java.util.Enumeration;
   import java.util.Hashtable;
   import java.util.Vector;
  +import org.apache.tomcat.util.HexUtils;
   import org.apache.tomcat.util.StringManager;
   import org.apache.tomcat.util.XMLParser;
   import org.apache.tomcat.util.XMLTree;
  @@ -84,7 +85,7 @@
    * <code>tomcat-users.dtd</code> file in this directory.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.3 $ $Date: 1999/10/23 22:30:17 $
  + * @version $Revision: 1.4 $ $Date: 1999/10/24 00:08:02 $
    */
   
   public final class FileRealmDatabase {
  @@ -174,75 +175,6 @@
       }
   
   
  -    /** [Private] Convert the specified byte array representation of an
  -     * encrypted password into the corresponding hexadecimal digit
  -     * representation.
  -     *
  -     * @param bytes Byte array representation
  -     */
  -    private String convert(byte bytes[]) {
  -
  -	StringBuffer sb = new StringBuffer(bytes.length * 2);
  -	for (int i = 0; i < bytes.length; i++) {
  -	    sb.append(convertDigit((int) (bytes[i] >> 4)));
  -	    sb.append(convertDigit((int) (bytes[i] & 0x0f)));
  -	}
  -	return (sb.toString());
  -
  -    }
  -
  -
  -    /**
  -     * [Private] Convert the specified value (0 .. 15) to the corresponding
  -     * hexadecimal digit.
  -     *
  -     * @param value Value to be converted
  -     */
  -    private char convertDigit(int value) {
  -
  -	value &= 0x0f;
  -	if (value >= 10)
  -	    return ((char) (value - 10 + 'a'));
  -	else
  -	    return ((char) (value + '0'));
  -
  -    }
  -
  -
  -    /**
  -     * [Private] Convert the hexadecimal digit representation of an encrypted
  -     * password into the corresponding byte array representation.
  -     *
  -     * @param digits Hexadecimal digits representation
  -     */
  -    private byte[] convert(String digits) {
  -
  -	ByteArrayOutputStream baos = new ByteArrayOutputStream();
  -	for (int i = 0; i < digits.length(); i += 2) {
  -	    char c1 = digits.charAt(i);
  -	    char c2 = '0';
  -	    if (i+1 < digits.length())
  -		c2 = digits.charAt(i+1);
  -	    byte b = 0;
  -	    if ((c1 >= '0') && (c1 <= '9'))
  -		b += ((c1 - '0') * 16);
  -	    else if ((c1 >= 'a') && (c1 <= 'f'))
  -		b += ((c1 - 'a' + 10) * 16);
  -	    else if ((c1 >= 'A') && (c1 <= 'F'))
  -		b += ((c1 - 'A' + 10) * 16);
  -	    if ((c2 >= '0') && (c2 <= '9'))
  -		b += (c2 - '0');
  -	    else if ((c2 >= 'a') && (c2 <= 'f'))
  -		b += (c2 - 'a' + 10);
  -	    else if ((c2 >= 'A') && (c2 <= 'F'))
  -		b += (c2 - 'A' + 10);
  -	    baos.write(b);
  -	}
  -	return (baos.toByteArray());
  -
  -    }
  -
  -
       /**
        * Create and return a new group.
        *
  @@ -486,7 +418,8 @@
   	String name =
   	    (String) element.getAttribute(Constants.Attribute.NAME);
   	byte[] password =
  -          convert((String) element.getAttribute(Constants.Attribute.PASSWORD));
  +          HexUtils.convert
  +	    ((String) element.getAttribute(Constants.Attribute.PASSWORD));
   	createUser(name, password);
   
       }
  @@ -561,8 +494,8 @@
   	while (users.hasMoreElements()) {
   	    FileRealmUser user = (FileRealmUser) users.nextElement();
   	    writer.println("  <user name=\"" + user.getName() +
  -			   "\" password=\"" + convert(user.getPassword()) +
  -			   "\" />");
  +			   "\" password=\"" +
  +			   HexUtils.convert(user.getPassword()) + "\" />");
   	}
   
   	// Render group elements for all defined groups
  
  
  
  1.2       +7 -41     jakarta-tomcat/src/share/org/apache/tomcat/security/file/FileRealmTool.java
  
  Index: FileRealmTool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/security/file/FileRealmTool.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FileRealmTool.java	1999/10/23 22:30:18	1.1
  +++ FileRealmTool.java	1999/10/24 00:08:02	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/security/file/FileRealmTool.java,v
1.1 1999/10/23 22:30:18 craigmcc Exp $
  - * $Revision: 1.1 $
  - * $Date: 1999/10/23 22:30:18 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/security/file/FileRealmTool.java,v
1.2 1999/10/24 00:08:02 craigmcc Exp $
  + * $Revision: 1.2 $
  + * $Date: 1999/10/24 00:08:02 $
    *
    * ====================================================================
    *
  @@ -73,6 +73,7 @@
   import java.io.IOException;
   import java.io.OutputStream;
   import java.util.Enumeration;
  +import org.apache.tomcat.util.HexUtils;
   import org.apache.tomcat.util.StringManager;
   import org.xml.sax.SAXException;
   import org.xml.sax.SAXParseException;
  @@ -114,7 +115,7 @@
    * </pre>
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.1 $ $Date: 1999/10/23 22:30:18 $
  + * @version $Revision: 1.2 $ $Date: 1999/10/24 00:08:02 $
    */
   
   public final class FileRealmTool {
  @@ -329,41 +330,6 @@
       }
   
   
  -    /** [Private] Convert the specified byte array representation of an
  -     * encrypted password into the corresponding hexadecimal digit
  -     * representation.
  -     *
  -     * @param bytes Byte array representation
  -     */
  -    private static String convert(byte bytes[]) {
  -
  -	StringBuffer sb = new StringBuffer(bytes.length * 2);
  -	for (int i = 0; i < bytes.length; i++) {
  -	    sb.append(convertDigit((int) (bytes[i] >> 4)));
  -	    sb.append(convertDigit((int) (bytes[i] & 0x0f)));
  -	}
  -	return (sb.toString());
  -
  -    }
  -
  -
  -    /**
  -     * [Private] Convert the specified value (0 .. 15) to the corresponding
  -     * hexadecimal digit.
  -     *
  -     * @param value Value to be converted
  -     */
  -    private static char convertDigit(int value) {
  -
  -	value &= 0x0f;
  -	if (value >= 10)
  -	    return ((char) (value - 10 + 'a'));
  -	else
  -	    return ((char) (value + '0'));
  -
  -    }
  -
  -
       /**
        * Drop an existing group, if it is already present.
        */
  @@ -507,8 +473,8 @@
   	while (users.hasMoreElements()) {
   	    FileRealmUser user = (FileRealmUser) users.nextElement();
   	    System.out.print("User '" + user.getName() +
  -			     "', password='" + convert(user.getPassword()) +
  -			     "'");
  +			     "', password='" +
  +			     HexUtils.convert(user.getPassword()) + "'");
   	    Enumeration groups = user.getGroups();
   	    while (groups.hasMoreElements()) {
   		FileRealmGroup group = (FileRealmGroup) groups.nextElement();
  
  
  
  1.2       +3 -1      jakarta-tomcat/src/share/org/apache/tomcat/util/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/LocalStrings.properties,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LocalStrings.properties	1999/10/09 00:20:55	1.1
  +++ LocalStrings.properties	1999/10/24 00:08:03	1.2
  @@ -1,4 +1,4 @@
  -# $Id: LocalStrings.properties,v 1.1 1999/10/09 00:20:55 duncan Exp $
  +# $Id: LocalStrings.properties,v 1.2 1999/10/24 00:08:03 craigmcc Exp $
   #
   # Copyright Statement
   
  @@ -6,6 +6,8 @@
   # This is the default locale and is en_US
   
   ascii.parseInt.nfe=number formatting error {0}
  +hexUtil.bad=Bad hexadecimal digit
  +hexUtil.odd=Odd number of hexadecimal digits
   httpDate.pe=invalid date format: {0}
   httpDate.iae=array too small: {0}
   messageBytes.iae=invalid date format: {0}
  
  
  
  1.1                  jakarta-tomcat/src/share/org/apache/tomcat/util/HexUtils.java
  
  Index: HexUtils.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/HexUtils.java,v 1.1
1999/10/24 00:08:03 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 1999/10/24 00:08:03 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 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 acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", 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 apache@apache.org.
   *
   * 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 Group.
   *
   * 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/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  
  package org.apache.tomcat.util;
  
  import java.io.ByteArrayOutputStream;
  
  /**
   * Library of utility methods useful in dealing with converting byte arrays
   * to and from strings of hexadecimal digits.
   *
   * @author Craig R. McClanahan
   */
  
  public final class HexUtils {
  
  
      /**
       * The string manager for this package.
       */
      private static StringManager sm =
  	StringManager.getManager(Constants.Package);
  
  
      /**
       * Convert a String of hexadecimal digits into the corresponding
       * byte array by encoding each two hexadecimal digits as a byte.
       *
       * @param digits Hexadecimal digits representation
       *
       * @exception IllegalArgumentException if an invalid hexadecimal digit
       *  is found, or the input string contains an odd number of hexadecimal
       *  digits
       */
      public static byte[] convert(String digits) {
  
  	ByteArrayOutputStream baos = new ByteArrayOutputStream();
  	for (int i = 0; i < digits.length(); i += 2) {
  	    char c1 = digits.charAt(i);
  	    if ((i+1) >= digits.length())
  		throw new IllegalArgumentException
  		    (sm.getString("hexUtil.odd"));
  	    char c2 = digits.charAt(i + 1);
  	    byte b = 0;
  	    if ((c1 >= '0') && (c1 <= '9'))
  		b += ((c1 - '0') * 16);
  	    else if ((c1 >= 'a') && (c1 <= 'f'))
  		b += ((c1 - 'a' + 10) * 16);
  	    else if ((c1 >= 'A') && (c1 <= 'F'))
  		b += ((c1 - 'A' + 10) * 16);
  	    else
  		throw new IllegalArgumentException
  		    (sm.getString("hexUtil.bad"));
  	    if ((c2 >= '0') && (c2 <= '9'))
  		b += (c2 - '0');
  	    else if ((c2 >= 'a') && (c2 <= 'f'))
  		b += (c2 - 'a' + 10);
  	    else if ((c2 >= 'A') && (c2 <= 'F'))
  		b += (c2 - 'A' + 10);
  	    else
  		throw new IllegalArgumentException
  		    (sm.getString("hexUtil.bad"));
  	    baos.write(b);
  	}
  	return (baos.toByteArray());
  
      }
  
  
      /**
       * Convert a byte array into a printable format containing a
       * String of hexadecimal digit characters (two per byte).
       *
       * @param bytes Byte array representation
       */
      public static String convert(byte bytes[]) {
  
  	StringBuffer sb = new StringBuffer(bytes.length * 2);
  	for (int i = 0; i < bytes.length; i++) {
  	    sb.append(convertDigit((int) (bytes[i] >> 4)));
  	    sb.append(convertDigit((int) (bytes[i] & 0x0f)));
  	}
  	return (sb.toString());
  
      }
  
  
      /**
       * [Private] Convert the specified value (0 .. 15) to the corresponding
       * hexadecimal digit.
       *
       * @param value Value to be converted
       */
      private static char convertDigit(int value) {
  
  	value &= 0x0f;
  	if (value >= 10)
  	    return ((char) (value - 10 + 'a'));
  	else
  	    return ((char) (value + '0'));
  
      }
  
  
  }
  
  
  

Mime
View raw message