Author: lhazlewood
Date: Fri Apr  8 18:54:16 2011
New Revision: 1090400

URL: http://svn.apache.org/viewvc?rev=1090400&view=rev
Log:
SHIRO-282: added ByteSource.Util inner class and changed direct 
SimpleByteSource usages to use ByteSource.Util instead.

Modified:
    
shiro/trunk/core/src/main/java/org/apache/shiro/authc/credential/DefaultPasswordService.java
    shiro/trunk/core/src/main/java/org/apache/shiro/crypto/JcaCipherService.java
    
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/SecureRandomNumberGenerator.java
    
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/DefaultHasher.java
    shiro/trunk/core/src/main/java/org/apache/shiro/util/ByteSource.java
    
shiro/trunk/samples/spring/src/main/java/org/apache/shiro/samples/spring/realm/SaltAwareJdbcRealm.java
    
shiro/trunk/tools/hasher/src/main/java/org/apache/shiro/tools/hasher/Hasher.java

Modified: 
shiro/trunk/core/src/main/java/org/apache/shiro/authc/credential/DefaultPasswordService.java
URL: 
http://svn.apache.org/viewvc/shiro/trunk/core/src/main/java/org/apache/shiro/authc/credential/DefaultPasswordService.java?rev=1090400&r1=1090399&r2=1090400&view=diff
==============================================================================
--- 
shiro/trunk/core/src/main/java/org/apache/shiro/authc/credential/DefaultPasswordService.java
 (original)
+++ 
shiro/trunk/core/src/main/java/org/apache/shiro/authc/credential/DefaultPasswordService.java
 Fri Apr  8 18:54:16 2011
@@ -26,7 +26,6 @@ import org.apache.shiro.codec.CodecSuppo
 import org.apache.shiro.codec.Hex;
 import org.apache.shiro.crypto.hash.*;
 import org.apache.shiro.util.ByteSource;
-import org.apache.shiro.util.SimpleByteSource;
 
 import java.util.Arrays;
 
@@ -98,7 +97,7 @@ public class DefaultPasswordService impl
     protected Hash hashProvidedCredentials(AuthenticationToken token, 
ByteSource salt) {
         Object credentials = token.getCredentials();
         byte[] credentialsBytes = new BytesHelper().getBytes(credentials);
-        ByteSource credentialsByteSource = new 
SimpleByteSource(credentialsBytes);
+        ByteSource credentialsByteSource = 
ByteSource.Util.bytes(credentialsBytes);
 
         HashRequest request = new SimpleHashRequest(credentialsByteSource, 
salt);
 

Modified: 
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/JcaCipherService.java
URL: 
http://svn.apache.org/viewvc/shiro/trunk/core/src/main/java/org/apache/shiro/crypto/JcaCipherService.java?rev=1090400&r1=1090399&r2=1090400&view=diff
==============================================================================
--- 
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/JcaCipherService.java 
(original)
+++ 
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/JcaCipherService.java 
Fri Apr  8 18:54:16 2011
@@ -19,7 +19,6 @@
 package org.apache.shiro.crypto;
 
 import org.apache.shiro.util.ByteSource;
-import org.apache.shiro.util.SimpleByteSource;
 import org.apache.shiro.util.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -342,8 +341,7 @@ public abstract class JcaCipherService i
                     "byte array is size " + (output != null ? output.length : 
0));
         }
 
-        return new SimpleByteSource(output);
-
+        return ByteSource.Util.bytes(output);
     }
 
     public ByteSource decrypt(byte[] ciphertext, byte[] key) throws 
CryptoException {
@@ -390,7 +388,7 @@ public abstract class JcaCipherService i
                     (ciphertext != null ? ciphertext.length : 0));
         }
         byte[] decrypted = crypt(ciphertext, key, iv, 
javax.crypto.Cipher.DECRYPT_MODE);
-        return decrypted == null ? null : new SimpleByteSource(decrypted);
+        return decrypted == null ? null : ByteSource.Util.bytes(decrypted);
     }
 
     /**

Modified: 
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/SecureRandomNumberGenerator.java
URL: 
http://svn.apache.org/viewvc/shiro/trunk/core/src/main/java/org/apache/shiro/crypto/SecureRandomNumberGenerator.java?rev=1090400&r1=1090399&r2=1090400&view=diff
==============================================================================
--- 
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/SecureRandomNumberGenerator.java
 (original)
+++ 
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/SecureRandomNumberGenerator.java
 Fri Apr  8 18:54:16 2011
@@ -19,7 +19,6 @@
 package org.apache.shiro.crypto;
 
 import org.apache.shiro.util.ByteSource;
-import org.apache.shiro.util.SimpleByteSource;
 
 import java.security.SecureRandom;
 
@@ -115,6 +114,6 @@ public class SecureRandomNumberGenerator
         }
         byte[] bytes = new byte[numBytes];
         this.secureRandom.nextBytes(bytes);
-        return new SimpleByteSource(bytes);
+        return ByteSource.Util.bytes(bytes);
     }
 }

Modified: 
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/DefaultHasher.java
URL: 
http://svn.apache.org/viewvc/shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/DefaultHasher.java?rev=1090400&r1=1090399&r2=1090400&view=diff
==============================================================================
--- 
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/DefaultHasher.java 
(original)
+++ 
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/DefaultHasher.java 
Fri Apr  8 18:54:16 2011
@@ -21,7 +21,6 @@ package org.apache.shiro.crypto.hash;
 import org.apache.shiro.crypto.RandomNumberGenerator;
 import org.apache.shiro.crypto.SecureRandomNumberGenerator;
 import org.apache.shiro.util.ByteSource;
-import org.apache.shiro.util.SimpleByteSource;
 
 /**
  * Default implementation of the {@link Hasher} interface, supporting 
secure-random salt generation, an internal
@@ -163,7 +162,7 @@ public class DefaultHasher implements Co
         int iterations = Math.max(1, getHashIterations());
 
         Hash result = new SimpleHash(algorithmName, sourceBytes, saltBytes, 
iterations);
-        ByteSource publicSalt = new SimpleByteSource(publicSaltBytes);
+        ByteSource publicSalt = ByteSource.Util.bytes(publicSaltBytes);
 
         return new SimpleHashResponse(result, publicSalt);
     }

Modified: shiro/trunk/core/src/main/java/org/apache/shiro/util/ByteSource.java
URL: 
http://svn.apache.org/viewvc/shiro/trunk/core/src/main/java/org/apache/shiro/util/ByteSource.java?rev=1090400&r1=1090399&r2=1090400&view=diff
==============================================================================
--- shiro/trunk/core/src/main/java/org/apache/shiro/util/ByteSource.java 
(original)
+++ shiro/trunk/core/src/main/java/org/apache/shiro/util/ByteSource.java Fri 
Apr  8 18:54:16 2011
@@ -18,12 +18,14 @@
  */
 package org.apache.shiro.util;
 
+import java.io.File;
+import java.io.InputStream;
+
 /**
- * A {@code ByteSource} wraps a byte array and provides additional encoding 
operations.  Most users will find
- * that the {@link SimpleByteSource SimpleByteSource} implementation meets 
most needs.
+ * A {@code ByteSource} wraps a byte array and provides additional encoding 
operations.  Most users will find the
+ * {@link Util} inner class sufficient to construct ByteSource instances.
  *
  * @since 1.0
- * @see SimpleByteSource
  */
 public interface ByteSource {
 
@@ -51,4 +53,75 @@ public interface ByteSource {
      *         underlying wrapped byte array.
      */
     public String toBase64();
+
+    /**
+     * Utility class that can construct ByteSource instances.  This is 
slightly nicer than needing to know the
+     * {@code ByteSource} implementation class to use.
+     *
+     * @since 1.2
+     */
+    public static final class Util {
+
+        /**
+         * Returns a new {@code ByteSource} instance representing the 
specified byte array.
+         *
+         * @param bytes the bytes to represent as a {@code ByteSource} 
instance.
+         * @return a new {@code ByteSource} instance representing the 
specified byte array.
+         */
+        public static ByteSource bytes(byte[] bytes) {
+            return new SimpleByteSource(bytes);
+        }
+
+        /**
+         * Returns a new {@code ByteSource} instance representing the 
specified character array's bytes.  The byte
+         * array is obtained assuming {@code UTF-8} encoding.
+         *
+         * @param chars the character array to represent as a {@code 
ByteSource} instance.
+         * @return a new {@code ByteSource} instance representing the 
specified character array's bytes.
+         */
+        public static ByteSource bytes(char[] chars) {
+            return new SimpleByteSource(chars);
+        }
+
+        /**
+         * Returns a new {@code ByteSource} instance representing the 
specified string's bytes.  The byte
+         * array is obtained assuming {@code UTF-8} encoding.
+         *
+         * @param string the string to represent as a {@code ByteSource} 
instance.
+         * @return a new {@code ByteSource} instance representing the 
specified string's bytes.
+         */
+        public static ByteSource bytes(String string) {
+            return new SimpleByteSource(string);
+        }
+
+        /**
+         * Returns a new {@code ByteSource} instance representing the 
specified ByteSource.
+         *
+         * @param source the ByteSource to represent as a new {@code 
ByteSource} instance.
+         * @return a new {@code ByteSource} instance representing the 
specified ByteSource.
+         */
+        public static ByteSource bytes(ByteSource source) {
+            return new SimpleByteSource(source);
+        }
+
+        /**
+         * Returns a new {@code ByteSource} instance representing the 
specified File's bytes.
+         *
+         * @param file the file to represent as a {@code ByteSource} instance.
+         * @return a new {@code ByteSource} instance representing the 
specified File's bytes.
+         */
+        public static ByteSource bytes(File file) {
+            return new SimpleByteSource(file);
+        }
+
+        /**
+         * Returns a new {@code ByteSource} instance representing the 
specified InputStream's bytes.
+         *
+         * @param stream the InputStream to represent as a {@code ByteSource} 
instance.
+         * @return a new {@code ByteSource} instance representing the 
specified InputStream's bytes.
+         */
+        public static ByteSource bytes(InputStream stream) {
+            return new SimpleByteSource(stream);
+        }
+    }
 }

Modified: 
shiro/trunk/samples/spring/src/main/java/org/apache/shiro/samples/spring/realm/SaltAwareJdbcRealm.java
URL: 
http://svn.apache.org/viewvc/shiro/trunk/samples/spring/src/main/java/org/apache/shiro/samples/spring/realm/SaltAwareJdbcRealm.java?rev=1090400&r1=1090399&r2=1090400&view=diff
==============================================================================
--- 
shiro/trunk/samples/spring/src/main/java/org/apache/shiro/samples/spring/realm/SaltAwareJdbcRealm.java
 (original)
+++ 
shiro/trunk/samples/spring/src/main/java/org/apache/shiro/samples/spring/realm/SaltAwareJdbcRealm.java
 Fri Apr  8 18:54:16 2011
@@ -2,8 +2,8 @@ package org.apache.shiro.samples.spring.
 
 import org.apache.shiro.authc.*;
 import org.apache.shiro.realm.jdbc.JdbcRealm;
+import org.apache.shiro.util.ByteSource;
 import org.apache.shiro.util.JdbcUtils;
-import org.apache.shiro.util.SimpleByteSource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -48,7 +48,7 @@ public class SaltAwareJdbcRealm extends 
              * Salts should not be based on anything that a user could enter 
(attackers can exploit this).  Instead
              * they should ideally be cryptographically-strong randomly 
generated numbers.
              */
-            saInfo.setCredentialsSalt(new SimpleByteSource(username));
+            saInfo.setCredentialsSalt(ByteSource.Util.bytes(username));
 
             info = saInfo;
 

Modified: 
shiro/trunk/tools/hasher/src/main/java/org/apache/shiro/tools/hasher/Hasher.java
URL: 
http://svn.apache.org/viewvc/shiro/trunk/tools/hasher/src/main/java/org/apache/shiro/tools/hasher/Hasher.java?rev=1090400&r1=1090399&r2=1090400&view=diff
==============================================================================
--- 
shiro/trunk/tools/hasher/src/main/java/org/apache/shiro/tools/hasher/Hasher.java
 (original)
+++ 
shiro/trunk/tools/hasher/src/main/java/org/apache/shiro/tools/hasher/Hasher.java
 Fri Apr  8 18:54:16 2011
@@ -27,7 +27,6 @@ import org.apache.shiro.crypto.hash.Simp
 import org.apache.shiro.io.ResourceUtils;
 import org.apache.shiro.util.ByteSource;
 import org.apache.shiro.util.JavaEnvironment;
-import org.apache.shiro.util.SimpleByteSource;
 import org.apache.shiro.util.StringUtils;
 
 import java.io.File;
@@ -313,7 +312,7 @@ public final class Hasher {
             if (generateSalt || (saltBytesString != null)) {
                 throw new IllegalArgumentException(SALT_MUTEX_MSG);
             }
-            return new SimpleByteSource(saltString);
+            return ByteSource.Util.bytes(saltString);
         }
 
         if (saltBytesString != null) {
@@ -334,7 +333,7 @@ public final class Hasher {
             } else {
                 bytes = Hex.decode(value);
             }
-            return new SimpleByteSource(bytes);
+            return ByteSource.Util.bytes(bytes);
         }
 
         if (generateSalt) {


Reply via email to