Skip to content
This repository was archived by the owner on Nov 24, 2024. It is now read-only.

Commit 08ed987

Browse files
authored
plexus-cipher 3.0.0 (#45)
Cipher is a small simple (nearly trivial) component that is able to encrypt/decrypt strings using provided "master password". Package is renamed to `org.codehaus.plexus.components.cipher`. This commit moves it to version 3.0.0 denoting a major (and backward incompatible jump.
1 parent 82a2e20 commit 08ed987

File tree

7 files changed

+36
-51
lines changed

7 files changed

+36
-51
lines changed

pom.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</parent>
1010

1111
<artifactId>plexus-cipher</artifactId>
12-
<version>2.1.1-SNAPSHOT</version>
12+
<version>3.0.0-SNAPSHOT</version>
1313

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

@@ -39,6 +39,7 @@
3939
<groupId>javax.inject</groupId>
4040
<artifactId>javax.inject</artifactId>
4141
<version>1</version>
42+
<scope>provided</scope>
4243
</dependency>
4344
<dependency>
4445
<groupId>org.eclipse.sisu</groupId>
@@ -60,7 +61,7 @@
6061
<artifactId>maven-surefire-plugin</artifactId>
6162
<executions>
6263
<execution>
63-
<id>utf8</id>
64+
<id>default-test</id>
6465
<goals>
6566
<goal>test</goal>
6667
</goals>

src/main/java/org/sonatype/plexus/components/cipher/PlexusCipher.java renamed to src/main/java/org/codehaus/plexus/components/cipher/PlexusCipher.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
1010
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1212
*/
13-
package org.sonatype.plexus.components.cipher;
13+
package org.codehaus.plexus.components.cipher;
1414

1515
/**
1616
* @author Oleg Gusakov
1717
*/
1818
public interface PlexusCipher {
19-
char ENCRYPTED_STRING_DECORATION_START = '{';
20-
21-
char ENCRYPTED_STRING_DECORATION_STOP = '}';
22-
2319
/**
2420
* encrypt given string with the given passPhrase and encode it into base64
2521
*

src/main/java/org/sonatype/plexus/components/cipher/PlexusCipherException.java renamed to src/main/java/org/codehaus/plexus/components/cipher/PlexusCipherException.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,13 @@
1010
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1212
*/
13-
package org.sonatype.plexus.components.cipher;
14-
15-
public class PlexusCipherException extends Exception {
16-
public PlexusCipherException() {}
13+
package org.codehaus.plexus.components.cipher;
1714

15+
public class PlexusCipherException extends RuntimeException {
1816
public PlexusCipherException(String message) {
1917
super(message);
2018
}
2119

22-
public PlexusCipherException(Throwable cause) {
23-
super(cause);
24-
}
25-
2620
public PlexusCipherException(String message, Throwable cause) {
2721
super(message, cause);
2822
}

src/main/java/org/sonatype/plexus/components/cipher/DefaultPlexusCipher.java renamed to src/main/java/org/codehaus/plexus/components/cipher/internal/DefaultPlexusCipher.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1212
*/
13-
package org.sonatype.plexus.components.cipher;
13+
package org.codehaus.plexus.components.cipher.internal;
1414

1515
import javax.inject.Named;
1616
import javax.inject.Singleton;
@@ -22,18 +22,20 @@
2222
import java.util.regex.Matcher;
2323
import java.util.regex.Pattern;
2424

25-
import org.eclipse.sisu.Typed;
25+
import org.codehaus.plexus.components.cipher.PlexusCipher;
26+
import org.codehaus.plexus.components.cipher.PlexusCipherException;
2627

2728
/**
2829
* Default implementation of {@link PlexusCipher}. This class is thread safe.
2930
*
3031
* @author Oleg Gusakov
3132
*/
3233
@Singleton
33-
@Named("default")
34-
@Typed(PlexusCipher.class)
34+
@Named
3535
public class DefaultPlexusCipher implements PlexusCipher {
3636
private static final Pattern ENCRYPTED_STRING_PATTERN = Pattern.compile(".*?[^\\\\]?\\{(.*?[^\\\\])\\}.*");
37+
private static final String ENCRYPTED_STRING_DECORATION_START = "{";
38+
private static final String ENCRYPTED_STRING_DECORATION_STOP = "}";
3739

3840
private final PBECipher _cipher;
3941

@@ -45,7 +47,7 @@ public DefaultPlexusCipher() {
4547
// ---------------------------------------------------------------
4648
@Override
4749
public String encrypt(final String str, final String passPhrase) throws PlexusCipherException {
48-
if (str == null || str.length() < 1) {
50+
if (str == null || str.isEmpty()) {
4951
return str;
5052
}
5153

@@ -61,7 +63,7 @@ public String encryptAndDecorate(final String str, final String passPhrase) thro
6163
// ---------------------------------------------------------------
6264
@Override
6365
public String decrypt(final String str, final String passPhrase) throws PlexusCipherException {
64-
if (str == null || str.length() < 1) {
66+
if (str == null || str.isEmpty()) {
6567
return str;
6668
}
6769

@@ -71,7 +73,7 @@ public String decrypt(final String str, final String passPhrase) throws PlexusCi
7173
// ---------------------------------------------------------------
7274
@Override
7375
public String decryptDecorated(final String str, final String passPhrase) throws PlexusCipherException {
74-
if (str == null || str.length() < 1) {
76+
if (str == null || str.isEmpty()) {
7577
return str;
7678
}
7779

@@ -85,7 +87,7 @@ public String decryptDecorated(final String str, final String passPhrase) throws
8587
// ----------------------------------------------------------------------------
8688
@Override
8789
public boolean isEncryptedString(final String str) {
88-
if (str == null || str.length() < 1) {
90+
if (str == null || str.isEmpty()) {
8991
return false;
9092
}
9193

@@ -98,11 +100,10 @@ public boolean isEncryptedString(final String str) {
98100
@Override
99101
public String unDecorate(final String str) throws PlexusCipherException {
100102
Matcher matcher = ENCRYPTED_STRING_PATTERN.matcher(str);
101-
102103
if (matcher.matches() || matcher.find()) {
103104
return matcher.group(1);
104105
} else {
105-
throw new PlexusCipherException("default.plexus.cipher.badEncryptedPassword");
106+
throw new PlexusCipherException("Malformed decorated string");
106107
}
107108
}
108109

src/main/java/org/sonatype/plexus/components/cipher/PBECipher.java renamed to src/main/java/org/codehaus/plexus/components/cipher/internal/PBECipher.java

+9-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Licensed to the Apache Software Foundation (ASF) under one
1717
under the License.
1818
*/
1919

20-
package org.sonatype.plexus.components.cipher;
20+
package org.codehaus.plexus.components.cipher.internal;
2121

2222
import javax.crypto.Cipher;
2323
import javax.crypto.NoSuchPaddingException;
@@ -26,6 +26,8 @@ Licensed to the Apache Software Foundation (ASF) under one
2626
import javax.crypto.spec.PBEKeySpec;
2727
import javax.crypto.spec.SecretKeySpec;
2828

29+
import java.nio.charset.Charset;
30+
import java.nio.charset.StandardCharsets;
2931
import java.security.InvalidAlgorithmParameterException;
3032
import java.security.InvalidKeyException;
3133
import java.security.NoSuchAlgorithmException;
@@ -34,30 +36,21 @@ Licensed to the Apache Software Foundation (ASF) under one
3436
import java.security.spec.KeySpec;
3537
import java.util.Base64;
3638

39+
import org.codehaus.plexus.components.cipher.PlexusCipherException;
40+
3741
/**
3842
* This class is thread-safe.
3943
*
4044
* @author Oleg Gusakov
4145
*/
4246
public class PBECipher {
43-
protected static final String STRING_ENCODING = "UTF8";
44-
47+
protected static final Charset STRING_ENCODING = StandardCharsets.UTF_8;
4548
protected static final int SPICE_SIZE = 16;
46-
4749
protected static final int SALT_SIZE = 8;
48-
4950
protected static final int CHUNK_SIZE = 16;
50-
51-
protected static final byte WIPER = 0;
52-
53-
protected static final String DIGEST_ALG = "SHA-256";
54-
5551
protected static final String KEY_ALG = "AES";
56-
5752
protected static final String CIPHER_ALG = "AES/CBC/PKCS5Padding";
58-
5953
protected static final int PBE_ITERATIONS = 310000;
60-
6154
private static final SecureRandom _secureRandom = new SecureRandom();
6255

6356
// ---------------------------------------------------------------
@@ -95,7 +88,7 @@ public String encrypt64(final String clearText, final String password) throws Pl
9588

9689
return Base64.getEncoder().encodeToString(allEncryptedBytes);
9790
} catch (Exception e) {
98-
throw new PlexusCipherException(e);
91+
throw new PlexusCipherException(e.getMessage(), e);
9992
}
10093
}
10194

@@ -122,7 +115,7 @@ public String decrypt64(final String encryptedText, final String password) throw
122115

123116
return new String(clearBytes, STRING_ENCODING);
124117
} catch (Exception e) {
125-
throw new PlexusCipherException(e);
118+
throw new PlexusCipherException(e.getMessage(), e);
126119
}
127120
}
128121
// -------------------------------------------------------------------------------
@@ -131,7 +124,7 @@ private Cipher createCipher(final char[] pwd, byte[] salt, final int mode)
131124
InvalidAlgorithmParameterException, InvalidKeySpecException {
132125

133126
KeySpec spec = new PBEKeySpec(pwd, salt, PBE_ITERATIONS, SPICE_SIZE * 16);
134-
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
127+
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
135128
byte[] keyAndIv = factory.generateSecret(spec).getEncoded();
136129

137130
byte[] key = new byte[SPICE_SIZE];

src/test/java/org/sonatype/plexus/components/cipher/DefaultPlexusCipherTest.java renamed to src/test/java/org/codehaus/plexus/components/cipher/internal/DefaultPlexusCipherTest.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1212
*/
13-
package org.sonatype.plexus.components.cipher;
13+
package org.codehaus.plexus.components.cipher.internal;
1414

15+
import org.codehaus.plexus.components.cipher.PlexusCipher;
16+
import org.codehaus.plexus.components.cipher.PlexusCipherException;
1517
import org.junit.jupiter.api.BeforeEach;
18+
import org.junit.jupiter.api.Disabled;
1619
import org.junit.jupiter.api.Test;
1720

1821
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
@@ -31,7 +34,7 @@ class DefaultPlexusCipherTest {
3134

3235
final String str = "my testing phrase";
3336

34-
final String encStr = "cYrPoOelYU0HGlsn3nERAIyiLVVgnsn/KC5ZqeAPG0beOZCYrFwWwBTp3uyxt/yx";
37+
final String encStr = "RRvejxJ+wksH/kWnYfun/GeFoPKh6JHcA2dmxMOIraZiIuLISplmdyvl2Sq04rpP";
3538
PlexusCipher pc;
3639

3740
@BeforeEach
@@ -88,6 +91,7 @@ void testDefaultAlgorithmExists() throws Exception {
8891

8992
// -------------------------------------------------------------
9093

94+
@Disabled("This test is not really a test")
9195
@Test
9296
void stestFindDefaultAlgorithm() {
9397
String[] res = DefaultPlexusCipher.getServiceTypes();
@@ -152,18 +156,14 @@ void testDecrypt() {
152156
@Test
153157
void testDecorate() {
154158
String res = pc.decorate("aaa");
155-
assertEquals(
156-
PlexusCipher.ENCRYPTED_STRING_DECORATION_START + "aaa" + PlexusCipher.ENCRYPTED_STRING_DECORATION_STOP,
157-
res,
158-
"Decoration failed");
159+
assertEquals("{aaa}", res, "Decoration failed");
159160
}
160161

161162
// -------------------------------------------------------------
162163

163164
@Test
164165
void testUnDecorate() throws Exception {
165-
String res = pc.unDecorate(
166-
PlexusCipher.ENCRYPTED_STRING_DECORATION_START + "aaa" + PlexusCipher.ENCRYPTED_STRING_DECORATION_STOP);
166+
String res = pc.unDecorate("{aaa}");
167167
assertEquals("aaa", res, "Decoration failed");
168168
}
169169

src/test/java/org/sonatype/plexus/components/cipher/PBECipherTest.java renamed to src/test/java/org/codehaus/plexus/components/cipher/internal/PBECipherTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Licensed to the Apache Software Foundation (ASF) under one
1717
under the License.
1818
*/
1919

20-
package org.sonatype.plexus.components.cipher;
20+
package org.codehaus.plexus.components.cipher.internal;
2121

2222
import org.junit.jupiter.api.BeforeEach;
2323
import org.junit.jupiter.api.Test;
@@ -34,7 +34,7 @@ class PBECipherTest {
3434

3535
final String clearText = "veryOpenText";
3636

37-
final String encryptedText = "F7eMV2QRQF4H0ODCA1nrTGUWacCXVvPemSjaQjGbO6U=";
37+
final String encryptedText = "xnQ1RvJFoJsHoTZKyv76ej3XTGKt99ShUt/kPv4yHjw=";
3838

3939
final String password = "testtest";
4040

0 commit comments

Comments
 (0)