test formatter

This commit is contained in:
Loredana Crusoveanu
2023-08-11 20:41:34 +03:00
parent db65a7002b
commit cc012c9c47
372 changed files with 2701 additions and 4007 deletions

View File

@@ -12,7 +12,7 @@ import com.baeldung.crypto.utils.CryptoUtils;
public class BadPaddingExamples {
public static byte[] encryptAndDecryptUsingDifferentKeys(byte[] plainTextBytes)
throws InvalidKeyException, GeneralSecurityException {
throws InvalidKeyException, GeneralSecurityException {
SecretKey encryptionKey = CryptoUtils.getKeyForText("BaeldungIsASuperCoolSite");
SecretKey differentKey = CryptoUtils.getKeyForText("ThisGivesUsAnAlternative");
@@ -27,7 +27,7 @@ public class BadPaddingExamples {
}
public static byte[] encryptAndDecryptUsingDifferentAlgorithms(SecretKey key, IvParameterSpec ivParameterSpec,
byte[] plainTextBytes) throws InvalidKeyException, GeneralSecurityException {
byte[] plainTextBytes) throws InvalidKeyException, GeneralSecurityException {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
@@ -41,7 +41,7 @@ public class BadPaddingExamples {
}
public static byte[] encryptAndDecryptUsingDifferentPaddings(SecretKey key, byte[] plainTextBytes)
throws InvalidKeyException, GeneralSecurityException {
throws InvalidKeyException, GeneralSecurityException {
Cipher cipher = Cipher.getInstance("AES/ECB/ISO10126Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherTextBytes = cipher.doFinal(plainTextBytes);
@@ -53,7 +53,8 @@ public class BadPaddingExamples {
}
public static byte[] encryptAndDecryptUsingSamePaddingKeyAndAlgorithm(SecretKey key, byte[] plainTextBytes)
throws InvalidKeyException, GeneralSecurityException {
throws InvalidKeyException,
GeneralSecurityException {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherTextBytes = cipher.doFinal(plainTextBytes);

View File

@@ -14,7 +14,8 @@ import com.baeldung.crypto.utils.CryptoUtils;
public class IllegalBlockSizeExamples {
public static byte[] encryptWithoutPadding(SecretKey key, byte[] plainTextBytes) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
NoSuchPaddingException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
@@ -22,7 +23,8 @@ public class IllegalBlockSizeExamples {
}
public static byte[] decryptTextThatIsNotEncrypted(SecretKey key) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
NoSuchPaddingException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException {
// note that this text is not encrypted at any point in this method.
String sampleText = "https://www.baeldung.com/";
byte[] unencryptedCipherTextBytes = sampleText.getBytes();

View File

@@ -10,7 +10,7 @@ import javax.crypto.spec.IvParameterSpec;
public class InvalidAlgorithmParameterExamples {
public static byte[] encryptUsingIv(SecretKey key, byte[] ivBytes, String plainText)
throws InvalidKeyException, GeneralSecurityException {
throws InvalidKeyException, GeneralSecurityException {
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

View File

@@ -12,7 +12,7 @@ import com.baeldung.crypto.utils.CryptoUtils;
public class InvalidKeyExamples {
public static byte[] decryptUsingCBCWithNoIV(SecretKey key, byte[] cipherTextBytes)
throws InvalidKeyException, GeneralSecurityException {
throws InvalidKeyException, GeneralSecurityException {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
@@ -20,7 +20,7 @@ public class InvalidKeyExamples {
}
public static byte[] decryptUsingCBCWithIV(SecretKey key, byte[] cipherTextBytes)
throws InvalidKeyException, GeneralSecurityException {
throws InvalidKeyException, GeneralSecurityException {
byte[] ivBytes = new byte[] { 'B', 'a', 'e', 'l', 'd', 'u', 'n', 'g', 'I', 's', 'G', 'r', 'e', 'a', 't', '!' };
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);

View File

@@ -8,22 +8,18 @@ import javax.crypto.NoSuchPaddingException;
public class NoSuchAlgorithmExamples {
public static Cipher getCipherInstanceWithBadAlgorithm()
throws NoSuchAlgorithmException, NoSuchPaddingException {
return Cipher.getInstance("ABC");
}
throws NoSuchAlgorithmException, NoSuchPaddingException { return Cipher.getInstance("ABC"); }
public static Cipher getCipherInstanceWithBadAlgorithmMode()
throws NoSuchAlgorithmException, NoSuchPaddingException {
throws NoSuchAlgorithmException, NoSuchPaddingException {
return Cipher.getInstance("AES/ABC");
}
public static Cipher getCipherInstanceWithBadPadding()
throws NoSuchAlgorithmException, NoSuchPaddingException {
return Cipher.getInstance("AES/CBC/ABC");
}
throws NoSuchAlgorithmException, NoSuchPaddingException { return Cipher.getInstance("AES/CBC/ABC"); }
public static Cipher getCipherInstanceWithValidAlgorithm()
throws NoSuchAlgorithmException, NoSuchPaddingException {
throws NoSuchAlgorithmException, NoSuchPaddingException {
return Cipher.getInstance("AES/CBC/PKCS5Padding");
}
}

View File

@@ -50,7 +50,7 @@ public class CryptoUtils {
}
public static IvParameterSpec getIVSecureRandom(String algorithm)
throws NoSuchAlgorithmException, NoSuchPaddingException {
throws NoSuchAlgorithmException, NoSuchPaddingException {
SecureRandom random = SecureRandom.getInstanceStrong();
byte[] iv = new byte[Cipher.getInstance(algorithm).getBlockSize()];
random.nextBytes(iv);
@@ -70,7 +70,8 @@ public class CryptoUtils {
}
public static byte[] encryptWithPadding(SecretKey key, byte[] bytes) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
@@ -79,7 +80,8 @@ public class CryptoUtils {
}
public static byte[] decryptWithPadding(SecretKey key, byte[] cipherTextBytes) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);

View File

@@ -14,11 +14,10 @@ import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class HMACUtil {
public static String hmacWithJava(String algorithm, String data, String key)
throws NoSuchAlgorithmException, InvalidKeyException {
throws NoSuchAlgorithmException, InvalidKeyException {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), algorithm);
Mac mac = Mac.getInstance(algorithm);
mac.init(secretKeySpec);
@@ -43,14 +42,14 @@ public class HMACUtil {
private static Digest getHashDigest(String algorithm) {
switch (algorithm) {
case "HmacMD5":
return new MD5Digest();
case "HmacSHA256":
return new SHA256Digest();
case "HmacSHA384":
return new SHA384Digest();
case "HmacSHA512":
return new SHA512Digest();
case "HmacMD5":
return new MD5Digest();
case "HmacSHA256":
return new SHA256Digest();
case "HmacSHA384":
return new SHA384Digest();
case "HmacSHA512":
return new SHA512Digest();
}
return new SHA256Digest();
}
@@ -58,10 +57,10 @@ public class HMACUtil {
public static String bytesToHex(byte[] hash) {
StringBuilder hexString = new StringBuilder(2 * hash.length);
for (byte h : hash) {
String hex = Integer.toHexString(0xff & h);
if (hex.length() == 1)
hexString.append('0');
hexString.append(hex);
String hex = Integer.toHexString(0xff & h);
if (hex.length() == 1)
hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
}

View File

@@ -25,11 +25,13 @@ import java.util.Random;
import static javax.crypto.Cipher.ENCRYPT_MODE;
public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
private static final String CIPHER = "AES";
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException {
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException,
IllegalBlockSizeException, BadPaddingException {
String plainText = "How you doing Mike?";
encrypt(plainText, getRandomKey(CIPHER, 128));
@@ -61,7 +63,8 @@ public class Main {
}
}
private static void encrypt(String plainText, Key key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
private static void encrypt(String plainText, Key key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(ENCRYPT_MODE, key);
byte[] cipherTextBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));

View File

@@ -25,7 +25,7 @@ public class ConversionClassUtil {
// Generating Secret Key using password and salt
public static SecretKey getKeyFromPassword(String password, String salt)
throws NoSuchAlgorithmException, InvalidKeySpecException {
throws NoSuchAlgorithmException, InvalidKeySpecException {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 65536, 256);
SecretKey originalKey = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");

View File

@@ -9,7 +9,8 @@ import org.junit.jupiter.api.Test;
import com.baeldung.crypto.utils.CryptoUtils;
public class CryptoDriverIVUnitTest{
public class CryptoDriverIVUnitTest {
private CryptoDriver driver = new CryptoDriver();
private String TEST_DATA = "Encrypt this for testing";

View File

@@ -32,29 +32,29 @@ public class BadPaddingExamplesUnitTest {
@Test
public void givenTwoDifferentAlgorithmPaddings_whenDecrypting_thenBadPaddingExceptionIsThrown()
throws GeneralSecurityException {
throws GeneralSecurityException {
Assert.assertThrows(BadPaddingException.class,
() -> BadPaddingExamples.encryptAndDecryptUsingDifferentPaddings(key, plainTextBytes));
() -> BadPaddingExamples.encryptAndDecryptUsingDifferentPaddings(key, plainTextBytes));
}
@Test
public void givenTwoDifferentKeys_whenDecrypting_thenBadPaddingExceptionIsThrown() throws GeneralSecurityException {
Assert.assertThrows(BadPaddingException.class,
() -> BadPaddingExamples.encryptAndDecryptUsingDifferentKeys(plainTextBytes));
() -> BadPaddingExamples.encryptAndDecryptUsingDifferentKeys(plainTextBytes));
}
@Test
public void givenTwoDifferentAlgorithms_whenDecrypting_thenBadPaddingExceptionIsThrown()
throws GeneralSecurityException {
Assert.assertThrows(BadPaddingException.class, () -> BadPaddingExamples
.encryptAndDecryptUsingDifferentAlgorithms(key, ivParameterSpec, plainTextBytes));
throws GeneralSecurityException {
Assert.assertThrows(BadPaddingException.class, () -> BadPaddingExamples.encryptAndDecryptUsingDifferentAlgorithms(key, ivParameterSpec,
plainTextBytes));
}
@Test
public void givenSameVariablesUsedForEncryptingAndDecrypting_whenDecrypting_thenNoExceptionIsThrown()
throws GeneralSecurityException {
throws GeneralSecurityException {
byte[] decryptedBytes = BadPaddingExamples.encryptAndDecryptUsingSamePaddingKeyAndAlgorithm(key,
plainTextBytes);
plainTextBytes);
Assert.assertEquals(plainText, new String(decryptedBytes));
}

View File

@@ -31,22 +31,24 @@ public class IllegalBlockSizeExamplesUnitTest {
@Test
public void whenEncryptingPlainTextWithoutPadding_thenIllegalBlockSizeExceptionIsThrown()
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {
Assert.assertThrows(IllegalBlockSizeException.class,
() -> IllegalBlockSizeExamples.encryptWithoutPadding(key, plainTextBytes));
() -> IllegalBlockSizeExamples.encryptWithoutPadding(key, plainTextBytes));
}
@Test
public void whenDecryptingCipherTextThatWasNotEncrypted_thenIllegalBlockSizeExceptionIsThrown()
throws GeneralSecurityException {
throws GeneralSecurityException {
Assert.assertThrows(IllegalBlockSizeException.class,
() -> IllegalBlockSizeExamples.decryptTextThatIsNotEncrypted(key));
() -> IllegalBlockSizeExamples.decryptTextThatIsNotEncrypted(key));
}
@Test
public void whenEncryptingAndDecryptingWithPadding_thenNoExceptionThrown() throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {
byte[] cipherTextBytes = CryptoUtils.encryptWithPadding(key, plainTextBytes);
byte[] decryptedBytes = CryptoUtils.decryptWithPadding(key, cipherTextBytes);

View File

@@ -27,19 +27,19 @@ public class InvalidAlgorithmParameterExamplesUnitTest {
@Test
public void givenIvIsTooShort_whenEncryptingUsingCBC_thenInvalidAlgorithmParameterExceptionIsThrown()
throws GeneralSecurityException {
throws GeneralSecurityException {
byte[] ivBytes = new byte[] { 'B', 'a', 'e', 'l', 'd', 'u', 'n', 'g', 'I', 's', 'G', 'r', 'e', 'a', 't' };
Assert.assertThrows(InvalidAlgorithmParameterException.class,
() -> InvalidAlgorithmParameterExamples.encryptUsingIv(key, ivBytes, plainText));
() -> InvalidAlgorithmParameterExamples.encryptUsingIv(key, ivBytes, plainText));
}
@Test
public void givenIvIsTooLong_whenEncryptingUsingCBC_thenInvalidAlgorithmParameterExceptionIsThrown()
throws GeneralSecurityException {
throws GeneralSecurityException {
byte[] ivBytes = new byte[] { 'B', 'a', 'e', 'l', 'd', 'u', 'n', 'g', 'I', 's', 'G', 'r', 'e', 'a', 't', '!',
'?' };
'?' };
Assert.assertThrows(InvalidAlgorithmParameterException.class,
() -> InvalidAlgorithmParameterExamples.encryptUsingIv(key, ivBytes, plainText));
() -> InvalidAlgorithmParameterExamples.encryptUsingIv(key, ivBytes, plainText));
}
@Test

View File

@@ -38,12 +38,12 @@ public class InvalidKeyExamplesUnitTest {
@Test
public void givenTextEncryptedWithCBC_whenDecryptingWithNoIv_thenInvalidKeyExceptionIsThrown() {
Assert.assertThrows(InvalidKeyException.class,
() -> InvalidKeyExamples.decryptUsingCBCWithNoIV(key, cipherTextBytes));
() -> InvalidKeyExamples.decryptUsingCBCWithNoIV(key, cipherTextBytes));
}
@Test
public void givenTextEncryptedWithCBC_whenDecryptingWithIv_thenTextIsDecrypted()
throws InvalidKeyException, GeneralSecurityException {
throws InvalidKeyException, GeneralSecurityException {
byte[] decryptedBytes = InvalidKeyExamples.decryptUsingCBCWithIV(key, cipherTextBytes);
Assert.assertEquals(plainText, new String(decryptedBytes));

View File

@@ -12,23 +12,23 @@ public class NoSuchAlgorithmExamplesUnitTest {
@Test
public void whenInitingCipherWithUnknownAlgorithm_thenNoSuchAlgorithmExceptionIsThrown()
throws GeneralSecurityException {
throws GeneralSecurityException {
Assert.assertThrows(NoSuchAlgorithmException.class,
() -> NoSuchAlgorithmExamples.getCipherInstanceWithBadAlgorithm());
() -> NoSuchAlgorithmExamples.getCipherInstanceWithBadAlgorithm());
}
@Test
public void whenInitingCipherWithUnknownAlgorithmMode_thenNoSuchAlgorithmExceptionIsThrown()
throws GeneralSecurityException {
throws GeneralSecurityException {
Assert.assertThrows(NoSuchAlgorithmException.class,
() -> NoSuchAlgorithmExamples.getCipherInstanceWithBadAlgorithmMode());
() -> NoSuchAlgorithmExamples.getCipherInstanceWithBadAlgorithmMode());
}
@Test
public void whenInitingCipherWithUnknownPadding_thenNoSuchAlgorithmExceptionIsThrown()
throws GeneralSecurityException {
throws GeneralSecurityException {
Assert.assertThrows(NoSuchAlgorithmException.class,
() -> NoSuchAlgorithmExamples.getCipherInstanceWithBadPadding());
() -> NoSuchAlgorithmExamples.getCipherInstanceWithBadPadding());
}
@Test

View File

@@ -7,6 +7,7 @@ import java.security.NoSuchAlgorithmException;
import static org.assertj.core.api.Assertions.assertThat;
public class CryptographyStrengthUnitTest {
private static final int UNLIMITED_KEY_SIZE = 2147483647;
@Test

View File

@@ -36,11 +36,7 @@ public class HashPasswordUnitTest {
int memLimit = 66536;
int hashLength = 32;
int parallelism = 1;
Argon2Parameters.Builder builder = new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id).withVersion(Argon2Parameters.ARGON2_VERSION_13)
.withIterations(iterations)
.withMemoryAsKB(memLimit)
.withParallelism(parallelism)
.withSalt(salt);
Argon2Parameters.Builder builder = new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id).withVersion(Argon2Parameters.ARGON2_VERSION_13).withIterations(iterations).withMemoryAsKB(memLimit).withParallelism(parallelism).withSalt(salt);
Argon2BytesGenerator generate = new Argon2BytesGenerator();
generate.init(builder.build());

View File

@@ -11,7 +11,7 @@ public class HMACUtilUnitTest {
@Test
public void givenDataAndKeyAndAlgorithm_whenHmacWithJava_thenSuccess()
throws NoSuchAlgorithmException, InvalidKeyException {
throws NoSuchAlgorithmException, InvalidKeyException {
//given
String hmacSHA256Value = "5b50d80c7dc7ae8bb1b1433cc0b99ecd2ac8397a555c6f75cb8a619ae35a0c35";
@@ -47,7 +47,7 @@ public class HMACUtilUnitTest {
//given
String hmacSHA512Value = "b313a21908df55c9e322e3c65a4b0b7561ab1594ca806b3affbc0d769a1" +
"290c1922aa6622587bea3c0c4d871470a6d06f54dbd20dbda84250e2741eb01f08e33";
"290c1922aa6622587bea3c0c4d871470a6d06f54dbd20dbda84250e2741eb01f08e33";
String hmacSHA512Algorithm = "HmacSHA512";
String data = "baeldung";
String key = "123456";

View File

@@ -12,7 +12,7 @@ public class ConversionClassUtilUnitTest {
@Test
void givenPasswordAndSalt_whenCreateSecreKeyCheckConversion_thenSuccess()
throws NoSuchAlgorithmException, InvalidKeySpecException {
throws NoSuchAlgorithmException, InvalidKeySpecException {
// given
String password = "Baeldung@2021";
String salt = "@$#baelDunG@#^$*";
@@ -28,7 +28,7 @@ public class ConversionClassUtilUnitTest {
@Test
void givenSize_whenCreateSecreKeyCheckConversion_thenSuccess()
throws NoSuchAlgorithmException, InvalidKeySpecException {
throws NoSuchAlgorithmException, InvalidKeySpecException {
// given
int size = 256;

View File

@@ -16,8 +16,8 @@ public class TrustStoreUnitTest {
@Test
public void whenOpeningTrustStore_thenExceptionIsThrown() throws Exception {
KeyStore keyStore = getKeyStore();
InvalidAlgorithmParameterException invalidAlgorithmParameterException =
Assertions.assertThrows(InvalidAlgorithmParameterException.class, () -> new PKIXParameters(keyStore));
InvalidAlgorithmParameterException invalidAlgorithmParameterException = Assertions.assertThrows(InvalidAlgorithmParameterException.class,
() -> new PKIXParameters(keyStore));
Assertions.assertEquals("the trustAnchors parameter must be non-empty", invalidAlgorithmParameterException.getMessage());
}