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"?>
<classpath>
<classpathentry kind="src" path="ejbModule"/>
<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">
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="owner.project.facets" value="jst.ejb;#system#"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</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>
<attribute name="owner.project.facets" value="java"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</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>
/target/
......@@ -15,16 +15,40 @@
<arguments>
</arguments>
</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>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<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.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</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>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=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">
<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="ClientProject" value="Encryption-BeanClient"/>
<property name="ClientJARURI" value="Encryption-BeanClient.jar"/>
......
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="WildFly 9.x Runtime"/>
<runtime name="WildFly 14.0 Runtime"/>
<fixed facet="jst.ejb"/>
<fixed facet="java"/>
<installed facet="java" version="1.7"/>
<installed facet="jst.ejb" version="3.1"/>
<installed facet="java" version="1.8"/>
<installed facet="jst.ejb" version="3.2"/>
</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.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.concurrent.Future;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
......@@ -23,26 +28,29 @@ import javax.ejb.Stateless;
import org.apache.commons.codec.binary.Base64;
import br.ufrn.imd.imd0409.encryption.EncryptionException;
import br.ufrn.imd.imd0409.encryption.EncryptionLocalBusiness;
import br.ufrn.imd.imd0409.encryption.EncryptionRemoteBusiness;
import br.ufrn.imd.web2.encryption.EncryptionException;
import br.ufrn.imd.web2.encryption.EncryptionLocal;
import br.ufrn.imd.web2.encryption.EncryptionRemote;
@Stateless(name = EncryptionBean.EJB_NAME)
@Local(EncryptionLocalBusiness.class)
@Remote(EncryptionRemoteBusiness.class)
public class EncryptionBean implements EncryptionLocalBusiness,
EncryptionRemoteBusiness {
@Local(EncryptionLocal.class)
@Remote(EncryptionRemote.class)
public class EncryptionBean implements EncryptionLocal, EncryptionRemote {
/**
* Logger
*/
private static final Logger log = Logger.getLogger(EncryptionBean.class.getName());
/**
* Name we'll assign to this EJB, will be referenced in the corresponding
* 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
* supplied in ejb-jar.xml
* Name of the environment entry representing the ciphers' passphrase supplied
* in ejb-jar.xml
*/
private static final String ENV_ENTRY_NAME_CIPHERS_PASSPHRASE = "ciphersPassphrase";
......@@ -75,9 +83,8 @@ public class EncryptionBean implements EncryptionLocalBusiness,
/**
* The salt used in symmetric encryption/decryption
*/
private static final byte[] DEFAULT_SALT_CIPHERS = { (byte) 0xB4,
(byte) 0xA2, (byte) 0x43, (byte) 0x89, 0x3E, (byte) 0xC5,
(byte) 0x78, (byte) 0x53 };
private static final byte[] DEFAULT_SALT_CIPHERS = { (byte) 0xB4, (byte) 0xA2, (byte) 0x43, (byte) 0x89, 0x3E,
(byte) 0xC5, (byte) 0x78, (byte) 0x53 };
/**
* Iteration count used for symmetric encryption/decryption
......@@ -90,9 +97,9 @@ public class EncryptionBean implements EncryptionLocalBusiness,
// ---------------------------------------------------------------------------||
/*
* The following members represent the internal state of the Service. Note
* how these are *not* leaked out via the end-user API, and are hence part
* of "internal state" and not "conversational state".
* The following members represent the internal state of the Service. Note how
* these are *not* leaked out via the end-user API, and are hence part of
* "internal state" and not "conversational state".
*/
/**
......@@ -103,8 +110,8 @@ public class EncryptionBean implements EncryptionLocalBusiness,
private SessionContext context;
/**
* Passphrase to use for the key in cipher operations; lazily initialized
* and loaded via SessionContext.lookup
* Passphrase to use for the key in cipher operations; lazily initialized and
* loaded via SessionContext.lookup
*/
private String ciphersPassphrase;
......@@ -138,14 +145,13 @@ public class EncryptionBean implements EncryptionLocalBusiness,
/**
* Initializes this service before it may handle requests
*
* @throws Exception
* @throws RuntimeException
* If some unexpected error occurred
*/
@PostConstruct
public void initialize() throws Exception {
public void initialize() {
// Log that we're here
System.out.println("Initializing, part of " + PostConstruct.class.getName()
+ " lifecycle");
log.info("Initializing, part of " + PostConstruct.class.getName() + " lifecycle");
/*
* Symmetric Encryption
......@@ -158,23 +164,55 @@ public class EncryptionBean implements EncryptionLocalBusiness,
final String ciphersPassphrase = this.getCiphersPassphrase();
// Obtain key and param spec for the ciphers
final KeySpec ciphersKeySpec = new PBEKeySpec(
ciphersPassphrase.toCharArray(), ciphersSalt,
final KeySpec ciphersKeySpec = new PBEKeySpec(ciphersPassphrase.toCharArray(), ciphersSalt,
ciphersIterationCount);
final SecretKey ciphersKey = SecretKeyFactory.getInstance(
cipherAlgorithm).generateSecret(ciphersKeySpec);
final AlgorithmParameterSpec paramSpec = new PBEParameterSpec(
ciphersSalt, ciphersIterationCount);
SecretKey ciphersKey;
try {
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
this.encryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm());
this.decryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm());
encryptionCipher.init(Cipher.ENCRYPT_MODE, ciphersKey, paramSpec);
decryptionCipher.init(Cipher.DECRYPT_MODE, ciphersKey, paramSpec);
try {
this.encryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm());
this.decryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm());
} 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
System.out.println("Initialized encryption cipher: " + this.encryptionCipher);
System.out.println("Initialized decryption cipher: " + this.decryptionCipher);
log.info("Initialized encryption cipher: " + this.encryptionCipher);
log.info("Initialized decryption cipher: " + this.decryptionCipher);
/*
* One-way Hashing
......@@ -185,15 +223,12 @@ public class EncryptionBean implements EncryptionLocalBusiness,
// Create the MessageDigest
try {
this.messageDigest = MessageDigest
.getInstance(messageDigestAlgorithm);
this.messageDigest = MessageDigest.getInstance(messageDigestAlgorithm);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Could not obtain the "
+ MessageDigest.class.getSimpleName() + " for algorithm: "
+ messageDigestAlgorithm, e);
throw new RuntimeException("Could not obtain the " + MessageDigest.class.getSimpleName()
+ " for algorithm: " + messageDigestAlgorithm, e);
}
System.out.println("Initialized MessageDigest for one-way hashing: "
+ this.messageDigest);
log.info("Initialized MessageDigest for one-way hashing: " + this.messageDigest);
}
// ---------------------------------------------------------------------------||
......@@ -204,12 +239,11 @@ public class EncryptionBean implements EncryptionLocalBusiness,
/**
* {@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)
*/
@Override
public boolean compare(final String hash, final String input)
throws IllegalArgumentException, EncryptionException {
public boolean compare(final String hash, final String input) throws IllegalArgumentException, EncryptionException {
// Precondition checks
if (hash == null) {
throw new IllegalArgumentException("hash is required.");
......@@ -231,16 +265,15 @@ public class EncryptionBean implements EncryptionLocalBusiness,
/**
* {@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
public String decrypt(final String input) throws IllegalArgumentException,
IllegalStateException, EncryptionException {
public String decrypt(final String input)
throws IllegalArgumentException, IllegalStateException, EncryptionException {
// Get the cipher
final Cipher cipher = this.decryptionCipher;
if (cipher == null) {
throw new IllegalStateException(
"Decyrption cipher not available, has this service been initialized?");
throw new IllegalStateException("Decyrption cipher not available, has this service been initialized?");
}
// Run the cipher
......@@ -255,7 +288,7 @@ public class EncryptionBean implements EncryptionLocalBusiness,
final String result = this.byteArrayToString(resultBytes);
// Log
System.out.println("Decryption on \"" + input + "\": " + result);
log.info("Decryption on \"" + input + "\": " + result);
// Return
return result;
......@@ -264,16 +297,14 @@ public class EncryptionBean implements EncryptionLocalBusiness,
/**
* {@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
public String encrypt(final String input) throws IllegalArgumentException,
EncryptionException {
public String encrypt(final String input) throws IllegalArgumentException, EncryptionException {
// Get the cipher
final Cipher cipher = this.encryptionCipher;
if (cipher == null) {
throw new IllegalStateException(
"Encyrption cipher not available, has this service been initialized?");
throw new IllegalStateException("Encyrption cipher not available, has this service been initialized?");
}
// Get bytes from the String
......@@ -288,8 +319,7 @@ public class EncryptionBean implements EncryptionLocalBusiness,
}
// Log
System.out.println("Encryption on \"" + input + "\": "
+ this.byteArrayToString(resultBytes));
log.info("Encryption on \"" + input + "\": " + this.byteArrayToString(resultBytes));
// Return
final String result = this.byteArrayToString(resultBytes);
......@@ -302,19 +332,17 @@ public class EncryptionBean implements EncryptionLocalBusiness,
* This is a weak implementation, but is enough to satisfy the example. If
* considering real-world stresses, we would be, at a minimum:
*
* 1) Incorporating a random salt and storing it alongside the hashed result
* 2) Additionally implementing an iteration count to re-hash N times
* 1) Incorporating a random salt and storing it alongside the hashed result 2)
* Additionally implementing an iteration count to re-hash N times
*/
/*
* (non-Javadoc)
*
* @see
* org.jboss.ejb3.examples.ch05.encryption.EncryptionCommonBusiness#hash
* @see org.jboss.ejb3.examples.ch05.encryption.EncryptionCommonBusiness#hash
* (java.lang.String)
*/
@Override
public String hash(final String input) throws IllegalArgumentException,
EncryptionException {
public String hash(final String input) throws IllegalArgumentException, EncryptionException {
// Precondition check
if (input == null) {
throw new IllegalArgumentException("Input is required.");
......@@ -334,7 +362,7 @@ public class EncryptionBean implements EncryptionLocalBusiness,
// Get the input back in some readable format
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 hash;
......@@ -343,12 +371,11 @@ public class EncryptionBean implements EncryptionLocalBusiness,
/**
* {@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
@Override
public Future<String> hashAsync(final String input)
throws IllegalArgumentException, EncryptionException {
public Future<String> hashAsync(final String input) throws IllegalArgumentException, EncryptionException {
// Get the real hash
final String hash = this.hash(input);
......@@ -357,16 +384,16 @@ public class EncryptionBean implements EncryptionLocalBusiness,
}
/**
* Override the way we get the ciphers' passphrase so that we may define it
* in 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
* the likelihood of a security breach while still allowing our programmer
* to use the default passphrase transparently during development.
* Override the way we get the ciphers' passphrase so that we may define it in 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 the
* likelihood of a security breach while still allowing our programmer to use
* the default passphrase transparently during development.
*
* 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.
* We do here for testing and to illustrate the example.
* Note that a real system won't expose this method in the public API, ever. We
* do here for testing and to illustrate the example.
*
* @see org.jboss.ejb3.examples.ch05.encryption.EncryptionBeanBase#getCiphersPassphrase()
*/
......@@ -379,8 +406,7 @@ public class EncryptionBean implements EncryptionLocalBusiness,
if (passphrase == null) {
// Do a lookup via SessionContext
passphrase = this
.getEnvironmentEntryAsString(ENV_ENTRY_NAME_CIPHERS_PASSPHRASE);
passphrase = this.getEnvironmentEntryAsString(ENV_ENTRY_NAME_CIPHERS_PASSPHRASE);
// See if provided
if (passphrase == null) {
......@@ -399,25 +425,25 @@ public class EncryptionBean implements EncryptionLocalBusiness,
}
// 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 passphrase;
}
/**
* Obtains the message digest algorithm as injected from the env-entry
* element defined in ejb-jar.xml. If not specified, fall back onto the
* default, logging a warn message
* Obtains the message digest algorithm as injected from the env-entry element
* defined in ejb-jar.xml. If not specified, fall back onto the default, logging
* a warn message
*
* @see org.jboss.ejb3.examples.ch05.encryption.EncryptionRemoteBusiness#getMessageDigestAlgorithm()
* @see org.jboss.ejb3.EncryptionRemote.ch05.encryption.EncryptionRemoteBusiness#getMessageDigestAlgorithm()
*/
@Override
public String getMessageDigestAlgorithm() {
// First see if this has been injected/set
if (this.messageDigestAlgorithm == null) {
// 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...");
// Set
......@@ -425,8 +451,7 @@ public class EncryptionBean implements EncryptionLocalBusiness,
}
// Log
System.out.println("Configured MessageDigest one-way hash algorithm is: "
+ this.messageDigestAlgorithm);
log.info("Configured MessageDigest one-way hash algorithm is: " + this.messageDigestAlgorithm);
// Return
return this.messageDigestAlgorithm;
......@@ -438,23 +463,21 @@ public class EncryptionBean implements EncryptionLocalBusiness,
// ---------------------------------------------------------------------------||
/**
* Obtains the environment entry with the specified name, casting to a
* String, and returning the result. If the entry is not assignable to a
* String, an {@link IllegalStateException} will be raised. In the event
* that the specified environment entry cannot be found, a warning message
* will be logged and we'll return null.
* Obtains the environment entry with the specified name, casting to a String,
* and returning the result. If the entry is not assignable to a String, an
* {@link IllegalStateException} will be raised. In the event that the specified
* environment entry cannot be found, a warning message will be logged and we'll
* return null.
*
* @param envEntryName
* @return
* @throws IllegalStateException
*/
private String getEnvironmentEntryAsString(final String envEntryName)
throws IllegalStateException {
private String getEnvironmentEntryAsString(final String envEntryName) throws IllegalStateException {
// See if we have a SessionContext
final SessionContext context = this.context;
if (context == null) {
System.out.println("No SessionContext, bypassing request to obtain environment entry: "
+ envEntryName);
log.warning("No SessionContext, bypassing request to obtain environment entry: " + envEntryName);
return null;
}
......@@ -462,13 +485,11 @@ public class EncryptionBean implements EncryptionLocalBusiness,
Object lookupValue = null;
try {
lookupValue = context.lookup(envEntryName);
System.out.println("Obtained environment entry \"" + envEntryName + "\": "
+ lookupValue);
log.fine("Obtained environment entry \"" + envEntryName + "\": " + lookupValue);
} catch (final IllegalArgumentException iae) {
// Not found defined within this EJB's Component Environment,
// so return null and let the caller handle it
System.out.println("Could not find environment entry with name: "
+ envEntryName);
log.warning("Could not find environment entry with name: " + envEntryName);
return null;
}
......@@ -477,9 +498,8 @@ public class EncryptionBean implements EncryptionLocalBusiness,
try {
returnValue = String.class.cast(lookupValue);
} catch (final ClassCastException cce) {
throw new IllegalStateException("The specified environment entry, "
+ lookupValue + ", was not able to be represented as a "
+ String.class.getName(), cce);
throw new IllegalStateException("The specified environment entry, " + lookupValue
+ ", was not able to be represented as a " + String.class.getName(), cce);
}
// Return
......@@ -487,21 +507,19 @@ public class EncryptionBean implements EncryptionLocalBusiness,
}
/**
* Returns a String representation of the specified byte array using the
* charset from {@link EncryptionBeanBase#getCharset()}. Wraps any
* {@link UnsupportedEncodingException} as a result of using an invalid
* charset in a {@link RuntimeException}.
* Returns a String representation of the specified byte array using the charset
* from {@link EncryptionBeanBase#getCharset()}. Wraps any
* {@link UnsupportedEncodingException} as a result of using an invalid charset
* in a {@link RuntimeException}.
*
* @param bytes
* @return
* @throws RuntimeException
* If the charset was invalid, or some otehr unknown error
* occurred
* If the charset was invalid, or some otehr unknown error occurred
* @throws IllegalArgumentException
* If the byte array was not specified
*/
private String byteArrayToString(final byte[] bytes)
throws RuntimeException, IllegalArgumentException {
private String byteArrayToString(final byte[] bytes) throws RuntimeException, IllegalArgumentException {
// Precondition check
if (bytes == null) {
throw new IllegalArgumentException("Byte array is required.");
......@@ -513,8 +531,7 @@ public class EncryptionBean implements EncryptionLocalBusiness,
try {
result = new String(bytes, charset);
} catch (final UnsupportedEncodingException e) {
throw new RuntimeException("Specified charset is invalid: "
+ charset, e);
throw new RuntimeException("Specified charset is invalid: " + charset, e);
}
// Return
......@@ -522,21 +539,19 @@ public class EncryptionBean implements EncryptionLocalBusiness,
}
/**
* Returns a byte array representation of the specified String using the
* charset from {@link EncryptionBeanBase#getCharset()}. Wraps any
* {@link UnsupportedEncodingException} as a result of using an invalid
* charset in a {@link RuntimeException}.
* Returns a byte array representation of the specified String using the charset
* from {@link EncryptionBeanBase#getCharset()}. Wraps any
* {@link UnsupportedEncodingException} as a result of using an invalid charset
* in a {@link RuntimeException}.
*
* @param input
* @return
* @throws RuntimeException
* If the charset was invalid, or some otehr unknown error
* occurred
* If the charset was invalid, or some otehr unknown error occurred
* @throws IllegalArgumentException
* If the input was not specified (null)
*/
private byte[] stringToByteArray(final String input)
throws RuntimeException, IllegalArgumentException {
private byte[] stringToByteArray(final String input) throws RuntimeException, IllegalArgumentException {
// Precondition check
if (input == null) {
throw new IllegalArgumentException("Input is required.");
......@@ -548,8 +563,7 @@ public class EncryptionBean implements EncryptionLocalBusiness,
try {
result = input.getBytes(charset);
} catch (final UnsupportedEncodingException e) {
throw new RuntimeException("Specified charset is invalid: "
+ charset, e);
throw new RuntimeException("Specified charset is invalid: " + charset, e);
}
// Return
......
/**
*
*/
package br.ufrn.imd.imd0409.encryption;
package br.ufrn.imd.web2.encryption;
import java.util.concurrent.Future;
......@@ -10,7 +10,7 @@ import java.util.concurrent.Future;
* EncryptionEJB
*
*/
public interface EncryptionCommonBusiness {
public interface EncryptionCommon {
/**
* Encrypts the specified String, returning the result
......@@ -28,7 +28,7 @@ public interface EncryptionCommonBusiness {
/**
* Decrypts the specified String, returning the result. The general contract
* 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).
*
* @param input
......
package br.ufrn.imd.imd0409.encryption;
package br.ufrn.imd.web2.encryption;
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 {
}