Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
Quick fix reported issues in code style
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Jul 30, 2023
1 parent 8806f6f commit 03beb95
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 77 deletions.
28 changes: 11 additions & 17 deletions src/main/java/org/sonatype/plexus/components/cipher/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ public class Base64 {
}

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

/**
* Decodes a byte[] containing containing
* characters in the Base64 alphabet.
* Decodes a byte[] containing characters in the Base64 alphabet.
*
* @param pArray A byte array containing Base64 character data
* @return a byte array containing binary data
Expand All @@ -246,7 +245,7 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) {
int lengthDataBits = binaryData.length * EIGHTBIT;
int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;
byte encodedData[];
byte[] encodedData;
int encodedDataLength;
int nbrChunks = 0;

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

encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
// log.debug( "val2 = " + val2 );
// log.debug( "k4 = " + (k<<4) );
// log.debug( "vak = " + (val2 | (k<<4)) );
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2 | (k << 4)];
encodedData[encodedIndex + 2] = lookUpBase64Alphabet[(l << 2) | val3];
encodedData[encodedIndex + 3] = lookUpBase64Alphabet[b3 & 0x3f];
Expand All @@ -320,8 +316,6 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) {
if (fewerThan24bits == EIGHTBIT) {
b1 = binaryData[dataIndex];
k = (byte) (b1 & 0x03);
// log.debug("b1=" + b1);
// log.debug("b1<<2 = " + (b1>>2) );
byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[k << 4];
Expand Down Expand Up @@ -374,7 +368,7 @@ public static byte[] decodeBase64(byte[] base64Data) {
}

int numberQuadruple = base64Data.length / FOURBYTE;
byte decodedData[];
byte[] decodedData;
byte b1, b2, b3, b4, marker0, marker1;

// Throw away anything not in base64Data
Expand Down Expand Up @@ -432,7 +426,7 @@ public static byte[] decodeBase64(byte[] base64Data) {
* @return The data, less whitespace (see RFC 2045).
*/
static byte[] discardWhitespace(byte[] data) {
byte groomedData[] = new byte[data.length];
byte[] groomedData = new byte[data.length];
int bytesCopied = 0;

for (byte datum : data) {
Expand All @@ -447,24 +441,24 @@ static byte[] discardWhitespace(byte[] data) {
}
}

byte packedData[] = new byte[bytesCopied];
byte[] packedData = new byte[bytesCopied];

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

return packedData;
}

/**
* Discards any characters outside of the base64 alphabet, per
* Discards any characters outside the base64 alphabet, per
* the requirements on page 25 of RFC 2045 - "Any characters
* outside of the base64 alphabet are to be ignored in base64
* outside the base64 alphabet are to be ignored in base64
* encoded data."
*
* @param data The base-64 encoded data to groom
* @return The data, less non-base64 characters (see RFC 2045).
*/
static byte[] discardNonBase64(byte[] data) {
byte groomedData[] = new byte[data.length];
byte[] groomedData = new byte[data.length];
int bytesCopied = 0;

for (byte datum : data) {
Expand All @@ -473,7 +467,7 @@ static byte[] discardNonBase64(byte[] data) {
}
}

byte packedData[] = new byte[bytesCopied];
byte[] packedData = new byte[bytesCopied];

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ public String decorate(final String str) {
public static String[] getServiceTypes() {
Set<String> result = new HashSet<>();

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

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

// ---------------------------------------------------------------
public static void main(final String[] args) {
// Security.addProvider( new BouncyCastleProvider() );

String[] serviceTypes = getServiceTypes();
if (serviceTypes != null) {
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");
}
for (String serviceType : serviceTypes) {
System.out.println(serviceType + ": provider list");
for (String provider : getCryptoImpls(serviceType)) {
System.out.println(" " + provider);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,9 @@ private Cipher createCipher(final char[] pwd, byte[] salt, final int mode)
InvalidAlgorithmParameterException, InvalidKeySpecException {
MessageDigest _digester = MessageDigest.getInstance(DIGEST_ALG);

byte[] keyAndIv = new byte[SPICE_SIZE * 2];

KeySpec spec = new PBEKeySpec(pwd, salt, 310000, SPICE_SIZE * 16);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
keyAndIv = factory.generateSecret(spec).getEncoded();
byte[] keyAndIv = factory.generateSecret(spec).getEncoded();

byte[] key = new byte[SPICE_SIZE];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,66 +23,66 @@ public interface PlexusCipher {
/**
* encrypt given string with the given passPhrase and encode it into base64
*
* @param str
* @param passPhrase
* @param str string to encrypt
* @param passPhrase pass phrase
* @return encrypted str
* @throws PlexusCipherException
* @throws PlexusCipherException if encryption fails
*/
String encrypt(String str, String passPhrase) throws PlexusCipherException;

/**
* encrypt given string with the given passPhrase, encode it into base64 and return result, wrapped into { }
* decorations
*
* @param str
* @param passPhrase
* @param str string to encrypt
* @param passPhrase pass phrase
* @return encrypted and decorated str
* @throws PlexusCipherException
* @throws PlexusCipherException if encryption fails
*/
String encryptAndDecorate(String str, String passPhrase) throws PlexusCipherException;

/**
* decrypt given base64 encrypted string
*
* @param str
* @param passPhrase
* @param str base64 encoded string
* @param passPhrase pass phrase
* @return decrypted str
* @throws PlexusCipherException
* @throws PlexusCipherException if decryption fails
*/
String decrypt(String str, String passPhrase) throws PlexusCipherException;

/**
* decrypt given base64 encoded encrypted string. If string is decorated, decrypt base64 encoded string inside
* decorations
*
* @param str
* @param passPhrase
* @param str base64 encoded string
* @param passPhrase pass phrase
* @return decrypted decorated str
* @throws PlexusCipherException
* @throws PlexusCipherException if decryption fails
*/
String decryptDecorated(String str, String passPhrase) throws PlexusCipherException;

/**
* check if given string is decorated
*
* @param str
* @param str string to check
* @return true if string is encrypted
*/
boolean isEncryptedString(String str);

/**
* return string inside decorations
*
* @param str
* @param str decorated string
* @return undecorated str
* @throws PlexusCipherException
* @throws PlexusCipherException if decryption fails
*/
String unDecorate(String str) throws PlexusCipherException;

/**
* decorated given string with { and }
*
* @param str
* @param str string to decorate
* @return decorated str
*/
String decorate(String str);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
public class DefaultPlexusCipherTest {
private final String passPhrase = "testtest";

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

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

DefaultPlexusCipher pc;

// -------------------------------------------------------------
@Before
public void prepare() throws Exception {
public void prepare() {
pc = new DefaultPlexusCipher();
}

Expand Down Expand Up @@ -90,7 +90,7 @@ public void testDefaultAlgorithmExists() throws Exception {
// -------------------------------------------------------------

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

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

// -------------------------------------------------------------

@Test
public void testEncrypt() throws Exception {
String xRes = pc.encrypt(str, passPhrase);
Expand All @@ -124,18 +123,16 @@ public void testEncrypt() throws Exception {

@Test
public void testEncryptVariableLengths() throws Exception {
String xRes = null;
String res = null;
String pass = "g";

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

xRes = pc.encrypt(str, pass);
String xRes = pc.encrypt(str, pass);

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

res = pc.decrypt(xRes, pass);
String res = pc.decrypt(xRes, pass);

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

@Test
public void testDecorate() throws Exception {
public void testDecorate() {
String res = pc.decorate("aaa");
assertEquals(
"Decoration failed",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,57 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;

/**
* @author Oleg Gusakov
*/
public class PBECipherTest {
PBECipher _cipher;
PBECipher pbeCipher;

String _cleatText = "veryOpenText";
final String clearText = "veryOpenText";

String _encryptedText = "F7eMV2QRQF4H0ODCA1nrTGUWacCXVvPemSjaQjGbO6U=";
final String encryptedText = "F7eMV2QRQF4H0ODCA1nrTGUWacCXVvPemSjaQjGbO6U=";

String _password = "testtest";
final String password = "testtest";

@Before
public void prepare() throws Exception {
_cipher = new PBECipher();
public void prepare() {
pbeCipher = new PBECipher();
}

@Test
public void testEncrypt() throws Exception {
String enc = _cipher.encrypt64(_cleatText, _password);
String enc = pbeCipher.encrypt64(clearText, password);

assertNotNull(enc);

System.out.println(enc);

String enc2 = _cipher.encrypt64(_cleatText, _password);
String enc2 = pbeCipher.encrypt64(clearText, password);

assertNotNull(enc2);

System.out.println(enc2);

assertFalse(enc.equals(enc2));
assertNotEquals(enc, enc2);
}

@Test
public void testDecrypt() throws Exception {
String clear = _cipher.decrypt64(_encryptedText, _password);
String clear = pbeCipher.decrypt64(encryptedText, password);

assertEquals(_cleatText, clear);
assertEquals(clearText, clear);
}

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

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

0 comments on commit 03beb95

Please sign in to comment.