You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by rg...@apache.org on 2009/08/15 00:30:49 UTC
svn commit: r804397 - in /commons/proper/vfs/trunk:
core/src/main/java/org/apache/commons/vfs/provider/
core/src/main/java/org/apache/commons/vfs/util/
core/src/test/java/org/apache/commons/vfs/util/ xdocs/
Author: rgoers
Date: Fri Aug 14 22:30:49 2009
New Revision: 804397
URL: http://svn.apache.org/viewvc?rev=804397&view=rev
Log:
Allow encryption/decryption to be supplied by user
Added:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/Cryptor.java
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/CryptorFactory.java
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/DefaultCryptor.java
- copied, changed from r804045, commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/EncryptDecrypt.java
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/EncryptUtil.java
- copied, changed from r804045, commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/EncryptDecrypt.java
Removed:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/EncryptDecrypt.java
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/HostFileNameParser.java
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs/util/EncryptDecryptTest.java
commons/proper/vfs/trunk/xdocs/filesystems.xml
Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/HostFileNameParser.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/HostFileNameParser.java?rev=804397&r1=804396&r2=804397&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/HostFileNameParser.java (original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/HostFileNameParser.java Fri Aug 14 22:30:49 2009
@@ -19,7 +19,8 @@
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileType;
-import org.apache.commons.vfs.util.EncryptDecrypt;
+import org.apache.commons.vfs.util.Cryptor;
+import org.apache.commons.vfs.util.CryptorFactory;
/**
* Implementation for any url based filesystem.<br />
@@ -127,7 +128,8 @@
{
try
{
- auth.password = EncryptDecrypt.decrypt(auth.password.substring(1, auth.password.length()-1));
+ Cryptor cryptor = CryptorFactory.getCryptor();
+ auth.password = cryptor.decrypt(auth.password.substring(1, auth.password.length()-1));
}
catch (Exception ex)
{
Added: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/Cryptor.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/Cryptor.java?rev=804397&view=auto
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/Cryptor.java (added)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/Cryptor.java Fri Aug 14 22:30:49 2009
@@ -0,0 +1,39 @@
+/*
+ * 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.commons.vfs.util;
+
+/**
+ * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
+ */
+public interface Cryptor
+{
+ /**
+ * Encrypt the plain text password.
+ * @param plainKey The password.
+ * @return The encrypted password String.
+ * @throws Exception If an error occurs.
+ */
+ String encrypt(String plainKey) throws Exception;
+
+ /**
+ * Decrypts the password.
+ * @param encryptedKey the encrypted password.
+ * @return The plain text password.
+ * @throws Exception If an error occurs.
+ */
+ String decrypt(String encryptedKey) throws Exception;
+}
Added: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/CryptorFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/CryptorFactory.java?rev=804397&view=auto
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/CryptorFactory.java (added)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/CryptorFactory.java Fri Aug 14 22:30:49 2009
@@ -0,0 +1,79 @@
+/*
+ * 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.commons.vfs.util;
+
+/**
+ * Factory to create an instance of a Cryptor.
+ * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
+ */
+public final class CryptorFactory
+{
+ /**
+ * The System property name to identify the Cryptor class to be used.
+ */
+ public static final String CRYPTOR_CLASS = "org.apache.commons.vfs.cryptor";
+
+ private static Cryptor instance;
+
+ /**
+ * Prevent instantiation of the class.
+ */
+ private CryptorFactory()
+ {
+
+ }
+
+ /**
+ * Allows the Cryptor class to be set programmatically.
+ * @param cryptor The Cryptor.
+ */
+ public static synchronized void setCryptor(Cryptor cryptor)
+ {
+ instance = cryptor;
+ }
+
+ /**
+ * Return the Cryptor. If one has not been previously set, create it. The Cryptor class
+ * can be set by setting the "org.apache.commons.vfs.cryptor" System property to the
+ * name of the Cryptor class.
+ * @return The Cryptor.
+ */
+ public static synchronized Cryptor getCryptor()
+ {
+ if (instance != null)
+ {
+ return instance;
+ }
+
+ String cryptorClass = System.getProperty(CRYPTOR_CLASS);
+ if (cryptorClass != null)
+ {
+ try
+ {
+ Class clazz = Class.forName(cryptorClass);
+ instance = (Cryptor) clazz.newInstance();
+ return instance;
+ }
+ catch (Exception ex)
+ {
+ throw new RuntimeException("Unable to create Cryptor " + cryptorClass, ex);
+ }
+ }
+ instance = new DefaultCryptor();
+ return instance;
+ }
+}
Copied: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/DefaultCryptor.java (from r804045, commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/EncryptDecrypt.java)
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/DefaultCryptor.java?p2=commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/DefaultCryptor.java&p1=commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/EncryptDecrypt.java&r1=804045&r2=804397&rev=804397&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/EncryptDecrypt.java (original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/DefaultCryptor.java Fri Aug 14 22:30:49 2009
@@ -21,38 +21,21 @@
/**
* Allows passwords to be encrypted and decrypted.
+ * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
*/
-public class EncryptDecrypt
+public class DefaultCryptor implements Cryptor
{
- private static char[] hexChars =
+ private static final char[] HEX_CHARS =
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
- private static byte[] keyBytes = {0x41, 0x70, 0x61, 0x63, 0x68, 0x65, 0x43, 0x6F, 0x6D, 0x6D,
+ private static final byte[] KEY_BYTES = {0x41, 0x70, 0x61, 0x63, 0x68, 0x65, 0x43, 0x6F, 0x6D, 0x6D,
0x6F, 0x6E, 0x73, 0x56, 0x46, 0x53};
- private static int INDEX_NOT_FOUND = -1;
+ private static final int INDEX_NOT_FOUND = -1;
- /**
- * This class can be called with "encrypt" password as the arguments where encrypt is
- * a literal and password is replaced with the clear text password to be encrypted.
- *
- * @param args The arguments in the form "command" parm1, parm2.
- * @throws Exception If an error occurs.
- */
- public static void main(String[] args) throws Exception
- {
- if (args.length != 2 || !(args[0].equals("encrypt")))
- {
- System.err.println("Usage: \"EncryptDecrypt encrypt\" password");
- System.err.println(" password : The clear text password to encrypt");
- System.exit(0);
- }
+ private static final int BITS_IN_HALF_BYTE = 4;
- if (args[0].equals("encrypt"))
- {
- System.out.println(encrypt(args[1]));
- }
- }
+ private static final char MASK = 0x0f;
/**
* Encrypt the plain text password.
@@ -60,10 +43,10 @@
* @return The encrypted password String.
* @throws Exception If an error occurs.
*/
- public static String encrypt(String plainKey) throws Exception
+ public String encrypt(String plainKey) throws Exception
{
byte[] input = plainKey.getBytes();
- SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
+ SecretKeySpec key = new SecretKeySpec(KEY_BYTES, "AES");
Cipher cipher = Cipher.getInstance("AES");
@@ -82,9 +65,9 @@
* @return The plain text password.
* @throws Exception If an error occurs.
*/
- public static String decrypt(String encryptedKey) throws Exception
+ public String decrypt(String encryptedKey) throws Exception
{
- SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
+ SecretKeySpec key = new SecretKeySpec(KEY_BYTES, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decoded = decode(encryptedKey);
@@ -94,19 +77,19 @@
return new String(plainText).substring(0, ptLength);
}
- private static String encode(byte[] bytes)
+ private String encode(byte[] bytes)
{
StringBuffer builder = new StringBuffer();
- for (int i=0; i < bytes.length; ++i)
+ for (int i = 0; i < bytes.length; ++i)
{
- builder.append(hexChars[(bytes[i] >> 4) & 0x0f]);
- builder.append(hexChars[bytes[i] & 0x0f]);
+ builder.append(HEX_CHARS[(bytes[i] >> BITS_IN_HALF_BYTE) & MASK]);
+ builder.append(HEX_CHARS[bytes[i] & MASK]);
}
return builder.toString();
}
- private static byte[] decode(String str)
+ private byte[] decode(String str)
{
int length = str.length() / 2;
byte[] decoded = new byte[length];
@@ -114,24 +97,24 @@
int index = 0;
for (int i = 0; i < chars.length; ++i)
{
- int id1 = indexOf(hexChars, chars[i]);
+ int id1 = indexOf(HEX_CHARS, chars[i]);
if (id1 == -1)
{
- throw new IllegalArgumentException("Character " + chars[i] + " at position " + i +
- " is not a valid hexideciam character");
+ throw new IllegalArgumentException("Character " + chars[i] + " at position " + i
+ + " is not a valid hexidecimal character");
}
- int id2 = indexOf(hexChars, chars[++i]);
+ int id2 = indexOf(HEX_CHARS, chars[++i]);
if (id2 == -1)
{
- throw new IllegalArgumentException("Character " + chars[i] + " at position " + i +
- " is not a valid hexideciam character");
+ throw new IllegalArgumentException("Character " + chars[i] + " at position " + i
+ + " is not a valid hexidecimal character");
}
- decoded[index++] = (byte) ((id1 << 4) | id2);
+ decoded[index++] = (byte) ((id1 << BITS_IN_HALF_BYTE) | id2);
}
return decoded;
}
- private static int indexOf(char[] array, char valueToFind)
+ private int indexOf(char[] array, char valueToFind)
{
if (array == null)
{
Copied: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/EncryptUtil.java (from r804045, commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/EncryptDecrypt.java)
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/EncryptUtil.java?p2=commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/EncryptUtil.java&p1=commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/EncryptDecrypt.java&r1=804045&r2=804397&rev=804397&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/EncryptDecrypt.java (original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/EncryptUtil.java Fri Aug 14 22:30:49 2009
@@ -16,21 +16,18 @@
*/
package org.apache.commons.vfs.util;
-import javax.crypto.Cipher;
-import javax.crypto.spec.SecretKeySpec;
-
/**
* Allows passwords to be encrypted and decrypted.
+ * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
*/
-public class EncryptDecrypt
+public final class EncryptUtil
{
- private static char[] hexChars =
- {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
-
- private static byte[] keyBytes = {0x41, 0x70, 0x61, 0x63, 0x68, 0x65, 0x43, 0x6F, 0x6D, 0x6D,
- 0x6F, 0x6E, 0x73, 0x56, 0x46, 0x53};
-
- private static int INDEX_NOT_FOUND = -1;
+ /**
+ * Don't allow class instantiation.
+ */
+ private EncryptUtil()
+ {
+ }
/**
* This class can be called with "encrypt" password as the arguments where encrypt is
@@ -43,107 +40,15 @@
{
if (args.length != 2 || !(args[0].equals("encrypt")))
{
- System.err.println("Usage: \"EncryptDecrypt encrypt\" password");
+ System.err.println("Usage: \"EncryptUtil encrypt\" password");
System.err.println(" password : The clear text password to encrypt");
System.exit(0);
}
+ Cryptor cryptor = CryptorFactory.getCryptor();
if (args[0].equals("encrypt"))
{
- System.out.println(encrypt(args[1]));
- }
- }
-
- /**
- * Encrypt the plain text password.
- * @param plainKey The password.
- * @return The encrypted password String.
- * @throws Exception If an error occurs.
- */
- public static String encrypt(String plainKey) throws Exception
- {
- byte[] input = plainKey.getBytes();
- SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
-
- Cipher cipher = Cipher.getInstance("AES");
-
- // encryption pass
- cipher.init(Cipher.ENCRYPT_MODE, key);
-
- byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
- int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
- ctLength += cipher.doFinal(cipherText, ctLength);
- return encode(cipherText);
- }
-
- /**
- * Decrypts the password.
- * @param encryptedKey the encrypted password.
- * @return The plain text password.
- * @throws Exception If an error occurs.
- */
- public static String decrypt(String encryptedKey) throws Exception
- {
- SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
- Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.DECRYPT_MODE, key);
- byte[] decoded = decode(encryptedKey);
- byte[] plainText = new byte[cipher.getOutputSize(decoded.length)];
- int ptLength = cipher.update(decoded, 0, decoded.length, plainText, 0);
- ptLength += cipher.doFinal(plainText, ptLength);
- return new String(plainText).substring(0, ptLength);
- }
-
- private static String encode(byte[] bytes)
- {
- StringBuffer builder = new StringBuffer();
-
- for (int i=0; i < bytes.length; ++i)
- {
- builder.append(hexChars[(bytes[i] >> 4) & 0x0f]);
- builder.append(hexChars[bytes[i] & 0x0f]);
- }
- return builder.toString();
- }
-
- private static byte[] decode(String str)
- {
- int length = str.length() / 2;
- byte[] decoded = new byte[length];
- char[] chars = str.toCharArray();
- int index = 0;
- for (int i = 0; i < chars.length; ++i)
- {
- int id1 = indexOf(hexChars, chars[i]);
- if (id1 == -1)
- {
- throw new IllegalArgumentException("Character " + chars[i] + " at position " + i +
- " is not a valid hexideciam character");
- }
- int id2 = indexOf(hexChars, chars[++i]);
- if (id2 == -1)
- {
- throw new IllegalArgumentException("Character " + chars[i] + " at position " + i +
- " is not a valid hexideciam character");
- }
- decoded[index++] = (byte) ((id1 << 4) | id2);
- }
- return decoded;
- }
-
- private static int indexOf(char[] array, char valueToFind)
- {
- if (array == null)
- {
- return INDEX_NOT_FOUND;
- }
- for (int i = 0; i < array.length; i++)
- {
- if (valueToFind == array[i])
- {
- return i;
- }
+ System.out.println(cryptor.encrypt(args[1]));
}
- return INDEX_NOT_FOUND;
}
}
Modified: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs/util/EncryptDecryptTest.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs/util/EncryptDecryptTest.java?rev=804397&r1=804396&r2=804397&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs/util/EncryptDecryptTest.java (original)
+++ commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs/util/EncryptDecryptTest.java Fri Aug 14 22:30:49 2009
@@ -29,10 +29,11 @@
String source = "Qryp2!t&tpR59";
String expected = "914589F049CE3E8E3BB1A41BEAE12A9C";
- String encrypted = EncryptDecrypt.encrypt(source);
+ Cryptor cryptor = CryptorFactory.getCryptor();
+ String encrypted = cryptor.encrypt(source);
assertEquals(expected, encrypted);
- String decrypted = EncryptDecrypt.decrypt(encrypted);
+ String decrypted = cryptor.decrypt(encrypted);
assertEquals(source, decrypted);
}
Modified: commons/proper/vfs/trunk/xdocs/filesystems.xml
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/xdocs/filesystems.xml?rev=804397&r1=804396&r2=804397&view=diff
==============================================================================
--- commons/proper/vfs/trunk/xdocs/filesystems.xml (original)
+++ commons/proper/vfs/trunk/xdocs/filesystems.xml Fri Aug 14 22:30:49 2009
@@ -100,14 +100,14 @@
<p>
To create an encrypted password do:
</p>
- <code> java -cp commons-vfs-2.0.jar org.apache.commons.vfs.util.EncryptDecrypt encrypt mypassword
+ <code> java -cp commons-vfs-2.0.jar org.apache.commons.vfs.util.EncryptUtil encrypt mypassword
</code>
<p>
where <i>mypassword</i> is the password you want to encrypt. The result of this will be a
single line of output containing uppercase hex characters. For example,
</p>
<code>
- java -cp commons-vfs-2.0.jar org.apache.commons.vfs.util.EncryptDecrypt encrypt WontUBee9
+ java -cp commons-vfs-2.0.jar org.apache.commons.vfs.util.EncryptUtil encrypt WontUBee9
D7B82198B272F5C93790FEB38A73C7B8
</code>
<p>