jspwiki-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From juanpa...@apache.org
Subject svn commit: r1600731 - in /jspwiki/trunk/jspwiki-war/src: main/java/org/apache/catalina/ main/java/org/apache/wiki/auth/user/ main/java/org/apache/wiki/util/ test/java/org/apache/wiki/util/
Date Thu, 05 Jun 2014 19:21:30 GMT
Author: juanpablo
Date: Thu Jun  5 19:21:29 2014
New Revision: 1600731

URL: http://svn.apache.org/r1600731
Log:
Applied Ichiro's patch, which fixes JSPWIKI-844 - Replace org.apache.catalina dependency -
thanks!

Added:
    jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/util/ByteUtils.java
    jspwiki/trunk/jspwiki-war/src/test/java/org/apache/wiki/util/ByteUtilsTest.java
Removed:
    jspwiki/trunk/jspwiki-war/src/main/java/org/apache/catalina/
Modified:
    jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/user/AbstractUserDatabase.java
    jspwiki/trunk/jspwiki-war/src/test/java/org/apache/wiki/util/AllTests.java

Modified: jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/user/AbstractUserDatabase.java
URL: http://svn.apache.org/viewvc/jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/user/AbstractUserDatabase.java?rev=1600731&r1=1600730&r2=1600731&view=diff
==============================================================================
--- jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/user/AbstractUserDatabase.java
(original)
+++ jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/user/AbstractUserDatabase.java
Thu Jun  5 19:21:29 2014
@@ -18,19 +18,21 @@
  */
 package org.apache.wiki.auth.user;
 
-import java.io.*;
+import java.io.UnsupportedEncodingException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.security.Principal;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Properties;
+import java.util.UUID;
 
-import org.apache.catalina.util.HexUtils;
 import org.apache.log4j.Logger;
 import org.apache.wiki.WikiEngine;
 import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
 import org.apache.wiki.auth.NoSuchPrincipalException;
 import org.apache.wiki.auth.WikiPrincipal;
 import org.apache.wiki.auth.WikiSecurityException;
+import org.apache.wiki.util.ByteUtils;
 import org.apache.wiki.util.CryptoUtil;
 
 /**
@@ -54,7 +56,6 @@ public abstract class AbstractUserDataba
      * @deprecated there is no need to call this method because the save, rename and
      * delete methods contain their own commit logic
      */
-    @SuppressWarnings("deprecation")
     public synchronized void commit() throws WikiSecurityException
     { }
 
@@ -213,7 +214,6 @@ public abstract class AbstractUserDataba
      * @param password the user's password (obtained from user input, e.g., a web form)
      * @return <code>true</code> if the supplied user password matches the
      * stored password
-     * @throws NoSuchAlgorithmException 
      * @see org.apache.wiki.auth.user.UserDatabase#validatePassword(java.lang.String,
      *      java.lang.String)
      */
@@ -341,7 +341,7 @@ public abstract class AbstractUserDataba
             MessageDigest md = MessageDigest.getInstance( "SHA" );
             md.update( text.getBytes("UTF-8") );
             byte[] digestedBytes = md.digest();
-            hash = HexUtils.convert( digestedBytes );
+            hash = ByteUtils.bytes2hex( digestedBytes );
         }
         catch( NoSuchAlgorithmException e )
         {

Added: jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/util/ByteUtils.java
URL: http://svn.apache.org/viewvc/jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/util/ByteUtils.java?rev=1600731&view=auto
==============================================================================
--- jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/util/ByteUtils.java (added)
+++ jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/util/ByteUtils.java Thu Jun  5
19:21:29 2014
@@ -0,0 +1,94 @@
+/* 
+    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.wiki.util;
+
+/**
+ *  A collection of static byte utility methods.
+ *  
+ * @author Ichiro Furusato
+ */
+public class ByteUtils
+{
+    private static final char[] hexArray = "0123456789abcdef".toCharArray();
+    
+    
+    /**
+     *  byte[] array to hex conversion. Note that this provides
+     *  no delimiters; the bytes are simply concatenated.
+     */
+    public static String bytes2hex( byte[] bytes )
+    {
+        char[] ca = new char[bytes.length * 2];
+        for ( int i = 0; i < bytes.length; i++ ) {
+            int v = bytes[i] & 0xff;
+            ca[i * 2] = hexArray[v >>> 4];
+            ca[i * 2 + 1] = hexArray[v & 0x0f];
+        }
+        return new String(ca);
+    }
+    
+    
+    /**
+     *  byte to hex conversion.
+     */
+    public static String byte2hex( byte b )
+    {
+        return Integer.toHexString(b & 0xff);
+    }
+
+    
+    /**
+     *  Parses a hexadecimal string into its corresponding bytes.
+     */
+    public static byte[] parseHexBinary( String hex )
+    {
+        final int len = hex.length();
+        // e.g., "111" is not a valid hex encoding.
+        if( len%2 != 0 ) {
+            throw new IllegalArgumentException("hexBinary needs to be even-length: "+hex);
+        }
+        byte[] out = new byte[len/2];
+        for( int i = 0; i < len; i+=2 ) {
+            int h = hexToBin(hex.charAt(i));
+            int l = hexToBin(hex.charAt(i+1));
+            if ( h==-1 || l==-1 ) {
+                throw new IllegalArgumentException("contains illegal character for hexBinary:
"+hex);
+            }
+            out[i/2] = (byte)(h*16+l);
+        }
+        return out;
+    }
+    
+    
+    /**
+     *  Converts a single hex character into its integer equivalent.
+     */
+    public static int hexToBin( char c )
+    {
+        if ( '0'<=c && c<='9' ) return c-'0';
+        if ( 'A'<=c && c<='F' ) return c-'A'+10;
+        if ( 'a'<=c && c<='f' ) return c-'a'+10;
+        return -1;
+    }
+
+    
+    private ByteUtils() {}
+
+}

Modified: jspwiki/trunk/jspwiki-war/src/test/java/org/apache/wiki/util/AllTests.java
URL: http://svn.apache.org/viewvc/jspwiki/trunk/jspwiki-war/src/test/java/org/apache/wiki/util/AllTests.java?rev=1600731&r1=1600730&r2=1600731&view=diff
==============================================================================
--- jspwiki/trunk/jspwiki-war/src/test/java/org/apache/wiki/util/AllTests.java (original)
+++ jspwiki/trunk/jspwiki-war/src/test/java/org/apache/wiki/util/AllTests.java Thu Jun  5
19:21:29 2014
@@ -18,12 +18,12 @@
  */
 package org.apache.wiki.util;
 
-import org.apache.wiki.PropertyReaderTest;
-
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
+import org.apache.wiki.PropertyReaderTest;
+
 
 public class AllTests extends TestCase {
 
@@ -34,6 +34,7 @@ public class AllTests extends TestCase {
     public static Test suite() {
         TestSuite suite = new TestSuite("Utility suite tests");
 
+        suite.addTest( ByteUtilsTest.suite() );
         suite.addTest( ClassUtilTest.suite() );
         suite.addTest( CommentedPropertiesTest.suite() );
         suite.addTest( CryptoUtilTest.suite() );

Added: jspwiki/trunk/jspwiki-war/src/test/java/org/apache/wiki/util/ByteUtilsTest.java
URL: http://svn.apache.org/viewvc/jspwiki/trunk/jspwiki-war/src/test/java/org/apache/wiki/util/ByteUtilsTest.java?rev=1600731&view=auto
==============================================================================
--- jspwiki/trunk/jspwiki-war/src/test/java/org/apache/wiki/util/ByteUtilsTest.java (added)
+++ jspwiki/trunk/jspwiki-war/src/test/java/org/apache/wiki/util/ByteUtilsTest.java Thu Jun
 5 19:21:29 2014
@@ -0,0 +1,60 @@
+/* 
+    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.wiki.util;
+
+import java.util.Arrays;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+public class ByteUtilsTest extends TestCase
+{
+    final byte[] bytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
63, 127 };
+    final String EXPECTED_HEX_STRING = "000102030405060708090a0b0c0d0e0f3f7f";
+
+    public void testByteUtilsConvertBytes()
+    {
+        String hex = ByteUtils.bytes2hex(bytes);
+        assertEquals(EXPECTED_HEX_STRING, hex);
+    }
+    
+    public void testConvertHexToBytes()
+    {
+        byte[] reconstructedBytes = ByteUtils.parseHexBinary(EXPECTED_HEX_STRING);
+        assertEquals(bytes.length,reconstructedBytes.length);
+        assertTrue(Arrays.equals(bytes,reconstructedBytes));
+    }
+    
+    public void testByteUtilsConvertByte()
+    {
+        assertEquals("0",  ByteUtils.byte2hex((byte)0));
+        assertEquals("f",  ByteUtils.byte2hex((byte)15));
+        assertEquals("10", ByteUtils.byte2hex((byte)16));
+        assertEquals("7f", ByteUtils.byte2hex((byte)127));
+    }
+
+    public static Test suite()
+    {
+        return new TestSuite( ByteUtilsTest.class );
+    }
+    
+}



Mime
View raw message