From 58a681c79aa64c4be0425cf3c6ef3c66067b029d Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Sun, 30 Jul 2023 14:38:16 +0200 Subject: [PATCH 1/2] Fix link to Build status --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4f20502..a5e2054 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Plexus-Cipher ============ -[![Build Status](https://travis-ci.org/codehaus-plexus/plexus-cipher.svg?branch=master)](https://travis-ci.org/codehaus-plexus/plexus-cipher) +[![Build Status](https://github.com/codehaus-plexus/plexus-cipher/actions/workflows/maven.yml/badge.svg)](https://github.com/codehaus-plexus/plexus-cipher/actions/workflows/maven.yml) [![Maven Central](https://img.shields.io/maven-central/v/org.codehaus.plexus/plexus-cipher.svg?label=Maven%20Central)](https://search.maven.org/artifact/org.codehaus.plexus/plexus-cipher) The current master is now at https://github.com/codehaus-plexus/plexus-cipher From c98ebeb81b609632d5d88ebe2c3b37d7d7c54486 Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Sun, 30 Jul 2023 14:33:36 +0200 Subject: [PATCH 2/2] Quick fix reported issues in code style --- .../plexus/components/cipher/Base64.java | 28 +++++++--------- .../cipher/DefaultPlexusCipher.java | 24 ++++---------- .../plexus/components/cipher/PBECipher.java | 4 +-- .../components/cipher/PlexusCipher.java | 32 +++++++++---------- .../cipher/DefaultPlexusCipherTest.java | 17 ++++------ .../components/cipher/PBECipherTest.java | 28 ++++++++-------- 6 files changed, 56 insertions(+), 77 deletions(-) diff --git a/src/main/java/org/sonatype/plexus/components/cipher/Base64.java b/src/main/java/org/sonatype/plexus/components/cipher/Base64.java index 46f0f00..bfaa15c 100644 --- a/src/main/java/org/sonatype/plexus/components/cipher/Base64.java +++ b/src/main/java/org/sonatype/plexus/components/cipher/Base64.java @@ -146,10 +146,10 @@ public class Base64 { } /** - * Returns whether or not the octect is in the base 64 alphabet. + * Returns whether the octect is in the base 64 alphabet. * * @param b The value to test - * @return true if the value is defined in the the base 64 alphabet, false otherwise. + * @return true if the value is defined in the base 64 alphabet, false otherwise. */ public static boolean isBase64(byte b) { return (b == PAD) || (b >= 0 && base64Alphabet[b] >= 0); @@ -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 @@ -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; @@ -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]; @@ -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]; @@ -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 @@ -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) { @@ -447,7 +441,7 @@ static byte[] discardWhitespace(byte[] data) { } } - byte packedData[] = new byte[bytesCopied]; + byte[] packedData = new byte[bytesCopied]; System.arraycopy(groomedData, 0, packedData, 0, bytesCopied); @@ -455,16 +449,16 @@ static byte[] discardWhitespace(byte[] data) { } /** - * 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) { @@ -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); diff --git a/src/main/java/org/sonatype/plexus/components/cipher/DefaultPlexusCipher.java b/src/main/java/org/sonatype/plexus/components/cipher/DefaultPlexusCipher.java index df7d145..7a83010 100644 --- a/src/main/java/org/sonatype/plexus/components/cipher/DefaultPlexusCipher.java +++ b/src/main/java/org/sonatype/plexus/components/cipher/DefaultPlexusCipher.java @@ -120,9 +120,8 @@ public String decorate(final String str) { public static String[] getServiceTypes() { Set 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 keys = provider.keySet(); for (Object o : keys) { @@ -146,7 +145,7 @@ public static String[] getServiceTypes() { public static String[] getCryptoImpls(final String serviceType) { Set result = new HashSet<>(); - // All all providers + // All providers Provider[] providers = Security.getProviders(); for (Provider provider : providers) { // Get services provided by each provider @@ -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); } } } diff --git a/src/main/java/org/sonatype/plexus/components/cipher/PBECipher.java b/src/main/java/org/sonatype/plexus/components/cipher/PBECipher.java index 17a620c..5f3df00 100644 --- a/src/main/java/org/sonatype/plexus/components/cipher/PBECipher.java +++ b/src/main/java/org/sonatype/plexus/components/cipher/PBECipher.java @@ -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]; diff --git a/src/main/java/org/sonatype/plexus/components/cipher/PlexusCipher.java b/src/main/java/org/sonatype/plexus/components/cipher/PlexusCipher.java index 5038f34..efa1fa9 100644 --- a/src/main/java/org/sonatype/plexus/components/cipher/PlexusCipher.java +++ b/src/main/java/org/sonatype/plexus/components/cipher/PlexusCipher.java @@ -23,10 +23,10 @@ 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; @@ -34,20 +34,20 @@ public interface PlexusCipher { * 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; @@ -55,17 +55,17 @@ public interface PlexusCipher { * 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); @@ -73,16 +73,16 @@ public interface PlexusCipher { /** * 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); diff --git a/src/test/java/org/sonatype/plexus/components/cipher/DefaultPlexusCipherTest.java b/src/test/java/org/sonatype/plexus/components/cipher/DefaultPlexusCipherTest.java index 929551a..cf403aa 100644 --- a/src/test/java/org/sonatype/plexus/components/cipher/DefaultPlexusCipherTest.java +++ b/src/test/java/org/sonatype/plexus/components/cipher/DefaultPlexusCipherTest.java @@ -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(); } @@ -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); @@ -108,7 +108,6 @@ public void stestFindDefaultAlgorithm() throws Exception { } // ------------------------------------------------------------- - @Test public void testEncrypt() throws Exception { String xRes = pc.encrypt(str, passPhrase); @@ -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); } @@ -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", diff --git a/src/test/java/org/sonatype/plexus/components/cipher/PBECipherTest.java b/src/test/java/org/sonatype/plexus/components/cipher/PBECipherTest.java index 001a460..98efe8a 100644 --- a/src/test/java/org/sonatype/plexus/components/cipher/PBECipherTest.java +++ b/src/test/java/org/sonatype/plexus/components/cipher/PBECipherTest.java @@ -23,48 +23,48 @@ 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 @@ -72,8 +72,8 @@ 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); } }