strolch/src/ch/eitchnet/privilege/handler/DefaultEncryptionHandler.java

110 lines
3.0 KiB
Java
Raw Normal View History

2010-05-25 21:41:34 +02:00
/*
* Copyright (c) 2010
*
* Robert von Burg
* eitch@eitchnet.ch
*
* All rights reserved.
*
*/
package ch.eitchnet.privilege.handler;
import java.io.UnsupportedEncodingException;
2010-09-19 22:19:38 +02:00
import java.security.MessageDigest;
2010-05-25 21:41:34 +02:00
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
2010-05-25 23:15:35 +02:00
import java.util.Map;
2010-05-25 21:41:34 +02:00
2010-05-29 21:11:28 +02:00
import org.apache.log4j.Logger;
2010-05-25 21:41:34 +02:00
2010-05-31 23:44:15 +02:00
import ch.eitchnet.privilege.helper.EncryptionHelper;
2010-09-18 22:00:20 +02:00
import ch.eitchnet.privilege.helper.XmlConstants;
2010-05-25 21:41:34 +02:00
import ch.eitchnet.privilege.i18n.PrivilegeException;
/**
2010-09-19 22:19:38 +02:00
* <p>
* This default {@link EncryptionHandler} creates nokens by using a {@link SecureRandom} object. Hashing is done by
* using {@link MessageDigest} and the configured algorithm which is passed in the parameters
* </p>
*
* Required parameters:
* <ul>
* <li> {@link XmlConstants#XML_PARAM_HASH_ALGORITHM}</li>
* </ul>
*
2010-05-25 21:41:34 +02:00
* @author rvonburg
*
*/
public class DefaultEncryptionHandler implements EncryptionHandler {
2010-09-19 22:19:38 +02:00
/**
* The log4j logger used in this instance
*/
2010-05-29 21:11:28 +02:00
private static final Logger logger = Logger.getLogger(DefaultEncryptionHandler.class);
2010-05-25 21:41:34 +02:00
2010-09-19 22:19:38 +02:00
/**
* The {@link SecureRandom} which is used to create new tokens
*/
2010-05-31 23:44:15 +02:00
private SecureRandom secureRandom;
2010-09-19 22:19:38 +02:00
/**
* The configured hash algorithm for this instance
*/
2010-05-31 23:44:15 +02:00
private String hashAlgorithm;
2010-05-25 21:41:34 +02:00
/**
* @see ch.eitchnet.privilege.handler.EncryptionHandler#convertToHash(java.lang.String)
*/
@Override
public String convertToHash(String string) {
try {
2010-09-19 20:57:23 +02:00
return EncryptionHelper.encryptString(this.hashAlgorithm, string);
2010-05-25 21:41:34 +02:00
} catch (NoSuchAlgorithmException e) {
2010-09-19 20:57:23 +02:00
throw new PrivilegeException("Algorithm " + this.hashAlgorithm + " was not found!", e);
2010-05-25 21:41:34 +02:00
} catch (UnsupportedEncodingException e) {
throw new PrivilegeException("Charset ASCII is not supported!", e);
}
}
/**
* @see ch.eitchnet.privilege.handler.EncryptionHandler#nextToken()
*/
@Override
public String nextToken() {
2010-05-31 23:44:15 +02:00
byte[] bytes = new byte[16];
2010-09-19 20:57:23 +02:00
this.secureRandom.nextBytes(bytes);
2010-05-31 23:44:15 +02:00
String randomString = new String(bytes);
//String randomString = new BigInteger(80, secureRandom).toString(32); // 80 big integer bits = 16 chars
2010-05-25 21:41:34 +02:00
return randomString;
}
2010-05-25 23:15:35 +02:00
/**
2010-09-18 22:00:20 +02:00
* @see ch.eitchnet.privilege.handler.EncryptionHandler#initialize(java.util.Map)
2010-05-25 23:15:35 +02:00
*/
2010-09-18 22:00:20 +02:00
@Override
public void initialize(Map<String, String> parameterMap) {
2010-05-25 23:15:35 +02:00
2010-09-19 20:57:23 +02:00
this.secureRandom = new SecureRandom();
2010-05-31 23:44:15 +02:00
2010-05-29 21:11:28 +02:00
// get hash algorithm parameters
2010-09-19 20:57:23 +02:00
this.hashAlgorithm = parameterMap.get(XmlConstants.XML_PARAM_HASH_ALGORITHM);
if (this.hashAlgorithm == null || this.hashAlgorithm.isEmpty()) {
2010-05-29 21:11:28 +02:00
throw new PrivilegeException("[" + EncryptionHandler.class.getName() + "] Defined parameter "
+ XmlConstants.XML_PARAM_HASH_ALGORITHM + " is invalid");
}
// test hash algorithm
try {
convertToHash("test");
2010-09-19 20:57:23 +02:00
logger.info("Using hashing algorithm " + this.hashAlgorithm);
2010-05-29 21:11:28 +02:00
} catch (Exception e) {
throw new PrivilegeException("[" + EncryptionHandler.class.getName() + "] Defined parameter "
+ XmlConstants.XML_PARAM_HASH_ALGORITHM + " is invalid because of underlying exception: "
+ e.getLocalizedMessage(), e);
}
2010-05-25 21:41:34 +02:00
}
}