package org.jboss.shrinkwrap.resolver.impl.maven.internal.decrypt;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.sonatype.plexus.components.cipher.PBECipher;
import org.sonatype.plexus.components.cipher.PlexusCipher;
import org.sonatype.plexus.components.cipher.PlexusCipherException;
public class MavenPlexusCipher implements PlexusCipher {
private static final Pattern ENCRYPTED_PATTERN_WITH_PRECEDING_STRING =
Pattern.compile("[^\\\\$]\\{(.*?[^\\\\])\\}.*", Pattern.DOTALL);
private static final Pattern ENCRYPTED_PATTERN_WITHOUT_PRECEDING_STRING =
Pattern.compile("^\\{(.*?[^\\\\])\\}.*", Pattern.MULTILINE + Pattern.DOTALL);
private final PBECipher cipher;
public MavenPlexusCipher() throws IllegalStateException {
try {
cipher = new PBECipher();
} catch (PlexusCipherException e) {
throw new IllegalStateException("Unable to instantiate Cipher to decrypt Maven passwords");
}
}
@Override
public String encrypt(final String str, final String passPhrase) throws PlexusCipherException {
if (str == null || str.length() < 1) {
return str;
}
return cipher.encrypt64(str, passPhrase);
}
@Override
public String encryptAndDecorate(final String str, final String passPhrase) throws PlexusCipherException {
return decorate(encrypt(str, passPhrase));
}
@Override
public String decrypt(final String str, final String passPhrase) throws PlexusCipherException {
if (str == null || str.length() < 1) {
return str;
}
return cipher.decrypt64(str, passPhrase);
}
@Override
public String decryptDecorated(final String str, final String passPhrase) throws PlexusCipherException {
if (str == null || str.length() < 1) {
return str;
}
if (isEncryptedString(str)) {
return decrypt(unDecorate(str), passPhrase);
}
return decrypt(str, passPhrase);
}
@Override
public boolean isEncryptedString(final String str) {
if (str == null || str.length() < 1) {
return false;
}
Matcher matcherWithPrecString = ENCRYPTED_PATTERN_WITH_PRECEDING_STRING.matcher(str);
Matcher matcherWithoutPrecString = ENCRYPTED_PATTERN_WITHOUT_PRECEDING_STRING.matcher(str);
return matcherWithoutPrecString.matches() || matcherWithoutPrecString.find()
|| matcherWithPrecString.matches() || matcherWithPrecString.find();
}
@Override
public String unDecorate(final String str) throws PlexusCipherException {
Matcher matcherWithoutPrecString = ENCRYPTED_PATTERN_WITHOUT_PRECEDING_STRING.matcher(str);
if (matcherWithoutPrecString.matches() || matcherWithoutPrecString.find()) {
return matcherWithoutPrecString.group(1);
}
Matcher matcherWithPrecString = ENCRYPTED_PATTERN_WITH_PRECEDING_STRING.matcher(str);
if (matcherWithPrecString.matches() || matcherWithPrecString.find()) {
return matcherWithPrecString.group(1);
} else {
throw new IllegalStateException("Unable to undecorate decrypted string " + str);
}
}
@Override
public String decorate(final String str) {
return ENCRYPTED_STRING_DECORATION_START + (str == null ? "" : str) + ENCRYPTED_STRING_DECORATION_STOP;
}
}