use of org.apache.openejb.cipher.PasswordCipher in project tomee by apache.
the class Cipher method main.
public static void main(final String[] args) throws SystemExitException {
final CommandLineParser parser = new PosixParser();
// create the Options
final Options options = new Options();
options.addOption(option("h", "help", "cmd.cipher.opt.help"));
options.addOption(option("c", "cipher", "c", "cmd.cipher.opt.impl"));
options.addOption(option("d", "decrypt", "cmd.cipher.opt.decrypt"));
options.addOption(option("e", "encrypt", "cmd.cipher.opt.encrypt"));
final CommandLine line;
try {
// parse the command line arguments
line = parser.parse(options, args);
} catch (final ParseException exp) {
help(options);
throw new SystemExitException(-1);
}
if (line.hasOption("help")) {
help(options);
return;
}
String cipherName = "Static3DES";
if (line.hasOption("cipher")) {
cipherName = line.getOptionValue("cipher");
}
if (line.getArgList().size() != 1) {
System.out.println("Must specify either a plain text to encrypt or a ciphered value to decrypt.");
help(options);
return;
}
final PasswordCipher cipher;
try {
cipher = PasswordCipherFactory.getPasswordCipher(cipherName);
} catch (final PasswordCipherException e) {
System.out.println("Could not load password cipher implementation class. Check your classpath.");
availableCiphers();
throw new SystemExitException(-1);
}
if (line.hasOption("decrypt")) {
final String pwdArg = (String) line.getArgList().get(0);
final char[] encryptdPassword = pwdArg.toCharArray();
System.out.println(cipher.decrypt(encryptdPassword));
} else {
// if option neither encrypt/decrypt is specified, we assume
// it is encrypt.
final String plainPassword = (String) line.getArgList().get(0);
System.out.println(new String(cipher.encrypt(plainPassword)));
}
}
use of org.apache.openejb.cipher.PasswordCipher in project tomee by apache.
the class PropertyPlaceHolderHelper method decryptIfNeeded.
private static Object decryptIfNeeded(final String replace, final boolean acceptCharArray) {
if (replace.startsWith(CIPHER_PREFIX)) {
final String algo = replace.substring(CIPHER_PREFIX.length(), replace.indexOf(':', CIPHER_PREFIX.length() + 1));
PasswordCipher cipher;
try {
cipher = PasswordCipherFactory.getPasswordCipher(algo);
} catch (final PasswordCipherException ex) {
try {
cipher = PasswordCipher.class.cast(Thread.currentThread().getContextClassLoader().loadClass(algo).newInstance());
} catch (final Exception e) {
throw new IllegalArgumentException(e);
}
}
final char[] input = replace.substring(CIPHER_PREFIX.length() + algo.length() + 1).toCharArray();
return acceptCharArray && SafePasswordCipher.class.isInstance(cipher) ? SafePasswordCipher.class.cast(cipher).decryptAsCharArray(input) : cipher.decrypt(input);
}
return replace;
}
use of org.apache.openejb.cipher.PasswordCipher in project tomee by apache.
the class BasicManagedDataSource method createDataSource.
protected DataSource createDataSource() throws SQLException {
final ReentrantLock l = lock;
l.lock();
try {
final Object dataSource = Reflections.get(this, "dataSource");
if (dataSource != null) {
return DataSource.class.cast(dataSource);
}
// check password codec if available
if (null != passwordCipher) {
final PasswordCipher cipher = PasswordCipherFactory.getPasswordCipher(passwordCipher);
// always use the initial encrypted value
final String plainPwd = cipher.decrypt(initialPassword.toCharArray());
// override previous password value
super.setPassword(plainPwd);
}
// get the plugin
final DataSourcePlugin helper = BasicDataSourceUtil.getDataSourcePlugin(getUrl());
// configure this
if (helper != null) {
final String currentUrl = getUrl();
final String newUrl = helper.updatedUrl(currentUrl);
if (!currentUrl.equals(newUrl)) {
super.setUrl(newUrl);
}
}
wrapTransactionManager();
// create the data source
if (helper == null || !helper.enableUserDirHack()) {
try {
return super.createDataSource();
} catch (final Throwable e) {
throw BasicDataSource.toSQLException(e);
}
} else {
// wrap super call with code that sets user.dir to openejb.base and then resets it
final Properties systemProperties = JavaSecurityManagers.getSystemProperties();
final String userDir = systemProperties.getProperty("user.dir");
try {
final File base = SystemInstance.get().getBase().getDirectory();
systemProperties.setProperty("user.dir", base.getAbsolutePath());
try {
return super.createDataSource();
} catch (final Throwable e) {
throw BasicDataSource.toSQLException(e);
}
} finally {
systemProperties.setProperty("user.dir", userDir);
}
}
} finally {
l.unlock();
}
}
Aggregations