Skip to content

Commit

Permalink
Update codebase (#4)
Browse files Browse the repository at this point in the history
Changes:
* drop legacy SISU, use latest
* drop legacy test helper class, use vanilla junit
* apply language level changes, where applicable
* fix javadoc where applicable
* Drop legacy plx Xml
* Make classes thread-safe, mark them as singletons
  • Loading branch information
cstamas committed Aug 10, 2021
1 parent 7a20924 commit 48b5a36
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 149 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ target/
.classpath
.settings/
bin
*.iml

44 changes: 25 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>

<artifactId>plexus-cipher</artifactId>
<version>1.9-SNAPSHOT</version>
<version>2.0-SNAPSHOT</version>

<name>Plexus Cipher: encryption/decryption Component</name>

Expand All @@ -32,16 +32,37 @@

<properties>
<javaVersion>7</javaVersion>
<sisuVersion>0.3.4</sisuVersion>
<project.build.outputTimestamp>2020-01-20T18:52:37Z</project.build.outputTimestamp>
</properties>


<dependencies>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.inject</artifactId>
<version>${sisuVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.eclipse.m2e</groupId>
Expand Down Expand Up @@ -106,7 +127,7 @@
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
<version>0.3.4</version>
<version>${sisuVersion}</version>
<executions>
<execution>
<goals>
Expand All @@ -119,19 +140,4 @@
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-inject-bean</artifactId>
<version>2.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class Base64
* The value of undefined encodings is <code>-1</code>.
* </p>
*/
private static byte[] base64Alphabet = new byte[BASELENGTH];
private static final byte[] base64Alphabet = new byte[BASELENGTH];

/**
* <p/>
Expand All @@ -110,7 +110,7 @@ public class Base64
* For example, <code>lookUpBase64Alphabet[62] </code> returns <code>'+'</code>.
* </p>
*/
private static byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
private static final byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];

// Populating the lookup and character arrays
static {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright (c) 2008 Sonatype, Inc. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
Expand All @@ -15,35 +15,38 @@
import java.security.Provider;
import java.security.Security;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.enterprise.inject.Typed;
import javax.inject.Named;
import javax.inject.Singleton;

import org.eclipse.sisu.Typed;

/**
* Default implementation of {@link PlexusCipher}. This class is thread safe.
*
* @author Oleg Gusakov
*/
@Singleton
@Named( "default" )
@Typed( PlexusCipher.class )
public class DefaultPlexusCipher
implements PlexusCipher
{

private static final Pattern ENCRYPTED_STRING_PATTERN = Pattern.compile( ".*?[^\\\\]?\\{(.*?[^\\\\])\\}.*" );

private final PBECipher _cipher;

// ---------------------------------------------------------------
public DefaultPlexusCipher()
throws PlexusCipherException
{
_cipher = new PBECipher();
}

// ---------------------------------------------------------------
@Override
public String encrypt( final String str, final String passPhrase )
throws PlexusCipherException
{
Expand All @@ -56,13 +59,15 @@ public String encrypt( final String str, final String 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
{
Expand All @@ -75,6 +80,7 @@ public String decrypt( final String str, final String passPhrase )
}

// ---------------------------------------------------------------
@Override
public String decryptDecorated( final String str, final String passPhrase )
throws PlexusCipherException
{
Expand All @@ -92,6 +98,7 @@ public String decryptDecorated( final String str, final String passPhrase )
}

// ----------------------------------------------------------------------------
@Override
public boolean isEncryptedString( final String str )
{
if ( str == null || str.length() < 1 )
Expand All @@ -105,7 +112,7 @@ public boolean isEncryptedString( final String str )
}

// ----------------------------------------------------------------------------
// -------------------
@Override
public String unDecorate( final String str )
throws PlexusCipherException
{
Expand All @@ -122,75 +129,67 @@ public String unDecorate( final String str )
}

// ----------------------------------------------------------------------------
// -------------------
@Override
public String decorate( final String str )
{
return ENCRYPTED_STRING_DECORATION_START + ( str == null ? "" : str ) + ENCRYPTED_STRING_DECORATION_STOP;
}

// ---------------------------------------------------------------
// ---------------------------------------------------------------
// ***************************************************************

/**
* Exploratory part. This method returns all available services types
*/
public static String[] getServiceTypes()
{
Set result = new HashSet();
Set<String> result = new HashSet<>();

// All all providers
Provider[] providers = Security.getProviders();
for ( int i = 0; i < providers.length; i++ )
{
for (Provider provider : providers) {
// Get services provided by each provider
Set keys = providers[i].keySet();
for ( Iterator it = keys.iterator(); it.hasNext(); )
{
String key = (String) it.next();
key = key.split( " " )[0];

if ( key.startsWith( "Alg.Alias." ) )
{
Set<Object> keys = provider.keySet();
for (Object o : keys) {
String key = (String) o;
key = key.split(" ")[0];

if (key.startsWith("Alg.Alias.")) {
// Strip the alias
key = key.substring( 10 );
key = key.substring(10);
}
int ix = key.indexOf( '.' );
result.add( key.substring( 0, ix ) );
int ix = key.indexOf('.');
result.add(key.substring(0, ix));
}
}
return (String[]) result.toArray( new String[result.size()] );
return result.toArray( new String[result.size()] );
}

/**
* This method returns the available implementations for a service type
*/
public static String[] getCryptoImpls( final String serviceType )
{
Set result = new HashSet();
Set<String> result = new HashSet<>();

// All all providers
Provider[] providers = Security.getProviders();
for ( int i = 0; i < providers.length; i++ )
{
for (Provider provider : providers) {
// Get services provided by each provider
Set keys = providers[i].keySet();
for ( Iterator it = keys.iterator(); it.hasNext(); )
{
String key = (String) it.next();
key = key.split( " " )[0];

if ( key.startsWith( serviceType + "." ) )
{
result.add( key.substring( serviceType.length() + 1 ) );
Set<Object> keys = provider.keySet();
for (Object o : keys) {
String key = (String) o;
key = key.split(" ")[0];

if (key.startsWith(serviceType + ".")) {
result.add(key.substring(serviceType.length() + 1));
}
else if ( key.startsWith( "Alg.Alias." + serviceType + "." ) )
{
else if (key.startsWith("Alg.Alias." + serviceType + ".")) {
// This is an alias
result.add( key.substring( serviceType.length() + 11 ) );
result.add(key.substring(serviceType.length() + 11));
}
}
}
return (String[]) result.toArray( new String[result.size()] );
return result.toArray( new String[result.size()] );
}

// ---------------------------------------------------------------
Expand All @@ -201,26 +200,18 @@ public static void main( final String[] args )
String[] serviceTypes = getServiceTypes();
if ( serviceTypes != null )
{
for ( int i = 0; i < serviceTypes.length; i++ )
{
String serviceType = serviceTypes[i];
String[] serviceProviders = getCryptoImpls( serviceType );
if ( serviceProviders != null )
{
System.out.println( serviceType + ": provider list" );
for ( int j = 0; j < serviceProviders.length; j++ )
{
String provider = serviceProviders[j];
System.out.println( " " + provider );
for (String serviceType : serviceTypes) {
String[] serviceProviders = getCryptoImpls(serviceType);
if (serviceProviders != null) {
System.out.println(serviceType + ": provider list");
for (String provider : serviceProviders) {
System.out.println(" " + provider);
}
}
else
{
System.out.println( serviceType + ": does not have any providers in this environment" );
else {
System.out.println(serviceType + ": does not have any providers in this environment");
}
}
}
}
// ---------------------------------------------------------------
// ---------------------------------------------------------------
}
Loading

0 comments on commit 48b5a36

Please sign in to comment.