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

92 lines
2.5 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.math.BigInteger;
import java.security.MessageDigest;
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
import org.dom4j.Element;
2010-05-29 20:47:04 +02:00
import ch.eitchnet.privilege.base.XmlConstants;
2010-05-25 23:15:35 +02:00
import ch.eitchnet.privilege.helper.ConfigurationHelper;
2010-05-25 21:41:34 +02:00
import ch.eitchnet.privilege.i18n.PrivilegeException;
/**
* @author rvonburg
*
*/
public class DefaultEncryptionHandler implements EncryptionHandler {
2010-05-25 23:15:35 +02:00
public String hashAlgorithm;
2010-05-25 21:41:34 +02:00
/**
* Hex char table for fast calculating of hex value
*/
private static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4',
(byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
(byte) 'e', (byte) 'f' };
/**
* @see ch.eitchnet.privilege.handler.EncryptionHandler#convertToHash(java.lang.String)
*/
@Override
public String convertToHash(String string) {
try {
2010-05-25 23:15:35 +02:00
MessageDigest digest = MessageDigest.getInstance(hashAlgorithm);
2010-05-25 21:41:34 +02:00
byte[] hashArray = digest.digest(string.getBytes());
byte[] hex = new byte[2 * hashArray.length];
int index = 0;
for (byte b : hashArray) {
int v = b & 0xFF;
hex[index++] = HEX_CHAR_TABLE[v >>> 4];
hex[index++] = HEX_CHAR_TABLE[v & 0xF];
}
return new String(hex, "ASCII");
} 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() {
SecureRandom secureRandom = new SecureRandom();
String randomString = new BigInteger(130, secureRandom).toString(32);
return randomString;
}
2010-05-25 23:15:35 +02:00
/**
* @see ch.eitchnet.privilege.base.PrivilegeContainerObject#initialize(org.dom4j.Element)
*/
2010-05-25 21:41:34 +02:00
public void initialize(Element element) {
2010-05-25 23:15:35 +02:00
2010-05-29 20:47:04 +02:00
Element parameterElement = element.element(XmlConstants.XML_PARAMETERS);
2010-05-25 23:15:35 +02:00
Map<String, String> parameterMap = ConfigurationHelper.convertToParameterMap(parameterElement);
// get and test configured algorithm
2010-05-29 20:47:04 +02:00
hashAlgorithm = parameterMap.get(XmlConstants.XML_PARAM_HASH_ALGORITHM);
2010-05-25 23:15:35 +02:00
convertToHash("test");
2010-05-25 21:41:34 +02:00
}
}