Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • web2/EJB-EncryptionBean
  • wilsonsf/EJB-EncryptionBean
2 results
Show changes
Commits on Source (8)
Showing
with 285 additions and 159 deletions
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" path="ejbModule"/> <classpathentry kind="src" output="target/classes" path="src/main/java">
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.jboss.ide.eclipse.as.core.server.runtime.runtimeTarget/WildFly 9.x Runtime">
<attributes> <attributes>
<attribute name="owner.project.facets" value="jst.ejb;#system#"/> <attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/java"> <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes> <attributes>
<attribute name="owner.project.facets" value="java"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="output" path="build/classes"/> <classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.jboss.ide.eclipse.as.core.server.runtime.runtimeTarget/WildFly 14.0 Runtime">
<attributes>
<attribute name="owner.project.facets" value="jst.ejb"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>
/target/
...@@ -15,16 +15,40 @@ ...@@ -15,16 +15,40 @@
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand>
<name>org.jboss.tools.jst.web.kb.kbbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.fusesource.ide.project.RiderProjectBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.jboss.tools.cdi.core.cdibuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand> <buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name> <name>org.eclipse.wst.validation.validationbuilder</name>
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec> </buildSpec>
<natures> <natures>
<nature>org.fusesource.ide.project.RiderProjectNature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature> <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature> <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature> <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.jboss.tools.jst.web.kb.kbnature</nature>
<nature>org.jboss.tools.cdi.core.cdinature</nature>
</natures> </natures>
</projectDescription> </projectDescription>
eclipse.preferences.version=1 eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7 org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0"> <?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="Encryption-Bean"> <wb-module deploy-name="Encryption-Bean">
<wb-resource deploy-path="/" source-path="/ejbModule" tag="defaultRootSource"/> <wb-resource deploy-path="/" source-path="/src/main/java"/>
<wb-resource deploy-path="/" source-path="/src/main/resources" tag="defaultRootSource"/>
<property name="java-output-path" value="/Encryption-Bean/build/classes"/> <property name="java-output-path" value="/Encryption-Bean/build/classes"/>
<property name="ClientProject" value="Encryption-BeanClient"/> <property name="ClientProject" value="Encryption-BeanClient"/>
<property name="ClientJARURI" value="Encryption-BeanClient.jar"/> <property name="ClientJARURI" value="Encryption-BeanClient.jar"/>
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<faceted-project> <faceted-project>
<runtime name="WildFly 9.x Runtime"/> <runtime name="WildFly 14.0 Runtime"/>
<fixed facet="jst.ejb"/> <fixed facet="jst.ejb"/>
<fixed facet="java"/> <fixed facet="java"/>
<installed facet="java" version="1.7"/> <installed facet="java" version="1.8"/>
<installed facet="jst.ejb" version="3.1"/> <installed facet="jst.ejb" version="3.2"/>
</faceted-project> </faceted-project>
disabled=06target
eclipse.preferences.version=1
Manifest-Version: 1.0
Class-Path: Encryption-BeanClient.jar
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version="3.1">
<display-name>Encryption-Bean </display-name>
<ejb-client-jar>Encryption-BeanClient.jar</ejb-client-jar>
</ejb-jar>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 EJB 3.0//EN" "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd">
<sun-ejb-jar>
<enterprise-beans/>
</sun-ejb-jar>
Manifest-Version: 1.0
Class-Path: Encryption-BeanClient.jar
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 EJB 3.0//EN" "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd">
<sun-ejb-jar>
<enterprise-beans/>
</sun-ejb-jar>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.ufrn.imd.web2</groupId>
<artifactId>Encryption-Bean</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>ejb</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.5</version>
<configuration>
<ejbVersion>3.2</ejbVersion>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
</dependency>
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>javax.ejb-api</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package br.ufrn.imd.imd0409.encryption; package br.ufrn.imd.web2.encryption;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec; import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec; import java.security.spec.KeySpec;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.logging.Logger;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory; import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEKeySpec;
...@@ -23,26 +28,29 @@ import javax.ejb.Stateless; ...@@ -23,26 +28,29 @@ import javax.ejb.Stateless;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import br.ufrn.imd.imd0409.encryption.EncryptionException; import br.ufrn.imd.web2.encryption.EncryptionException;
import br.ufrn.imd.imd0409.encryption.EncryptionLocalBusiness; import br.ufrn.imd.web2.encryption.EncryptionLocal;
import br.ufrn.imd.imd0409.encryption.EncryptionRemoteBusiness; import br.ufrn.imd.web2.encryption.EncryptionRemote;
@Stateless(name = EncryptionBean.EJB_NAME) @Stateless(name = EncryptionBean.EJB_NAME)
@Local(EncryptionLocalBusiness.class) @Local(EncryptionLocal.class)
@Remote(EncryptionRemoteBusiness.class) @Remote(EncryptionRemote.class)
public class EncryptionBean implements EncryptionLocalBusiness, public class EncryptionBean implements EncryptionLocal, EncryptionRemote {
EncryptionRemoteBusiness {
/**
* Logger
*/
private static final Logger log = Logger.getLogger(EncryptionBean.class.getName());
/** /**
* Name we'll assign to this EJB, will be referenced in the corresponding * Name we'll assign to this EJB, will be referenced in the corresponding
* META-INF/ejb-jar.xml file * META-INF/ejb-jar.xml file
*/ */
static final String EJB_NAME = "EncryptionEJB"; static final String EJB_NAME = "EncryptionBean";
/** /**
* Name of the environment entry representing the ciphers' passphrase * Name of the environment entry representing the ciphers' passphrase supplied
* supplied in ejb-jar.xml * in ejb-jar.xml
*/ */
private static final String ENV_ENTRY_NAME_CIPHERS_PASSPHRASE = "ciphersPassphrase"; private static final String ENV_ENTRY_NAME_CIPHERS_PASSPHRASE = "ciphersPassphrase";
...@@ -75,9 +83,8 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -75,9 +83,8 @@ public class EncryptionBean implements EncryptionLocalBusiness,
/** /**
* The salt used in symmetric encryption/decryption * The salt used in symmetric encryption/decryption
*/ */
private static final byte[] DEFAULT_SALT_CIPHERS = { (byte) 0xB4, private static final byte[] DEFAULT_SALT_CIPHERS = { (byte) 0xB4, (byte) 0xA2, (byte) 0x43, (byte) 0x89, 0x3E,
(byte) 0xA2, (byte) 0x43, (byte) 0x89, 0x3E, (byte) 0xC5, (byte) 0xC5, (byte) 0x78, (byte) 0x53 };
(byte) 0x78, (byte) 0x53 };
/** /**
* Iteration count used for symmetric encryption/decryption * Iteration count used for symmetric encryption/decryption
...@@ -90,9 +97,9 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -90,9 +97,9 @@ public class EncryptionBean implements EncryptionLocalBusiness,
// ---------------------------------------------------------------------------|| // ---------------------------------------------------------------------------||
/* /*
* The following members represent the internal state of the Service. Note * The following members represent the internal state of the Service. Note how
* how these are *not* leaked out via the end-user API, and are hence part * these are *not* leaked out via the end-user API, and are hence part of
* of "internal state" and not "conversational state". * "internal state" and not "conversational state".
*/ */
/** /**
...@@ -103,8 +110,8 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -103,8 +110,8 @@ public class EncryptionBean implements EncryptionLocalBusiness,
private SessionContext context; private SessionContext context;
/** /**
* Passphrase to use for the key in cipher operations; lazily initialized * Passphrase to use for the key in cipher operations; lazily initialized and
* and loaded via SessionContext.lookup * loaded via SessionContext.lookup
*/ */
private String ciphersPassphrase; private String ciphersPassphrase;
...@@ -138,14 +145,13 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -138,14 +145,13 @@ public class EncryptionBean implements EncryptionLocalBusiness,
/** /**
* Initializes this service before it may handle requests * Initializes this service before it may handle requests
* *
* @throws Exception * @throws RuntimeException
* If some unexpected error occurred * If some unexpected error occurred
*/ */
@PostConstruct @PostConstruct
public void initialize() throws Exception { public void initialize() {
// Log that we're here // Log that we're here
System.out.println("Initializing, part of " + PostConstruct.class.getName() log.info("Initializing, part of " + PostConstruct.class.getName() + " lifecycle");
+ " lifecycle");
/* /*
* Symmetric Encryption * Symmetric Encryption
...@@ -158,23 +164,55 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -158,23 +164,55 @@ public class EncryptionBean implements EncryptionLocalBusiness,
final String ciphersPassphrase = this.getCiphersPassphrase(); final String ciphersPassphrase = this.getCiphersPassphrase();
// Obtain key and param spec for the ciphers // Obtain key and param spec for the ciphers
final KeySpec ciphersKeySpec = new PBEKeySpec( final KeySpec ciphersKeySpec = new PBEKeySpec(ciphersPassphrase.toCharArray(), ciphersSalt,
ciphersPassphrase.toCharArray(), ciphersSalt,
ciphersIterationCount); ciphersIterationCount);
final SecretKey ciphersKey = SecretKeyFactory.getInstance(
cipherAlgorithm).generateSecret(ciphersKeySpec); SecretKey ciphersKey;
final AlgorithmParameterSpec paramSpec = new PBEParameterSpec( try {
ciphersSalt, ciphersIterationCount); ciphersKey = SecretKeyFactory.getInstance(cipherAlgorithm).generateSecret(ciphersKeySpec);
} catch (InvalidKeySpecException e1) {
throw new RuntimeException("Could not obtain the " + KeySpec.class.getSimpleName()
+ " for algorithm: " + cipherAlgorithm, e1);
} catch (NoSuchAlgorithmException e1) {
throw new RuntimeException("Could not obtain the " + KeySpec.class.getSimpleName()
+ " for algorithm: " + cipherAlgorithm, e1);
}
final AlgorithmParameterSpec paramSpec = new PBEParameterSpec(ciphersSalt, ciphersIterationCount);
// Create and init the ciphers // Create and init the ciphers
this.encryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm()); try {
this.decryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm()); this.encryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm());
encryptionCipher.init(Cipher.ENCRYPT_MODE, ciphersKey, paramSpec); this.decryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm());
decryptionCipher.init(Cipher.DECRYPT_MODE, ciphersKey, paramSpec); } catch (NoSuchAlgorithmException e1) {
throw new RuntimeException("Could not obtain the " + Cipher.class.getSimpleName()
+ " for algorithm: " + ciphersKey.getAlgorithm(), e1);
} catch (NoSuchPaddingException e1) {
throw new RuntimeException("Could not obtain the " + Cipher.class.getSimpleName()
+ " for algorithm: " + ciphersKey.getAlgorithm() + "due to unavailable padding scheme", e1);
}
try {
encryptionCipher.init(Cipher.ENCRYPT_MODE, ciphersKey, paramSpec);
} catch (InvalidKeyException e1) {
throw new RuntimeException("Could not initialize the encryption cipher " + Cipher.class.getSimpleName()
+ " due to invalid encryption key for algorithm: " + ciphersKey.getAlgorithm(), e1);
} catch (InvalidAlgorithmParameterException e1) {
throw new RuntimeException("Could not initialize the " + Cipher.class.getSimpleName()
+ " due to invalid algorithm: " + ciphersKey.getAlgorithm(), e1);
}
try {
decryptionCipher.init(Cipher.DECRYPT_MODE, ciphersKey, paramSpec);
} catch (InvalidKeyException e1) {
throw new RuntimeException("Could not initialize the decription cipher " + Cipher.class.getSimpleName()
+ " due to invalid encryption key for algorithm: " + ciphersKey.getAlgorithm(), e1);
} catch (InvalidAlgorithmParameterException e1) {
throw new RuntimeException("Could not initialize the decription cipher " + Cipher.class.getSimpleName()
+ " due to invalid algorithm: " + ciphersKey.getAlgorithm(), e1);
}
// Log // Log
System.out.println("Initialized encryption cipher: " + this.encryptionCipher); log.info("Initialized encryption cipher: " + this.encryptionCipher);
System.out.println("Initialized decryption cipher: " + this.decryptionCipher); log.info("Initialized decryption cipher: " + this.decryptionCipher);
/* /*
* One-way Hashing * One-way Hashing
...@@ -185,15 +223,12 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -185,15 +223,12 @@ public class EncryptionBean implements EncryptionLocalBusiness,
// Create the MessageDigest // Create the MessageDigest
try { try {
this.messageDigest = MessageDigest this.messageDigest = MessageDigest.getInstance(messageDigestAlgorithm);
.getInstance(messageDigestAlgorithm);
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Could not obtain the " throw new RuntimeException("Could not obtain the " + MessageDigest.class.getSimpleName()
+ MessageDigest.class.getSimpleName() + " for algorithm: " + " for algorithm: " + messageDigestAlgorithm, e);
+ messageDigestAlgorithm, e);
} }
System.out.println("Initialized MessageDigest for one-way hashing: " log.info("Initialized MessageDigest for one-way hashing: " + this.messageDigest);
+ this.messageDigest);
} }
// ---------------------------------------------------------------------------|| // ---------------------------------------------------------------------------||
...@@ -204,12 +239,11 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -204,12 +239,11 @@ public class EncryptionBean implements EncryptionLocalBusiness,
/** /**
* {@inheritDoc} * {@inheritDoc}
* *
* @see org.jboss.ejb3.examples.ch05.encryption.EncryptionCommonBusiness#compare(java.lang.String, * @see org.jboss.ejb3.EncryptionCommon.ch05.encryption.EncryptionCommonBusiness#compare(java.lang.String,
* java.lang.String) * java.lang.String)
*/ */
@Override @Override
public boolean compare(final String hash, final String input) public boolean compare(final String hash, final String input) throws IllegalArgumentException, EncryptionException {
throws IllegalArgumentException, EncryptionException {
// Precondition checks // Precondition checks
if (hash == null) { if (hash == null) {
throw new IllegalArgumentException("hash is required."); throw new IllegalArgumentException("hash is required.");
...@@ -231,16 +265,15 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -231,16 +265,15 @@ public class EncryptionBean implements EncryptionLocalBusiness,
/** /**
* {@inheritDoc} * {@inheritDoc}
* *
* @see org.jboss.ejb3.examples.ch05.encryption.EncryptionCommonBusiness#decrypt(java.lang.String) * @see org.jboss.ejb3.EncryptionCommon.ch05.encryption.EncryptionCommonBusiness#decrypt(java.lang.String)
*/ */
@Override @Override
public String decrypt(final String input) throws IllegalArgumentException, public String decrypt(final String input)
IllegalStateException, EncryptionException { throws IllegalArgumentException, IllegalStateException, EncryptionException {
// Get the cipher // Get the cipher
final Cipher cipher = this.decryptionCipher; final Cipher cipher = this.decryptionCipher;
if (cipher == null) { if (cipher == null) {
throw new IllegalStateException( throw new IllegalStateException("Decyrption cipher not available, has this service been initialized?");
"Decyrption cipher not available, has this service been initialized?");
} }
// Run the cipher // Run the cipher
...@@ -255,7 +288,7 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -255,7 +288,7 @@ public class EncryptionBean implements EncryptionLocalBusiness,
final String result = this.byteArrayToString(resultBytes); final String result = this.byteArrayToString(resultBytes);
// Log // Log
System.out.println("Decryption on \"" + input + "\": " + result); log.info("Decryption on \"" + input + "\": " + result);
// Return // Return
return result; return result;
...@@ -264,16 +297,14 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -264,16 +297,14 @@ public class EncryptionBean implements EncryptionLocalBusiness,
/** /**
* {@inheritDoc} * {@inheritDoc}
* *
* @see org.jboss.ejb3.examples.ch05.encryption.EncryptionCommonBusiness#encrypt(java.lang.String) * @see org.jboss.ejb3.EncryptionCommon.ch05.encryption.EncryptionCommonBusiness#encrypt(java.lang.String)
*/ */
@Override @Override
public String encrypt(final String input) throws IllegalArgumentException, public String encrypt(final String input) throws IllegalArgumentException, EncryptionException {
EncryptionException {
// Get the cipher // Get the cipher
final Cipher cipher = this.encryptionCipher; final Cipher cipher = this.encryptionCipher;
if (cipher == null) { if (cipher == null) {
throw new IllegalStateException( throw new IllegalStateException("Encyrption cipher not available, has this service been initialized?");
"Encyrption cipher not available, has this service been initialized?");
} }
// Get bytes from the String // Get bytes from the String
...@@ -288,8 +319,7 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -288,8 +319,7 @@ public class EncryptionBean implements EncryptionLocalBusiness,
} }
// Log // Log
System.out.println("Encryption on \"" + input + "\": " log.info("Encryption on \"" + input + "\": " + this.byteArrayToString(resultBytes));
+ this.byteArrayToString(resultBytes));
// Return // Return
final String result = this.byteArrayToString(resultBytes); final String result = this.byteArrayToString(resultBytes);
...@@ -302,19 +332,17 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -302,19 +332,17 @@ public class EncryptionBean implements EncryptionLocalBusiness,
* This is a weak implementation, but is enough to satisfy the example. If * This is a weak implementation, but is enough to satisfy the example. If
* considering real-world stresses, we would be, at a minimum: * considering real-world stresses, we would be, at a minimum:
* *
* 1) Incorporating a random salt and storing it alongside the hashed result * 1) Incorporating a random salt and storing it alongside the hashed result 2)
* 2) Additionally implementing an iteration count to re-hash N times * Additionally implementing an iteration count to re-hash N times
*/ */
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see * @see org.jboss.ejb3.examples.ch05.encryption.EncryptionCommonBusiness#hash
* org.jboss.ejb3.examples.ch05.encryption.EncryptionCommonBusiness#hash
* (java.lang.String) * (java.lang.String)
*/ */
@Override @Override
public String hash(final String input) throws IllegalArgumentException, public String hash(final String input) throws IllegalArgumentException, EncryptionException {
EncryptionException {
// Precondition check // Precondition check
if (input == null) { if (input == null) {
throw new IllegalArgumentException("Input is required."); throw new IllegalArgumentException("Input is required.");
...@@ -334,7 +362,7 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -334,7 +362,7 @@ public class EncryptionBean implements EncryptionLocalBusiness,
// Get the input back in some readable format // Get the input back in some readable format
final String hash = this.byteArrayToString(encodedBytes); final String hash = this.byteArrayToString(encodedBytes);
System.out.println("One-way hash of \"" + input + "\": " + hash); log.info("One-way hash of \"" + input + "\": " + hash);
// Return // Return
return hash; return hash;
...@@ -343,12 +371,11 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -343,12 +371,11 @@ public class EncryptionBean implements EncryptionLocalBusiness,
/** /**
* {@inheritDoc} * {@inheritDoc}
* *
* @see org.jboss.ejb3.examples.ch05.encryption.EncryptionCommonBusiness#hashAsync(java.lang.String) * @see org.jboss.ejb3.EncryptionCommon.ch05.encryption.EncryptionCommonBusiness#hashAsync(java.lang.String)
*/ */
@Asynchronous @Asynchronous
@Override @Override
public Future<String> hashAsync(final String input) public Future<String> hashAsync(final String input) throws IllegalArgumentException, EncryptionException {
throws IllegalArgumentException, EncryptionException {
// Get the real hash // Get the real hash
final String hash = this.hash(input); final String hash = this.hash(input);
...@@ -357,16 +384,16 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -357,16 +384,16 @@ public class EncryptionBean implements EncryptionLocalBusiness,
} }
/** /**
* Override the way we get the ciphers' passphrase so that we may define it * Override the way we get the ciphers' passphrase so that we may define it in a
* in a secure location on the server. Now our production systems will use a * secure location on the server. Now our production systems will use a
* different key for encoding than our development servers, and we may limit * different key for encoding than our development servers, and we may limit the
* the likelihood of a security breach while still allowing our programmer * likelihood of a security breach while still allowing our programmer to use
* to use the default passphrase transparently during development. * the default passphrase transparently during development.
* *
* If not provided as an env-entry, fall back upon the default. * If not provided as an env-entry, fall back upon the default.
* *
* Note that a real system won't expose this method in the public API, ever. * Note that a real system won't expose this method in the public API, ever. We
* We do here for testing and to illustrate the example. * do here for testing and to illustrate the example.
* *
* @see org.jboss.ejb3.examples.ch05.encryption.EncryptionBeanBase#getCiphersPassphrase() * @see org.jboss.ejb3.examples.ch05.encryption.EncryptionBeanBase#getCiphersPassphrase()
*/ */
...@@ -379,8 +406,7 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -379,8 +406,7 @@ public class EncryptionBean implements EncryptionLocalBusiness,
if (passphrase == null) { if (passphrase == null) {
// Do a lookup via SessionContext // Do a lookup via SessionContext
passphrase = this passphrase = this.getEnvironmentEntryAsString(ENV_ENTRY_NAME_CIPHERS_PASSPHRASE);
.getEnvironmentEntryAsString(ENV_ENTRY_NAME_CIPHERS_PASSPHRASE);
// See if provided // See if provided
if (passphrase == null) { if (passphrase == null) {
...@@ -399,25 +425,25 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -399,25 +425,25 @@ public class EncryptionBean implements EncryptionLocalBusiness,
} }
// In a secure system, we don't log this. ;) // In a secure system, we don't log this. ;)
System.out.println("Using encryption passphrase for ciphers keys: " + passphrase); log.info("Using encryption passphrase for ciphers keys: " + passphrase);
// Return // Return
return passphrase; return passphrase;
} }
/** /**
* Obtains the message digest algorithm as injected from the env-entry * Obtains the message digest algorithm as injected from the env-entry element
* element defined in ejb-jar.xml. If not specified, fall back onto the * defined in ejb-jar.xml. If not specified, fall back onto the default, logging
* default, logging a warn message * a warn message
* *
* @see org.jboss.ejb3.examples.ch05.encryption.EncryptionRemoteBusiness#getMessageDigestAlgorithm() * @see org.jboss.ejb3.EncryptionRemote.ch05.encryption.EncryptionRemoteBusiness#getMessageDigestAlgorithm()
*/ */
@Override @Override
public String getMessageDigestAlgorithm() { public String getMessageDigestAlgorithm() {
// First see if this has been injected/set // First see if this has been injected/set
if (this.messageDigestAlgorithm == null) { if (this.messageDigestAlgorithm == null) {
// Log a warning // Log a warning
System.out.println("No message digest algorithm has been supplied explicitly via " log.warning("No message digest algorithm has been supplied explicitly via "
+ "an env-entry, falling back on the default..."); + "an env-entry, falling back on the default...");
// Set // Set
...@@ -425,8 +451,7 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -425,8 +451,7 @@ public class EncryptionBean implements EncryptionLocalBusiness,
} }
// Log // Log
System.out.println("Configured MessageDigest one-way hash algorithm is: " log.info("Configured MessageDigest one-way hash algorithm is: " + this.messageDigestAlgorithm);
+ this.messageDigestAlgorithm);
// Return // Return
return this.messageDigestAlgorithm; return this.messageDigestAlgorithm;
...@@ -438,23 +463,21 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -438,23 +463,21 @@ public class EncryptionBean implements EncryptionLocalBusiness,
// ---------------------------------------------------------------------------|| // ---------------------------------------------------------------------------||
/** /**
* Obtains the environment entry with the specified name, casting to a * Obtains the environment entry with the specified name, casting to a String,
* String, and returning the result. If the entry is not assignable to a * and returning the result. If the entry is not assignable to a String, an
* String, an {@link IllegalStateException} will be raised. In the event * {@link IllegalStateException} will be raised. In the event that the specified
* that the specified environment entry cannot be found, a warning message * environment entry cannot be found, a warning message will be logged and we'll
* will be logged and we'll return null. * return null.
* *
* @param envEntryName * @param envEntryName
* @return * @return
* @throws IllegalStateException * @throws IllegalStateException
*/ */
private String getEnvironmentEntryAsString(final String envEntryName) private String getEnvironmentEntryAsString(final String envEntryName) throws IllegalStateException {
throws IllegalStateException {
// See if we have a SessionContext // See if we have a SessionContext
final SessionContext context = this.context; final SessionContext context = this.context;
if (context == null) { if (context == null) {
System.out.println("No SessionContext, bypassing request to obtain environment entry: " log.warning("No SessionContext, bypassing request to obtain environment entry: " + envEntryName);
+ envEntryName);
return null; return null;
} }
...@@ -462,13 +485,11 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -462,13 +485,11 @@ public class EncryptionBean implements EncryptionLocalBusiness,
Object lookupValue = null; Object lookupValue = null;
try { try {
lookupValue = context.lookup(envEntryName); lookupValue = context.lookup(envEntryName);
System.out.println("Obtained environment entry \"" + envEntryName + "\": " log.fine("Obtained environment entry \"" + envEntryName + "\": " + lookupValue);
+ lookupValue);
} catch (final IllegalArgumentException iae) { } catch (final IllegalArgumentException iae) {
// Not found defined within this EJB's Component Environment, // Not found defined within this EJB's Component Environment,
// so return null and let the caller handle it // so return null and let the caller handle it
System.out.println("Could not find environment entry with name: " log.warning("Could not find environment entry with name: " + envEntryName);
+ envEntryName);
return null; return null;
} }
...@@ -477,9 +498,8 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -477,9 +498,8 @@ public class EncryptionBean implements EncryptionLocalBusiness,
try { try {
returnValue = String.class.cast(lookupValue); returnValue = String.class.cast(lookupValue);
} catch (final ClassCastException cce) { } catch (final ClassCastException cce) {
throw new IllegalStateException("The specified environment entry, " throw new IllegalStateException("The specified environment entry, " + lookupValue
+ lookupValue + ", was not able to be represented as a " + ", was not able to be represented as a " + String.class.getName(), cce);
+ String.class.getName(), cce);
} }
// Return // Return
...@@ -487,21 +507,19 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -487,21 +507,19 @@ public class EncryptionBean implements EncryptionLocalBusiness,
} }
/** /**
* Returns a String representation of the specified byte array using the * Returns a String representation of the specified byte array using the charset
* charset from {@link EncryptionBeanBase#getCharset()}. Wraps any * from {@link EncryptionBeanBase#getCharset()}. Wraps any
* {@link UnsupportedEncodingException} as a result of using an invalid * {@link UnsupportedEncodingException} as a result of using an invalid charset
* charset in a {@link RuntimeException}. * in a {@link RuntimeException}.
* *
* @param bytes * @param bytes
* @return * @return
* @throws RuntimeException * @throws RuntimeException
* If the charset was invalid, or some otehr unknown error * If the charset was invalid, or some otehr unknown error occurred
* occurred
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If the byte array was not specified * If the byte array was not specified
*/ */
private String byteArrayToString(final byte[] bytes) private String byteArrayToString(final byte[] bytes) throws RuntimeException, IllegalArgumentException {
throws RuntimeException, IllegalArgumentException {
// Precondition check // Precondition check
if (bytes == null) { if (bytes == null) {
throw new IllegalArgumentException("Byte array is required."); throw new IllegalArgumentException("Byte array is required.");
...@@ -513,8 +531,7 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -513,8 +531,7 @@ public class EncryptionBean implements EncryptionLocalBusiness,
try { try {
result = new String(bytes, charset); result = new String(bytes, charset);
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
throw new RuntimeException("Specified charset is invalid: " throw new RuntimeException("Specified charset is invalid: " + charset, e);
+ charset, e);
} }
// Return // Return
...@@ -522,21 +539,19 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -522,21 +539,19 @@ public class EncryptionBean implements EncryptionLocalBusiness,
} }
/** /**
* Returns a byte array representation of the specified String using the * Returns a byte array representation of the specified String using the charset
* charset from {@link EncryptionBeanBase#getCharset()}. Wraps any * from {@link EncryptionBeanBase#getCharset()}. Wraps any
* {@link UnsupportedEncodingException} as a result of using an invalid * {@link UnsupportedEncodingException} as a result of using an invalid charset
* charset in a {@link RuntimeException}. * in a {@link RuntimeException}.
* *
* @param input * @param input
* @return * @return
* @throws RuntimeException * @throws RuntimeException
* If the charset was invalid, or some otehr unknown error * If the charset was invalid, or some otehr unknown error occurred
* occurred
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If the input was not specified (null) * If the input was not specified (null)
*/ */
private byte[] stringToByteArray(final String input) private byte[] stringToByteArray(final String input) throws RuntimeException, IllegalArgumentException {
throws RuntimeException, IllegalArgumentException {
// Precondition check // Precondition check
if (input == null) { if (input == null) {
throw new IllegalArgumentException("Input is required."); throw new IllegalArgumentException("Input is required.");
...@@ -548,8 +563,7 @@ public class EncryptionBean implements EncryptionLocalBusiness, ...@@ -548,8 +563,7 @@ public class EncryptionBean implements EncryptionLocalBusiness,
try { try {
result = input.getBytes(charset); result = input.getBytes(charset);
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
throw new RuntimeException("Specified charset is invalid: " throw new RuntimeException("Specified charset is invalid: " + charset, e);
+ charset, e);
} }
// Return // Return
......
/** /**
* *
*/ */
package br.ufrn.imd.imd0409.encryption; package br.ufrn.imd.web2.encryption;
import java.util.concurrent.Future; import java.util.concurrent.Future;
...@@ -10,7 +10,7 @@ import java.util.concurrent.Future; ...@@ -10,7 +10,7 @@ import java.util.concurrent.Future;
* EncryptionEJB * EncryptionEJB
* *
*/ */
public interface EncryptionCommonBusiness { public interface EncryptionCommon {
/** /**
* Encrypts the specified String, returning the result * Encrypts the specified String, returning the result
...@@ -28,7 +28,7 @@ public interface EncryptionCommonBusiness { ...@@ -28,7 +28,7 @@ public interface EncryptionCommonBusiness {
/** /**
* Decrypts the specified String, returning the result. The general contract * Decrypts the specified String, returning the result. The general contract
* is that the result of decrypting a String encrypted with * is that the result of decrypting a String encrypted with
* {@link EncryptionCommonBusiness#encrypt(String)} will be equal by value * {@link EncryptionCommon#encrypt(String)} will be equal by value
* to the original input (round trip). * to the original input (round trip).
* *
* @param input * @param input
......
package br.ufrn.imd.imd0409.encryption; package br.ufrn.imd.web2.encryption;
public class EncryptionException extends Exception { public class EncryptionException extends Exception {
......
package br.ufrn.imd.web2.encryption;
import javax.ejb.Local;
@Local
public interface EncryptionLocal extends EncryptionCommon {
}
package br.ufrn.imd.web2.encryption;
import javax.ejb.Remote;
@Remote
public interface EncryptionRemote extends EncryptionCommon {
}