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

88 lines
2.4 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;
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;
/**
* @author rvonburg
*
*/
public class DefaultEncryptionHandler implements EncryptionHandler {
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-05-31 23:44:15 +02:00
private SecureRandom secureRandom;
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-05-31 23:44:15 +02:00
return EncryptionHelper.encryptString(hashAlgorithm, string);
2010-05-25 21:41:34 +02:00
} catch (NoSuchAlgorithmException e) {
2010-05-25 23:15:35 +02:00
throw new PrivilegeException("Algorithm " + 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];
secureRandom.nextBytes(bytes);
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-05-31 23:44:15 +02:00
secureRandom = new SecureRandom();
2010-05-29 21:11:28 +02:00
// get hash algorithm parameters
2010-05-29 20:47:04 +02:00
hashAlgorithm = parameterMap.get(XmlConstants.XML_PARAM_HASH_ALGORITHM);
2010-05-29 21:11:28 +02:00
if (hashAlgorithm == null || hashAlgorithm.isEmpty()) {
throw new PrivilegeException("[" + EncryptionHandler.class.getName() + "] Defined parameter "
+ XmlConstants.XML_PARAM_HASH_ALGORITHM + " is invalid");
}
// test hash algorithm
try {
convertToHash("test");
logger.info("Using hashing algorithm " + hashAlgorithm);
} 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
}
}