Search in sources :

Example 6 with BasicTextEncryptor

use of org.jasypt.util.text.BasicTextEncryptor in project directory-fortress-core by apache.

the class EncryptUtil method init.

private void init() {
    textEncryptor = new BasicTextEncryptor();
    textEncryptor.setPassword(Config.getInstance().getProperty(CRYPTO_PROP, "adlfarerovcja;39 d"));
}
Also used : BasicTextEncryptor(org.jasypt.util.text.BasicTextEncryptor)

Example 7 with BasicTextEncryptor

use of org.jasypt.util.text.BasicTextEncryptor in project ChatGameFontificator by GlitchCog.

the class FontificatorProperties method encryptProperty.

public void encryptProperty(String key) {
    BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
    textEncryptor.setPassword(ENC_PASSWORD);
    final String decryptedValue = getProperty(key);
    if (decryptedValue != null && !decryptedValue.isEmpty()) {
        try {
            final String encryptedValue = textEncryptor.encrypt(decryptedValue);
            setProperty(key, encryptedValue);
        } catch (Exception e) {
            final String errorMessage = "Error encrypting value for " + key + " property";
            logger.error(errorMessage, e);
            ChatWindow.popup.handleProblem(errorMessage);
            setProperty(key, "");
        }
    }
}
Also used : BasicTextEncryptor(org.jasypt.util.text.BasicTextEncryptor)

Example 8 with BasicTextEncryptor

use of org.jasypt.util.text.BasicTextEncryptor in project incubator-gobblin by apache.

the class PasswordManager method getEncryptors.

private List<TextEncryptor> getEncryptors(CachedInstanceKey cacheKey) {
    List<TextEncryptor> encryptors = new ArrayList<>();
    int numOfEncryptionKeys = cacheKey.numOfEncryptionKeys;
    String suffix = "";
    int i = 1;
    if (cacheKey.masterPasswordFile == null || numOfEncryptionKeys < 1) {
        return encryptors;
    }
    Exception exception = null;
    do {
        Path currentMasterPasswordFile = new Path(cacheKey.masterPasswordFile + suffix);
        try (Closer closer = Closer.create()) {
            if (!fs.exists(currentMasterPasswordFile) || fs.getFileStatus(currentMasterPasswordFile).isDirectory()) {
                continue;
            }
            InputStream in = closer.register(fs.open(currentMasterPasswordFile));
            String masterPassword = new LineReader(new InputStreamReader(in, Charsets.UTF_8)).readLine();
            TextEncryptor encryptor = useStrongEncryptor ? new StrongTextEncryptor() : new BasicTextEncryptor();
            // setPassword() needs to be called via reflection since the TextEncryptor interface doesn't have this method.
            encryptor.getClass().getMethod("setPassword", String.class).invoke(encryptor, masterPassword);
            encryptors.add(encryptor);
            suffix = "." + String.valueOf(i);
        } catch (FileNotFoundException fnf) {
            // It is ok for password files not being present
            LOG.warn("Master password file " + currentMasterPasswordFile + " not found.");
        } catch (IOException ioe) {
            exception = ioe;
            LOG.warn("Master password could not be read from file " + currentMasterPasswordFile);
        } catch (Exception e) {
            LOG.warn("Encryptor could not be instantiated.");
        }
    } while (i++ < numOfEncryptionKeys);
    // Throw exception if could not read any existing password file
    if (encryptors.size() < 1 && exception != null) {
        throw new RuntimeException("Master Password could not be read from any master password file.", exception);
    }
    return encryptors;
}
Also used : Path(org.apache.hadoop.fs.Path) Closer(com.google.common.io.Closer) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) TextEncryptor(org.jasypt.util.text.TextEncryptor) StrongTextEncryptor(org.jasypt.util.text.StrongTextEncryptor) BasicTextEncryptor(org.jasypt.util.text.BasicTextEncryptor) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) StrongTextEncryptor(org.jasypt.util.text.StrongTextEncryptor) LineReader(com.google.common.io.LineReader) BasicTextEncryptor(org.jasypt.util.text.BasicTextEncryptor)

Example 9 with BasicTextEncryptor

use of org.jasypt.util.text.BasicTextEncryptor in project incubator-gobblin by apache.

the class PasswordManagerTest method testBasicEncryptionAndDecryption.

@Test
public void testBasicEncryptionAndDecryption() throws IOException {
    String password = UUID.randomUUID().toString();
    String masterPassword = UUID.randomUUID().toString();
    File masterPwdFile = getMasterPwdFile(masterPassword);
    State state = new State();
    state.setProp(ConfigurationKeys.ENCRYPT_KEY_LOC, masterPwdFile.toString());
    BasicTextEncryptor encryptor = new BasicTextEncryptor();
    encryptor.setPassword(masterPassword);
    String encrypted = encryptor.encrypt(password);
    encrypted = "ENC(" + encrypted + ")";
    String decrypted = PasswordManager.getInstance(state).readPassword(encrypted);
    Assert.assertEquals(decrypted, password);
}
Also used : State(org.apache.gobblin.configuration.State) BasicTextEncryptor(org.jasypt.util.text.BasicTextEncryptor) File(java.io.File) Test(org.testng.annotations.Test)

Example 10 with BasicTextEncryptor

use of org.jasypt.util.text.BasicTextEncryptor in project incubator-gobblin by apache.

the class PasswordManagerTest method testMultipleMasterPasswordsWithoutPasswordFiles.

@Test
public void testMultipleMasterPasswordsWithoutPasswordFiles() throws IOException {
    String password = UUID.randomUUID().toString();
    String masterPassword = UUID.randomUUID().toString();
    String masterPassword1 = UUID.randomUUID().toString();
    File masterPasswordFile = File.createTempFile("masterPassword", null);
    Files.write(masterPassword, masterPasswordFile, Charset.defaultCharset());
    State state = new State();
    BasicTextEncryptor encryptor = new BasicTextEncryptor();
    state.setProp(ConfigurationKeys.ENCRYPT_KEY_LOC, masterPasswordFile.toString());
    PasswordManager passwordManager = PasswordManager.getInstance(state);
    // Test current master password
    encryptor.setPassword(masterPassword);
    String encrypted = "ENC(" + encryptor.encrypt(password) + ")";
    String decrypted = passwordManager.readPassword(encrypted);
    Assert.assertEquals(decrypted, password);
    // Test last master password using same passwordManager
    // This should throw FileNotFoundException as file for masterPassword1 is not created.
    encryptor = new BasicTextEncryptor();
    encryptor.setPassword(masterPassword1);
    encrypted = "ENC(" + encryptor.encrypt(password) + ")";
    try {
        passwordManager.readPassword(encrypted);
    } catch (RuntimeException e) {
        Assert.assertTrue(e.getMessage().startsWith("Failed to decrypt password"));
        return;
    }
    Assert.fail("Password Manager decrypted password without correct master password.");
}
Also used : State(org.apache.gobblin.configuration.State) BasicTextEncryptor(org.jasypt.util.text.BasicTextEncryptor) File(java.io.File) Test(org.testng.annotations.Test)

Aggregations

BasicTextEncryptor (org.jasypt.util.text.BasicTextEncryptor)13 File (java.io.File)4 State (org.apache.gobblin.configuration.State)4 Test (org.testng.annotations.Test)4 StrongTextEncryptor (org.jasypt.util.text.StrongTextEncryptor)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Closer (com.google.common.io.Closer)1 LineReader (com.google.common.io.LineReader)1 Config (com.typesafe.config.Config)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 Path (org.apache.hadoop.fs.Path)1 TextEncryptor (org.jasypt.util.text.TextEncryptor)1 Test (org.junit.Test)1