[jboss-cvs] JBossAS SVN: r95952 - projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Nov 3 09:34:40 EST 2009
Author: alesj
Date: 2009-11-03 09:34:40 -0500 (Tue, 03 Nov 2009)
New Revision: 95952
Added:
projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/AbstractKeyProvider.java
projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/CipherCrypter.java
projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/KeyPairProvider.java
projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/KeyProvider.java
projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/KeyProviderFactory.java
projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/SecretKeyProvider.java
projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/SingleKeyProvider.java
Modified:
projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/CryptVisitor.java
projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Crypter.java
projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Tools.java
Log:
Proper key creation abstraction.
Added: projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/AbstractKeyProvider.java
===================================================================
--- projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/AbstractKeyProvider.java (rev 0)
+++ projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/AbstractKeyProvider.java 2009-11-03 14:34:40 UTC (rev 95952)
@@ -0,0 +1,95 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.demos.classloader.crypt;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public abstract class AbstractKeyProvider<T> implements KeyProvider
+{
+ private File keystore;
+ private Class<T> keyClass;
+ private T key;
+
+ protected AbstractKeyProvider(Class<T> keyClass, File keystore)
+ {
+ if (keyClass == null)
+ throw new IllegalArgumentException("Null key class");
+ if (keystore == null)
+ throw new IllegalArgumentException("Null key");
+
+ this.keyClass = keyClass;
+ this.keystore = keystore;
+ }
+
+ protected T getKey() throws Exception
+ {
+ if (key == null)
+ key = readKey();
+
+ return key;
+ }
+
+ public T readKey() throws Exception
+ {
+ if (keystore.exists())
+ {
+ ObjectInputStream ois = new ObjectInputStream(new FileInputStream(keystore));
+ try
+ {
+ return keyClass.cast(ois.readObject());
+ }
+ finally
+ {
+ ois.close();
+ }
+ }
+ else
+ {
+ return generateKey();
+ }
+ }
+
+ protected abstract T generateKey() throws Exception;
+
+ public void writeKey() throws Exception
+ {
+ T key = getKey();
+
+ ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(keystore));
+ try
+ {
+ oos.writeObject(key);
+ oos.flush();
+ }
+ finally
+ {
+ oos.close();
+ }
+ }
+}
\ No newline at end of file
Copied: projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/CipherCrypter.java (from rev 95946, projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Crypter.java)
===================================================================
--- projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/CipherCrypter.java (rev 0)
+++ projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/CipherCrypter.java 2009-11-03 14:34:40 UTC (rev 95952)
@@ -0,0 +1,101 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.demos.classloader.crypt;
+
+import java.security.Key;
+import java.security.PrivateKey;
+import java.security.PublicKey;
+
+import javax.crypto.Cipher;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class CipherCrypter implements Crypter
+{
+ public static final String MODE = "CBC";
+ public static final String PADDING = "PKCS5Padding";
+ public static final String XFORM = ALGORITHM + "/" + MODE + "/" + PADDING;
+
+ private Cipher cipher;
+
+ public CipherCrypter(PrivateKey privateKey) throws Exception
+ {
+ this(null, privateKey);
+ }
+
+ public CipherCrypter(PublicKey publicKey) throws Exception
+ {
+ this(null, publicKey);
+ }
+
+ public CipherCrypter(String xform, PrivateKey privateKey) throws Exception
+ {
+ this(xform, Cipher.ENCRYPT_MODE, privateKey);
+ }
+
+ public CipherCrypter(String xform, PublicKey publicKey) throws Exception
+ {
+ this(xform, Cipher.DECRYPT_MODE, publicKey);
+ }
+
+ private CipherCrypter(String xform, int mode, Key key) throws Exception
+ {
+ if (key == null)
+ throw new IllegalArgumentException("Null key.");
+ if (xform == null)
+ xform = XFORM;
+
+ cipher = Cipher.getInstance(xform);
+ cipher.init(mode, key);
+ }
+
+ public static Crypter getEncrypter(Key key) throws Exception
+ {
+ return new CipherCrypter(null, Cipher.ENCRYPT_MODE, key);
+ }
+
+ public static Crypter getEncrypter(String xform, Key key) throws Exception
+ {
+ return new CipherCrypter(xform, Cipher.ENCRYPT_MODE, key);
+ }
+
+ public static Crypter getDecrypter(Key key) throws Exception
+ {
+ return new CipherCrypter(null, Cipher.DECRYPT_MODE, key);
+ }
+
+ public static Crypter getDecrypter(String xform, Key key) throws Exception
+ {
+ return new CipherCrypter(xform, Cipher.DECRYPT_MODE, key);
+ }
+
+ public byte[] encrypt(byte[] bytes) throws Exception
+ {
+ return cipher.doFinal(bytes);
+ }
+
+ public byte[] decrypt(byte[] bytes) throws Exception
+ {
+ return cipher.doFinal(bytes);
+ }
+}
\ No newline at end of file
Modified: projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/CryptVisitor.java
===================================================================
--- projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/CryptVisitor.java 2009-11-03 13:51:24 UTC (rev 95951)
+++ projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/CryptVisitor.java 2009-11-03 14:34:40 UTC (rev 95952)
@@ -24,19 +24,20 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
-import java.util.jar.Manifest;
import org.jboss.virtual.VFSUtils;
import org.jboss.virtual.VirtualFile;
import org.jboss.virtual.VirtualFileVisitor;
import org.jboss.virtual.VisitorAttributes;
+import org.jboss.virtual.plugins.vfs.helpers.AbstractVirtualFileFilterWithAttributes;
/**
* @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
*/
-public class CryptVisitor implements VirtualFileVisitor
+public class CryptVisitor extends AbstractVirtualFileFilterWithAttributes implements VirtualFileVisitor
{
private VirtualFile root;
private Crypter crypter;
@@ -45,6 +46,7 @@
public CryptVisitor(VirtualFile root, Crypter crypter) throws Exception
{
+ super(VisitorAttributes.RECURSE_LEAVES_ONLY);
this.root = root;
this.crypter = crypter;
}
@@ -53,11 +55,7 @@
{
file = File.createTempFile("crypt-", ".jar");
FileOutputStream fos = new FileOutputStream(file);
- Manifest manifest = VFSUtils.getManifest(root);
- if (manifest != null)
- jos = new JarOutputStream(fos, manifest);
- else
- jos = new JarOutputStream(fos);
+ jos = new JarOutputStream(fos);
root.visit(this);
@@ -72,9 +70,9 @@
return file;
}
- public VisitorAttributes getAttributes()
+ public boolean accepts(VirtualFile file)
{
- return VisitorAttributes.RECURSE_LEAVES_ONLY;
+ return file.getName().endsWith(".class");
}
public void visit(VirtualFile vf)
@@ -84,7 +82,12 @@
String path = vf.getPathName();
JarEntry entry = new JarEntry(path);
jos.putNextEntry(entry);
- jos.write(encrypt(vf));
+ byte[] bytes = bytes(vf);
+
+ if (accepts(vf))
+ bytes = crypter.encrypt(bytes);
+
+ jos.write(bytes);
}
catch (Exception e)
{
@@ -92,11 +95,10 @@
}
}
- protected byte[] encrypt(VirtualFile vf) throws Exception
+ protected byte[] bytes(VirtualFile vf) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
VFSUtils.copyStreamAndClose(vf.openStream(), baos);
- byte[] bytes = baos.toByteArray();
- return crypter.encrypt(bytes);
+ return baos.toByteArray();
}
}
Modified: projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Crypter.java
===================================================================
--- projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Crypter.java 2009-11-03 13:51:24 UTC (rev 95951)
+++ projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Crypter.java 2009-11-03 14:34:40 UTC (rev 95952)
@@ -21,67 +21,14 @@
*/
package org.jboss.demos.classloader.crypt;
-import java.security.PrivateKey;
-import java.security.PublicKey;
-
-import javax.crypto.Cipher;
-
/**
* @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
*/
-public class Crypter
+public interface Crypter
{
- private String xform;
- private PrivateKey privateKey;
- private PublicKey publicKey;
+ public static final String ALGORITHM = "AES";
- private Crypter(String xform)
- {
- if (xform == null)
- xform = "RSA/NONE/PKCS1PADDING";
+ byte[] encrypt(byte[] bytes) throws Exception;
- this.xform = xform;
- }
-
- public Crypter(PrivateKey privateKey)
- {
- this(null, privateKey);
- }
-
- public Crypter(PublicKey publicKey)
- {
- this(null, publicKey);
- }
-
- public Crypter(String xform, PrivateKey privateKey)
- {
- this(xform);
- this.privateKey = privateKey;
- }
-
- public Crypter(String xform, PublicKey publicKey)
- {
- this(xform);
- this.publicKey = publicKey;
- }
-
- public byte[] encrypt(byte[] bytes) throws Exception
- {
- if (privateKey == null)
- throw new IllegalArgumentException("No private key, cannot encrypt.");
-
- Cipher cipher = Cipher.getInstance(xform);
- cipher.init(Cipher.ENCRYPT_MODE, privateKey);
- return cipher.doFinal(bytes);
- }
-
- public byte[] decrypt(byte[] bytes) throws Exception
- {
- if (publicKey == null)
- throw new IllegalArgumentException("No public key, cannot decrypt.");
-
- Cipher cipher = Cipher.getInstance(xform);
- cipher.init(Cipher.DECRYPT_MODE, publicKey);
- return cipher.doFinal(bytes);
- }
+ byte[] decrypt(byte[] bytes) throws Exception;
}
Added: projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/KeyPairProvider.java
===================================================================
--- projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/KeyPairProvider.java (rev 0)
+++ projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/KeyPairProvider.java 2009-11-03 14:34:40 UTC (rev 95952)
@@ -0,0 +1,55 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.demos.classloader.crypt;
+
+import java.io.File;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.Key;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class KeyPairProvider extends AbstractKeyProvider<KeyPair>
+{
+ public KeyPairProvider(File keystore)
+ {
+ super(KeyPair.class, keystore);
+ }
+
+ protected KeyPair generateKey() throws Exception
+ {
+ KeyPairGenerator kpg = KeyPairGenerator.getInstance(Crypter.ALGORITHM);
+ kpg.initialize(512); // keysize.
+ return kpg.generateKeyPair();
+ }
+
+ public Key getEncriptionKey() throws Exception
+ {
+ return getKey().getPrivate();
+ }
+
+ public Key getDecriptionKey() throws Exception
+ {
+ return getKey().getPublic();
+ }
+}
\ No newline at end of file
Copied: projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/KeyProvider.java (from rev 95946, projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Tools.java)
===================================================================
--- projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/KeyProvider.java (rev 0)
+++ projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/KeyProvider.java 2009-11-03 14:34:40 UTC (rev 95952)
@@ -0,0 +1,36 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.demos.classloader.crypt;
+
+import java.security.Key;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public interface KeyProvider
+{
+ Key getEncriptionKey() throws Exception;
+
+ Key getDecriptionKey() throws Exception;
+
+ void writeKey() throws Exception;
+}
\ No newline at end of file
Added: projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/KeyProviderFactory.java
===================================================================
--- projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/KeyProviderFactory.java (rev 0)
+++ projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/KeyProviderFactory.java 2009-11-03 14:34:40 UTC (rev 95952)
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.demos.classloader.crypt;
+
+import java.io.File;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class KeyProviderFactory
+{
+ public static KeyProvider createKeyProvider(File keystore)
+ {
+ if ("RSA".equals(Crypter.ALGORITHM))
+ return new KeyPairProvider(keystore);
+ else if ("AES".equals(Crypter.ALGORITHM))
+ return new SecretKeyProvider(keystore);
+ else
+ return new SingleKeyProvider(keystore);
+ }
+}
\ No newline at end of file
Added: projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/SecretKeyProvider.java
===================================================================
--- projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/SecretKeyProvider.java (rev 0)
+++ projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/SecretKeyProvider.java 2009-11-03 14:34:40 UTC (rev 95952)
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.demos.classloader.crypt;
+
+import java.io.File;
+import java.security.Key;
+
+import javax.crypto.spec.SecretKeySpec;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class SecretKeyProvider extends SingleKeyProvider
+{
+ public SecretKeyProvider(File keystore)
+ {
+ super(keystore);
+ }
+
+ @Override
+ protected Key wrapKey(Key key)
+ {
+ byte[] raw = key.getEncoded();
+ return new SecretKeySpec(raw, Crypter.ALGORITHM);
+ }
+}
\ No newline at end of file
Added: projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/SingleKeyProvider.java
===================================================================
--- projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/SingleKeyProvider.java (rev 0)
+++ projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/SingleKeyProvider.java 2009-11-03 14:34:40 UTC (rev 95952)
@@ -0,0 +1,63 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.demos.classloader.crypt;
+
+import java.io.File;
+import java.security.Key;
+
+import javax.crypto.KeyGenerator;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class SingleKeyProvider extends AbstractKeyProvider<Key>
+{
+ private Key key;
+
+ public SingleKeyProvider(File keystore)
+ {
+ super(Key.class, keystore);
+ }
+
+ protected Key generateKey() throws Exception
+ {
+ KeyGenerator kg = KeyGenerator.getInstance(Crypter.ALGORITHM);
+ kg.init(128); // keysize.
+ Key key = kg.generateKey();
+ return wrapKey(key);
+ }
+
+ protected Key wrapKey(Key key)
+ {
+ return key;
+ }
+
+ public Key getEncriptionKey() throws Exception
+ {
+ return getKey();
+ }
+
+ public Key getDecriptionKey() throws Exception
+ {
+ return getKey();
+ }
+}
\ No newline at end of file
Modified: projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Tools.java
===================================================================
--- projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Tools.java 2009-11-03 13:51:24 UTC (rev 95951)
+++ projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Tools.java 2009-11-03 14:34:40 UTC (rev 95952)
@@ -22,13 +22,7 @@
package org.jboss.demos.classloader.crypt;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.security.KeyPair;
-import java.security.KeyPairGenerator;
-import java.security.PrivateKey;
+import java.security.Key;
import java.util.Arrays;
import org.jboss.virtual.VFS;
@@ -40,9 +34,9 @@
public class Tools
{
private File jarToCrypt;
- private PrivateKey key;
+ private Key key;
- public Tools(File jarToCrypt, PrivateKey key)
+ public Tools(File jarToCrypt, Key key)
{
this.jarToCrypt = jarToCrypt;
this.key = key;
@@ -60,63 +54,30 @@
try
{
File keystore = new File(args[1]);
- KeyPair kp = readKeyPair(keystore, true);
+ KeyProvider keyProvider = KeyProviderFactory.createKeyProvider(keystore);
if (keystore.exists() == false)
- writeKeyPair(keystore, kp);
+ keyProvider.writeKey();
- PrivateKey privateKey = kp.getPrivate();
- Tools tools = new Tools(archive, privateKey);
+ Key key = keyProvider.getEncriptionKey();
+ Tools tools = new Tools(archive, key);
tools.crypt();
}
catch (Exception e)
{
- System.err.println(e);
+ e.printStackTrace();
}
}
public void crypt() throws Exception
{
VirtualFile root = VFS.getRoot(jarToCrypt.toURI());
- Crypter crypter = new Crypter(key);
+ Crypter crypter = CipherCrypter.getEncrypter(key);
CryptVisitor visitor = new CryptVisitor(root, crypter);
File file = visitor.getFile();
- if (file.renameTo(jarToCrypt))
+ File copy = new File(jarToCrypt.getParentFile(), file.getName());
+ if (file.renameTo(copy) == false)
{
- System.err.println("Cannot rename file: " + file);
+ System.err.println("Cannot move file: " + copy);
}
}
-
- public static KeyPair readKeyPair(File keystore, boolean create) throws Exception
- {
- boolean exists = keystore.exists();
- if (exists == false && create == false)
- throw new IllegalArgumentException("No such file: " + keystore);
-
- if (exists)
- {
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream(keystore));
- ois.close();
- return KeyPair.class.cast(ois.readObject());
- }
- else
- {
- KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
- kpg.initialize(512); // 512 is the keysize.
- return kpg.generateKeyPair();
- }
- }
-
- public static void writeKeyPair(File keystore, KeyPair kp) throws Exception
- {
- ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(keystore));
- try
- {
- oos.writeObject(kp);
- oos.flush();
- }
- finally
- {
- oos.close();
- }
- }
}
\ No newline at end of file
More information about the jboss-cvs-commits
mailing list