Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  package rs.baselib.crypto;
  
  import java.net.URL;
 import java.util.Map;
 
 
Creates a crypting delegator. This class finds its configuration via classpath. You can give java argument -Dencryption.config=path which will then load the config from this path. The default location is encryption-config.xml.

Author(s):
ralph
 
 
 	private static Logger log = LoggerFactory.getLogger(DefaultCryptingDelegateFactory.class);
 
 
 	private ICryptingDelegate cryptingDelegate = null;
 	private KeyPair keyPair = null;
 	private String algorithm;
 	private boolean specLoaded = false;

Returns the crypting delegate factory.

Returns:
the factory
 
 	public static ICryptingDelegateFactory getInstance() {
 		return ;
 	}

Constructor.
 
 		init();
 	}

Initializes this factory.
 
 	protected void init() {
 		try {
 			// Some init
 
 			// Load the configuration
 			String configLocation = System.getProperty("encryption.config");
 			if (configLocation == nullconfigLocation = "encryption-config.xml";
 			URL configURL = FileFinder.find(configLocation);
 			.info("Encryption configuration defined as: "+configLocation);
 			.info("Encryption configuration found at: "+configURL);
 			 = new XMLConfiguration(configURL);
 			
 			// Attention! Do not load here due to bootstrap issues
 			// Apply lazy load through loadSpec() method
 		} catch (Exception e) {
 			throw new RuntimeException("Cannot load keys"e);
 		}
 	};

Loads the spec params (lazily).
 
 	protected synchronized void loadSpec() {
 		if (!) {
 			if ( == null = loadAlgorithm();
 			if ( == null = loadParamSpec();
 			 = true;
 		}
 	}

Returns the configuration.

Returns:
the configuration
		return ;
	}

Returns the name of the delegate class.

Returns:
the name of delegate class
		String className = .getString("cryptingDelegate(0)[@class]");
		if (className == nullclassName = DefaultCryptingDelegate.class.getName();
		return className;
	}

Returns a callback for the given type.

Parameters:
type type of password callback
Returns:
the password callback
	protected synchronized IPasswordCallback getPasswordCallback(String type) {
		String className = config.getString("[@class]");
		if (className != null) {
			if (rc == null) {
				rc = (IPasswordCallback)ConfigurationUtils.load(configtrue);
				.put(classNamerc);
			}
			return rc;
		}
		return null;
	}

Returns the configuration for the password callback.

Parameters:
type type of callback
Returns:
classname
		int index = 0;
		while (true) {
			try {
				rc = .configurationAt("passwordCallback("+index+")");
catch (IllegalArgumentException e) {
				break;
			}
			if (rc == nullbreak;
			String t = rc.getString("[@type]");
			if (type.equals(t)) return rc;
			index++;
		}
		return null;
	}

Asks the respective callback to deliver a password.

Parameters:
type type of callback
Returns:
password or null if no callback exists.
	protected char[] getPassword(String type) {
		char rc[] = null;
		if (callback == nullrc = null;
		else rc = callback.getPassword();
		return rc;
	}

Return the public key salt.

Returns:
key salt
	protected byte[] getKeySalt() {
		return getSalt("key");
	}


	public byte[] getSalt() {
		return getKeySalt();
	}

Asks the respective callback to deliver a salt.

Parameters:
type type of callback
Returns:
salt or null if no callback exists.
	protected byte[] getSalt(String type) {
		byte rc[] = null;
		if (callback == nullrc = null;
		else rc = callback.getSalt();
		return rc;
	}

Returns the configuration for the keystore.

Returns:
classname
		try {
			return .configurationAt("keystore(0)");
catch (IllegalArgumentException t) {
			// No such config
		}
		return null;
	}

Returns the key store.

Returns:
the key store
	protected KeyStore getKeyStore() throws IOException {
		if (path != null) {
			.info("Keystore defined as: "+path);
			URL url = FileFinder.find(path);
			.info("Keystore found at: "+url);
			return EncryptionUtils.getKeyStore(getKeyStoreType(), url.openStream(), getKeyStorePassword());
		}
		return null;
	}

Returns the key store path.

Returns:
key store path
	protected String getKeyStoreType() {
		String rc = ConfigurationUtils.getParam(getKeyStoreConfig(), "type");
		if (rc == nullrc = KeyStore.getDefaultType();
		return rc;
	}

Returns the key store path.

Returns:
key store path
	protected String getKeyStorePath() {
		return ConfigurationUtils.getParam(getKeyStoreConfig(), "location");
	}

Return the key store password

Returns:
key store password
	protected char[] getKeyStorePassword() {
		return getPassword("keystore");
	}

Returns the public key alias in key store.

Returns:
key alias
	protected String getKeyAlias() {
		return ConfigurationUtils.getParam(getKeyStoreConfig(), "key.alias");
	}

Return the public key password

Returns:
key password
	protected char[] getKeyPassword() {
		return getPassword("key");
	}

	public char[] getPassphrase() {
		return getKeyPassword();
	}

	}

Creates and initializes the crypting delegate.
	@SuppressWarnings({ "unchecked" })
	protected synchronized void createCryptingDelegate() {
		if ( != nullreturn;
		try {
			Class<? extends ICryptingDelegateclazz = (Class<? extends ICryptingDelegate>)LangUtils.forName(getDelegateClassName());
catch (Exception e) {
			throw new RuntimeException("Cannot create crypting delegate"e);
		}
	}

Returns the keyPair.

Returns:
the keyPair
	public KeyPair getKeyPair() {
		if ( == null) {
			try {
				// Load the key pair from the key store
				String keyAlias = getKeyAlias();
				char keyPassword[] = getKeyPassword();
				if ((keyAlias != null) && (keyPassword != null)) {
					setKeyPair(new KeyPair(pubKeyprivKey));
				}
catch (Exception e) {
				throw new RuntimeException("Cannot create key pair:"e);
			}
		}
		return ;
	}

Sets the keyPair.

Parameters:
keyPair the keyPair to set
	public void setKeyPair(KeyPair keyPair) {
		this. = keyPair;
	}

Returns the algorithm.

Returns:
the algorithm
	public String getAlgorithm() {
		if (!)  loadSpec();
		return ;
	}

Sets the algorithm.

Parameters:
algorithm the algorithm to set
	public void setAlgorithm(String algorithm) {
		this. = algorithm;
	}

Returns the algorithm definition from the config

Parameters:
config the config
Returns:
the algorithm
	protected String loadAlgorithm(Configuration config) {
		try {
			return config.getString("algorithm("+0+")");
catch (IllegalArgumentException e) {
			// No such definition
		}
		return null;
	}

Returns the paramSpec.

Returns:
the paramSpec
		if (!)  loadSpec();
		return ;
	}

Sets the paramSpec.

Parameters:
paramSpec the paramSpec to set
	public void setParamSpec(AlgorithmParameterSpec paramSpec) {
		this. = paramSpec;
	}

Currently only default PBE spec.

Parameters:
config configuration to load from
Returns:
the param spec
	}
New to GrepCode? Check out our FAQ X