From 4aaca291dec2adc939965c0d334cd90f45ed3c7b Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Tue, 8 Aug 2023 11:07:30 +0200 Subject: [PATCH] Remove MD5 from RandomValuePropertySource Before that change, we get 32 random bytes, and then used MD5 on them to get a hex string. This removes the MD5, we now get 128 bits (output size of MD5) of random bytes directly. --- .../boot/env/RandomValuePropertySource.java | 9 +++++---- .../boot/env/RandomValuePropertySourceTests.java | 6 ++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/RandomValuePropertySource.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/RandomValuePropertySource.java index 7c2f23f631..b01f16fb3d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/RandomValuePropertySource.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/RandomValuePropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.boot.env; +import java.util.HexFormat; import java.util.OptionalInt; import java.util.OptionalLong; import java.util.Random; @@ -31,7 +32,6 @@ import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; import org.springframework.core.log.LogMessage; import org.springframework.util.Assert; -import org.springframework.util.DigestUtils; import org.springframework.util.StringUtils; /** @@ -58,6 +58,7 @@ import org.springframework.util.StringUtils; * @author Dave Syer * @author Matt Benson * @author Madhura Bhave + * @author Moritz Halbritter * @since 1.0.0 */ public class RandomValuePropertySource extends PropertySource { @@ -136,9 +137,9 @@ public class RandomValuePropertySource extends PropertySource { } private Object getRandomBytes() { - byte[] bytes = new byte[32]; + byte[] bytes = new byte[16]; getSource().nextBytes(bytes); - return DigestUtils.md5DigestAsHex(bytes); + return HexFormat.of().withLowerCase().formatHex(bytes); } public static void addToEnvironment(ConfigurableEnvironment environment) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/RandomValuePropertySourceTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/RandomValuePropertySourceTests.java index 77732be5db..1fb9ad176d 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/RandomValuePropertySourceTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/RandomValuePropertySourceTests.java @@ -37,6 +37,7 @@ import static org.mockito.Mockito.spy; * * @author Dave Syer * @author Matt Benson + * @author Moritz Halbritter */ class RandomValuePropertySourceTests { @@ -192,4 +193,9 @@ class RandomValuePropertySourceTests { RandomValuePropertySource.RANDOM_PROPERTY_SOURCE_NAME, "mockProperties"); } + @Test + void randomStringIs32CharsLong() { + assertThat(this.source.getProperty("random.string")).asString().hasSize(32); + } + }