diff --git a/core-java-modules/core-java-security-3/src/main/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtil.java b/core-java-modules/core-java-security-3/src/main/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtil.java index 5ff1b7cb9d..8fdf682666 100644 --- a/core-java-modules/core-java-security-3/src/main/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtil.java +++ b/core-java-modules/core-java-security-3/src/main/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtil.java @@ -13,41 +13,41 @@ import javax.crypto.spec.SecretKeySpec; public class ConversionClassUtil { - /* Generating Secret key */ + /* Generating Secret key */ - // Generating Secret Key using KeyGenerator class with 256 - public static SecretKey generateKey(int n) throws NoSuchAlgorithmException { - KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); - keyGenerator.init(n); - SecretKey originalKey = keyGenerator.generateKey(); - return originalKey; - } + // Generating Secret Key using KeyGenerator class with 256 + public static SecretKey generateKey(int n) throws NoSuchAlgorithmException { + KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); + keyGenerator.init(n); + SecretKey originalKey = keyGenerator.generateKey(); + return originalKey; + } - // Generating Secret Key using password and salt - public static SecretKey getKeyFromPassword(String password, String salt) - 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"); - return originalKey; - } + // Generating Secret Key using password and salt + public static SecretKey getKeyFromPassword(String password, String salt) + 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"); + return originalKey; + } - /* Converting Secret key into String */ - public static String convertSecretKeyToString(SecretKey secretKey) throws NoSuchAlgorithmException { - // Converting the Secret Key into byte array - byte[] rawData = secretKey.getEncoded(); - // Getting String - Base64 encoded version of the Secret Key - String encodedKey = Base64.getEncoder().encodeToString(rawData); - return encodedKey; - } + /* Converting Secret key into String */ + public static String convertSecretKeyToString(SecretKey secretKey) throws NoSuchAlgorithmException { + // Converting the Secret Key into byte array + byte[] rawData = secretKey.getEncoded(); + // Getting String - Base64 encoded version of the Secret Key + String encodedKey = Base64.getEncoder().encodeToString(rawData); + return encodedKey; + } - /* Converting String into Secret key into */ - public static SecretKey convertStringToSecretKeyto(String encodedKey) { - // Decoding the Base64 encoded string into byte array - byte[] decodedKey = Base64.getDecoder().decode(encodedKey); - // Rebuilding the Secret Key using SecretKeySpec Class - SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); - return originalKey; - } + /* Converting String into Secret key into */ + public static SecretKey convertStringToSecretKeyto(String encodedKey) { + // Decoding the Base64 encoded string into byte array + byte[] decodedKey = Base64.getDecoder().decode(encodedKey); + // Rebuilding the Secret Key using SecretKeySpec Class + SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); + return originalKey; + } } diff --git a/core-java-modules/core-java-security-3/src/test/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtilUnitTest.java b/core-java-modules/core-java-security-3/src/test/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtilUnitTest.java index 29c8ba9fd0..7a912dbf26 100644 --- a/core-java-modules/core-java-security-3/src/test/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtilUnitTest.java +++ b/core-java-modules/core-java-security-3/src/test/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtilUnitTest.java @@ -10,35 +10,35 @@ import org.junit.jupiter.api.Test; public class ConversionClassUtilUnitTest { - @Test - void givenPasswordAndSalt_whenCreateSecreKeyCheckConversion_thenSuccess() - throws NoSuchAlgorithmException, InvalidKeySpecException { - // given - String password = "Baeldung@2021"; - String salt = "@$#baelDunG@#^$*"; + @Test + void givenPasswordAndSalt_whenCreateSecreKeyCheckConversion_thenSuccess() + throws NoSuchAlgorithmException, InvalidKeySpecException { + // given + String password = "Baeldung@2021"; + String salt = "@$#baelDunG@#^$*"; - // when - SecretKey encodedKey = ConversionClassUtil.getKeyFromPassword(password, salt); - String encodedString = ConversionClassUtil.convertSecretKeyToString(encodedKey); - SecretKey decodeKey = ConversionClassUtil.convertStringToSecretKeyto(encodedString); + // when + SecretKey encodedKey = ConversionClassUtil.getKeyFromPassword(password, salt); + String encodedString = ConversionClassUtil.convertSecretKeyToString(encodedKey); + SecretKey decodeKey = ConversionClassUtil.convertStringToSecretKeyto(encodedString); - // then - Assertions.assertEquals(encodedKey, decodeKey); - } + // then + Assertions.assertEquals(encodedKey, decodeKey); + } - @Test - void givenSize_whenCreateSecreKeyCheckConversion_thenSuccess() - throws NoSuchAlgorithmException, InvalidKeySpecException { - // given - int size = 256; + @Test + void givenSize_whenCreateSecreKeyCheckConversion_thenSuccess() + throws NoSuchAlgorithmException, InvalidKeySpecException { + // given + int size = 256; - // when - SecretKey encodedKey = ConversionClassUtil.generateKey(size); - String encodedString = ConversionClassUtil.convertSecretKeyToString(encodedKey); - SecretKey decodeKey = ConversionClassUtil.convertStringToSecretKeyto(encodedString); + // when + SecretKey encodedKey = ConversionClassUtil.generateKey(size); + String encodedString = ConversionClassUtil.convertSecretKeyToString(encodedKey); + SecretKey decodeKey = ConversionClassUtil.convertStringToSecretKeyto(encodedString); - // then - Assertions.assertEquals(encodedKey, decodeKey); - } + // then + Assertions.assertEquals(encodedKey, decodeKey); + } } diff --git a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/MaxHTTPHeaderSizeConfig.java b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/MaxHTTPHeaderSizeConfig.java index 5a928d8935..0a71f914db 100644 --- a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/MaxHTTPHeaderSizeConfig.java +++ b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/MaxHTTPHeaderSizeConfig.java @@ -8,8 +8,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @ComponentScan({ "com.baeldung.sampleapp.web" }) public class MaxHTTPHeaderSizeConfig implements WebMvcConfigurer { - public MaxHTTPHeaderSizeConfig() { - super(); - } + public MaxHTTPHeaderSizeConfig() { + super(); + } } diff --git a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/MaxHttpHeaderSizeController.java b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/MaxHttpHeaderSizeController.java index 16ee4515a5..0c55f88fa1 100644 --- a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/MaxHttpHeaderSizeController.java +++ b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/MaxHttpHeaderSizeController.java @@ -9,9 +9,9 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping(value = "/request-header-test") public class MaxHttpHeaderSizeController { - @GetMapping - public boolean testMaxHTTPHeaderSize(@RequestHeader(value = "token") String token) { - return true; - } + @GetMapping + public boolean testMaxHTTPHeaderSize(@RequestHeader(value = "token") String token) { + return true; + } } diff --git a/spring-boot-modules/spring-boot-runtime/src/test/java/com/baeldung/web/controller/MaxHttpHeaderSizeControllerIntegrationTest.java b/spring-boot-modules/spring-boot-runtime/src/test/java/com/baeldung/web/controller/MaxHttpHeaderSizeControllerIntegrationTest.java index 209e5497c4..9b839f34a0 100644 --- a/spring-boot-modules/spring-boot-runtime/src/test/java/com/baeldung/web/controller/MaxHttpHeaderSizeControllerIntegrationTest.java +++ b/spring-boot-modules/spring-boot-runtime/src/test/java/com/baeldung/web/controller/MaxHttpHeaderSizeControllerIntegrationTest.java @@ -23,26 +23,26 @@ import com.baeldung.sampleapp.config.WebConfig; @WebAppConfiguration public class MaxHttpHeaderSizeControllerIntegrationTest { - private MockMvc mockMvc; + private MockMvc mockMvc; - @Autowired - private WebApplicationContext webApplicationContext; + @Autowired + private WebApplicationContext webApplicationContext; - @Before - public void setUp() { - mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); - } + @Before + public void setUp() { + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); + } - @Test - public void givenTokenWithLessThan8KBLegth_whenSendGetRequest_thenReturnsOK() throws Exception { - mockMvc.perform(get("/request-header-test").contentType(MediaType.APPLICATION_JSON_VALUE) - .with(httpBasic("user", "password")).header("token", "token")).andExpect(status().isOk()); - } + @Test + public void givenTokenWithLessThan8KBLegth_whenSendGetRequest_thenReturnsOK() throws Exception { + mockMvc.perform(get("/request-header-test").contentType(MediaType.APPLICATION_JSON_VALUE) + .with(httpBasic("user", "password")).header("token", "token")).andExpect(status().isOk()); + } - @Test - public void givenTokenIsMissingInHeade_whenSendGetRequest_thenThrowsBadRequest() throws Exception { - mockMvc.perform(get("/request-header-test").contentType(MediaType.APPLICATION_JSON_VALUE) - .with(httpBasic("user", "password"))).andExpect(status().isBadRequest()); - } + @Test + public void givenTokenIsMissingInHeade_whenSendGetRequest_thenThrowsBadRequest() throws Exception { + mockMvc.perform(get("/request-header-test").contentType(MediaType.APPLICATION_JSON_VALUE) + .with(httpBasic("user", "password"))).andExpect(status().isBadRequest()); + } } diff --git a/spring-boot-modules/spring-boot-runtime/src/test/java/com/baeldung/web/controller/MaxHttpHeaderSizeControllerLiveTest.java b/spring-boot-modules/spring-boot-runtime/src/test/java/com/baeldung/web/controller/MaxHttpHeaderSizeControllerLiveTest.java index 14e1913082..9c7e7c9029 100644 --- a/spring-boot-modules/spring-boot-runtime/src/test/java/com/baeldung/web/controller/MaxHttpHeaderSizeControllerLiveTest.java +++ b/spring-boot-modules/spring-boot-runtime/src/test/java/com/baeldung/web/controller/MaxHttpHeaderSizeControllerLiveTest.java @@ -25,29 +25,29 @@ import com.baeldung.sampleapp.config.MaxHTTPHeaderSizeConfig; // Start MaxHttpHeaderSizeController Spring Boot App(MainApplication) first public class MaxHttpHeaderSizeControllerLiveTest { - @Test(expected = HttpClientErrorException.class) - public void givenTokenWithGreaterThan8KBLegth_whenSendGetRequest_thenThrowsBadRequest() throws Exception { - final String url = "http://localhost:8080/request-header-test"; - HttpHeaders headers = new HttpHeaders(); - headers.set("token", readRandomStringFromFile()); + @Test(expected = HttpClientErrorException.class) + public void givenTokenWithGreaterThan8KBLegth_whenSendGetRequest_thenThrowsBadRequest() throws Exception { + final String url = "http://localhost:8080/request-header-test"; + HttpHeaders headers = new HttpHeaders(); + headers.set("token", readRandomStringFromFile()); - HttpEntity entity = new HttpEntity(headers); - final ResponseEntity response = new RestTemplate().exchange(url, HttpMethod.GET, entity, String.class); - } + HttpEntity entity = new HttpEntity(headers); + final ResponseEntity response = new RestTemplate().exchange(url, HttpMethod.GET, entity, String.class); + } - static String readRandomStringFromFile() throws IOException { - BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/randomSringForheader.txt")); - StringBuilder stringBuilder = new StringBuilder(); - String line = null; - String ls = System.getProperty("line.separator"); - while ((line = reader.readLine()) != null) { - stringBuilder.append(line); - stringBuilder.append(ls); - } - stringBuilder.deleteCharAt(stringBuilder.length() - 1); - reader.close(); - String content = stringBuilder.toString(); - return content; - } + static String readRandomStringFromFile() throws IOException { + BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/randomSringForheader.txt")); + StringBuilder stringBuilder = new StringBuilder(); + String line = null; + String ls = System.getProperty("line.separator"); + while ((line = reader.readLine()) != null) { + stringBuilder.append(line); + stringBuilder.append(ls); + } + stringBuilder.deleteCharAt(stringBuilder.length() - 1); + reader.close(); + String content = stringBuilder.toString(); + return content; + } }