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

Commit 03beb95

Browse files
committed
Quick fix reported issues in code style
1 parent 8806f6f commit 03beb95

File tree

6 files changed

+56
-77
lines changed

6 files changed

+56
-77
lines changed

src/main/java/org/sonatype/plexus/components/cipher/Base64.java

+11-17
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ public class Base64 {
146146
}
147147

148148
/**
149-
* Returns whether or not the <code>octect</code> is in the base 64 alphabet.
149+
* Returns whether the <code>octect</code> is in the base 64 alphabet.
150150
*
151151
* @param b The value to test
152-
* @return <code>true</code> if the value is defined in the the base 64 alphabet, <code>false</code> otherwise.
152+
* @return <code>true</code> if the value is defined in the base 64 alphabet, <code>false</code> otherwise.
153153
*/
154154
public static boolean isBase64(byte b) {
155155
return (b == PAD) || (b >= 0 && base64Alphabet[b] >= 0);
@@ -223,8 +223,7 @@ public Object decode(Object pObject) throws IllegalArgumentException {
223223
}
224224

225225
/**
226-
* Decodes a byte[] containing containing
227-
* characters in the Base64 alphabet.
226+
* Decodes a byte[] containing characters in the Base64 alphabet.
228227
*
229228
* @param pArray A byte array containing Base64 character data
230229
* @return a byte array containing binary data
@@ -246,7 +245,7 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) {
246245
int lengthDataBits = binaryData.length * EIGHTBIT;
247246
int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
248247
int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;
249-
byte encodedData[];
248+
byte[] encodedData;
250249
int encodedDataLength;
251250
int nbrChunks = 0;
252251

@@ -293,9 +292,6 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) {
293292
byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc);
294293

295294
encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
296-
// log.debug( "val2 = " + val2 );
297-
// log.debug( "k4 = " + (k<<4) );
298-
// log.debug( "vak = " + (val2 | (k<<4)) );
299295
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2 | (k << 4)];
300296
encodedData[encodedIndex + 2] = lookUpBase64Alphabet[(l << 2) | val3];
301297
encodedData[encodedIndex + 3] = lookUpBase64Alphabet[b3 & 0x3f];
@@ -320,8 +316,6 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) {
320316
if (fewerThan24bits == EIGHTBIT) {
321317
b1 = binaryData[dataIndex];
322318
k = (byte) (b1 & 0x03);
323-
// log.debug("b1=" + b1);
324-
// log.debug("b1<<2 = " + (b1>>2) );
325319
byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
326320
encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
327321
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[k << 4];
@@ -374,7 +368,7 @@ public static byte[] decodeBase64(byte[] base64Data) {
374368
}
375369

376370
int numberQuadruple = base64Data.length / FOURBYTE;
377-
byte decodedData[];
371+
byte[] decodedData;
378372
byte b1, b2, b3, b4, marker0, marker1;
379373

380374
// Throw away anything not in base64Data
@@ -432,7 +426,7 @@ public static byte[] decodeBase64(byte[] base64Data) {
432426
* @return The data, less whitespace (see RFC 2045).
433427
*/
434428
static byte[] discardWhitespace(byte[] data) {
435-
byte groomedData[] = new byte[data.length];
429+
byte[] groomedData = new byte[data.length];
436430
int bytesCopied = 0;
437431

438432
for (byte datum : data) {
@@ -447,24 +441,24 @@ static byte[] discardWhitespace(byte[] data) {
447441
}
448442
}
449443

450-
byte packedData[] = new byte[bytesCopied];
444+
byte[] packedData = new byte[bytesCopied];
451445

452446
System.arraycopy(groomedData, 0, packedData, 0, bytesCopied);
453447

454448
return packedData;
455449
}
456450

457451
/**
458-
* Discards any characters outside of the base64 alphabet, per
452+
* Discards any characters outside the base64 alphabet, per
459453
* the requirements on page 25 of RFC 2045 - "Any characters
460-
* outside of the base64 alphabet are to be ignored in base64
454+
* outside the base64 alphabet are to be ignored in base64
461455
* encoded data."
462456
*
463457
* @param data The base-64 encoded data to groom
464458
* @return The data, less non-base64 characters (see RFC 2045).
465459
*/
466460
static byte[] discardNonBase64(byte[] data) {
467-
byte groomedData[] = new byte[data.length];
461+
byte[] groomedData = new byte[data.length];
468462
int bytesCopied = 0;
469463

470464
for (byte datum : data) {
@@ -473,7 +467,7 @@ static byte[] discardNonBase64(byte[] data) {
473467
}
474468
}
475469

476-
byte packedData[] = new byte[bytesCopied];
470+
byte[] packedData = new byte[bytesCopied];
477471

478472
System.arraycopy(groomedData, 0, packedData, 0, bytesCopied);
479473

src/main/java/org/sonatype/plexus/components/cipher/DefaultPlexusCipher.java

+7-17
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,8 @@ public String decorate(final String str) {
120120
public static String[] getServiceTypes() {
121121
Set<String> result = new HashSet<>();
122122

123-
// All all providers
124-
Provider[] providers = Security.getProviders();
125-
for (Provider provider : providers) {
123+
// All providers
124+
for (Provider provider : Security.getProviders()) {
126125
// Get services provided by each provider
127126
Set<Object> keys = provider.keySet();
128127
for (Object o : keys) {
@@ -146,7 +145,7 @@ public static String[] getServiceTypes() {
146145
public static String[] getCryptoImpls(final String serviceType) {
147146
Set<String> result = new HashSet<>();
148147

149-
// All all providers
148+
// All providers
150149
Provider[] providers = Security.getProviders();
151150
for (Provider provider : providers) {
152151
// Get services provided by each provider
@@ -168,20 +167,11 @@ public static String[] getCryptoImpls(final String serviceType) {
168167

169168
// ---------------------------------------------------------------
170169
public static void main(final String[] args) {
171-
// Security.addProvider( new BouncyCastleProvider() );
172-
173170
String[] serviceTypes = getServiceTypes();
174-
if (serviceTypes != null) {
175-
for (String serviceType : serviceTypes) {
176-
String[] serviceProviders = getCryptoImpls(serviceType);
177-
if (serviceProviders != null) {
178-
System.out.println(serviceType + ": provider list");
179-
for (String provider : serviceProviders) {
180-
System.out.println(" " + provider);
181-
}
182-
} else {
183-
System.out.println(serviceType + ": does not have any providers in this environment");
184-
}
171+
for (String serviceType : serviceTypes) {
172+
System.out.println(serviceType + ": provider list");
173+
for (String provider : getCryptoImpls(serviceType)) {
174+
System.out.println(" " + provider);
185175
}
186176
}
187177
}

src/main/java/org/sonatype/plexus/components/cipher/PBECipher.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,9 @@ private Cipher createCipher(final char[] pwd, byte[] salt, final int mode)
133133
InvalidAlgorithmParameterException, InvalidKeySpecException {
134134
MessageDigest _digester = MessageDigest.getInstance(DIGEST_ALG);
135135

136-
byte[] keyAndIv = new byte[SPICE_SIZE * 2];
137-
138136
KeySpec spec = new PBEKeySpec(pwd, salt, 310000, SPICE_SIZE * 16);
139137
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
140-
keyAndIv = factory.generateSecret(spec).getEncoded();
138+
byte[] keyAndIv = factory.generateSecret(spec).getEncoded();
141139

142140
byte[] key = new byte[SPICE_SIZE];
143141

src/main/java/org/sonatype/plexus/components/cipher/PlexusCipher.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -23,66 +23,66 @@ public interface PlexusCipher {
2323
/**
2424
* encrypt given string with the given passPhrase and encode it into base64
2525
*
26-
* @param str
27-
* @param passPhrase
26+
* @param str string to encrypt
27+
* @param passPhrase pass phrase
2828
* @return encrypted str
29-
* @throws PlexusCipherException
29+
* @throws PlexusCipherException if encryption fails
3030
*/
3131
String encrypt(String str, String passPhrase) throws PlexusCipherException;
3232

3333
/**
3434
* encrypt given string with the given passPhrase, encode it into base64 and return result, wrapped into { }
3535
* decorations
3636
*
37-
* @param str
38-
* @param passPhrase
37+
* @param str string to encrypt
38+
* @param passPhrase pass phrase
3939
* @return encrypted and decorated str
40-
* @throws PlexusCipherException
40+
* @throws PlexusCipherException if encryption fails
4141
*/
4242
String encryptAndDecorate(String str, String passPhrase) throws PlexusCipherException;
4343

4444
/**
4545
* decrypt given base64 encrypted string
4646
*
47-
* @param str
48-
* @param passPhrase
47+
* @param str base64 encoded string
48+
* @param passPhrase pass phrase
4949
* @return decrypted str
50-
* @throws PlexusCipherException
50+
* @throws PlexusCipherException if decryption fails
5151
*/
5252
String decrypt(String str, String passPhrase) throws PlexusCipherException;
5353

5454
/**
5555
* decrypt given base64 encoded encrypted string. If string is decorated, decrypt base64 encoded string inside
5656
* decorations
5757
*
58-
* @param str
59-
* @param passPhrase
58+
* @param str base64 encoded string
59+
* @param passPhrase pass phrase
6060
* @return decrypted decorated str
61-
* @throws PlexusCipherException
61+
* @throws PlexusCipherException if decryption fails
6262
*/
6363
String decryptDecorated(String str, String passPhrase) throws PlexusCipherException;
6464

6565
/**
6666
* check if given string is decorated
6767
*
68-
* @param str
68+
* @param str string to check
6969
* @return true if string is encrypted
7070
*/
7171
boolean isEncryptedString(String str);
7272

7373
/**
7474
* return string inside decorations
7575
*
76-
* @param str
76+
* @param str decorated string
7777
* @return undecorated str
78-
* @throws PlexusCipherException
78+
* @throws PlexusCipherException if decryption fails
7979
*/
8080
String unDecorate(String str) throws PlexusCipherException;
8181

8282
/**
8383
* decorated given string with { and }
8484
*
85-
* @param str
85+
* @param str string to decorate
8686
* @return decorated str
8787
*/
8888
String decorate(String str);

src/test/java/org/sonatype/plexus/components/cipher/DefaultPlexusCipherTest.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
public class DefaultPlexusCipherTest {
2929
private final String passPhrase = "testtest";
3030

31-
String str = "my testing phrase";
31+
final String str = "my testing phrase";
3232

33-
String encStr = "LFulS0pAlmMHpDtm+81oPcqctcwpco5p4Fo7640/gqDRifCahXBefG4FxgKcu17v";
33+
final String encStr = "LFulS0pAlmMHpDtm+81oPcqctcwpco5p4Fo7640/gqDRifCahXBefG4FxgKcu17v";
3434

3535
DefaultPlexusCipher pc;
3636

3737
// -------------------------------------------------------------
3838
@Before
39-
public void prepare() throws Exception {
39+
public void prepare() {
4040
pc = new DefaultPlexusCipher();
4141
}
4242

@@ -90,7 +90,7 @@ public void testDefaultAlgorithmExists() throws Exception {
9090
// -------------------------------------------------------------
9191

9292
@Test
93-
public void stestFindDefaultAlgorithm() throws Exception {
93+
public void stestFindDefaultAlgorithm() {
9494
String[] res = DefaultPlexusCipher.getServiceTypes();
9595
assertNotNull("No service types found in the current environment", res);
9696

@@ -108,7 +108,6 @@ public void stestFindDefaultAlgorithm() throws Exception {
108108
}
109109

110110
// -------------------------------------------------------------
111-
112111
@Test
113112
public void testEncrypt() throws Exception {
114113
String xRes = pc.encrypt(str, passPhrase);
@@ -124,18 +123,16 @@ public void testEncrypt() throws Exception {
124123

125124
@Test
126125
public void testEncryptVariableLengths() throws Exception {
127-
String xRes = null;
128-
String res = null;
129126
String pass = "g";
130127

131128
for (int i = 0; i < 64; i++) {
132129
pass = pass + 'a';
133130

134-
xRes = pc.encrypt(str, pass);
131+
String xRes = pc.encrypt(str, pass);
135132

136133
System.out.println(pass.length() + ": " + xRes);
137134

138-
res = pc.decrypt(xRes, pass);
135+
String res = pc.decrypt(xRes, pass);
139136

140137
assertEquals("Encryption/Decryption did not produce desired result", str, res);
141138
}
@@ -150,7 +147,7 @@ public void testDecrypt() throws Exception {
150147
// -------------------------------------------------------------
151148

152149
@Test
153-
public void testDecorate() throws Exception {
150+
public void testDecorate() {
154151
String res = pc.decorate("aaa");
155152
assertEquals(
156153
"Decoration failed",

src/test/java/org/sonatype/plexus/components/cipher/PBECipherTest.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -23,57 +23,57 @@ Licensed to the Apache Software Foundation (ASF) under one
2323
import org.junit.Test;
2424

2525
import static org.junit.Assert.assertEquals;
26-
import static org.junit.Assert.assertFalse;
26+
import static org.junit.Assert.assertNotEquals;
2727
import static org.junit.Assert.assertNotNull;
2828

2929
/**
3030
* @author Oleg Gusakov
3131
*/
3232
public class PBECipherTest {
33-
PBECipher _cipher;
33+
PBECipher pbeCipher;
3434

35-
String _cleatText = "veryOpenText";
35+
final String clearText = "veryOpenText";
3636

37-
String _encryptedText = "F7eMV2QRQF4H0ODCA1nrTGUWacCXVvPemSjaQjGbO6U=";
37+
final String encryptedText = "F7eMV2QRQF4H0ODCA1nrTGUWacCXVvPemSjaQjGbO6U=";
3838

39-
String _password = "testtest";
39+
final String password = "testtest";
4040

4141
@Before
42-
public void prepare() throws Exception {
43-
_cipher = new PBECipher();
42+
public void prepare() {
43+
pbeCipher = new PBECipher();
4444
}
4545

4646
@Test
4747
public void testEncrypt() throws Exception {
48-
String enc = _cipher.encrypt64(_cleatText, _password);
48+
String enc = pbeCipher.encrypt64(clearText, password);
4949

5050
assertNotNull(enc);
5151

5252
System.out.println(enc);
5353

54-
String enc2 = _cipher.encrypt64(_cleatText, _password);
54+
String enc2 = pbeCipher.encrypt64(clearText, password);
5555

5656
assertNotNull(enc2);
5757

5858
System.out.println(enc2);
5959

60-
assertFalse(enc.equals(enc2));
60+
assertNotEquals(enc, enc2);
6161
}
6262

6363
@Test
6464
public void testDecrypt() throws Exception {
65-
String clear = _cipher.decrypt64(_encryptedText, _password);
65+
String clear = pbeCipher.decrypt64(encryptedText, password);
6666

67-
assertEquals(_cleatText, clear);
67+
assertEquals(clearText, clear);
6868
}
6969

7070
@Test
7171
public void testEncoding() throws Exception {
7272
System.out.println("file.encoding=" + System.getProperty("file.encoding"));
7373

7474
String pwd = "äüöÜÖÄß\"§$%&/()=?é";
75-
String encPwd = _cipher.encrypt64(pwd, pwd);
76-
String decPwd = _cipher.decrypt64(encPwd, pwd);
75+
String encPwd = pbeCipher.encrypt64(pwd, pwd);
76+
String decPwd = pbeCipher.decrypt64(encPwd, pwd);
7777
assertEquals(pwd, decPwd);
7878
}
7979
}

0 commit comments

Comments
 (0)