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.
pull/36455/head
Moritz Halbritter 1 year ago
parent 0588f9bf37
commit 4aaca291de

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@
package org.springframework.boot.env; package org.springframework.boot.env;
import java.util.HexFormat;
import java.util.OptionalInt; import java.util.OptionalInt;
import java.util.OptionalLong; import java.util.OptionalLong;
import java.util.Random; import java.util.Random;
@ -31,7 +32,6 @@ import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.log.LogMessage; import org.springframework.core.log.LogMessage;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
@ -58,6 +58,7 @@ import org.springframework.util.StringUtils;
* @author Dave Syer * @author Dave Syer
* @author Matt Benson * @author Matt Benson
* @author Madhura Bhave * @author Madhura Bhave
* @author Moritz Halbritter
* @since 1.0.0 * @since 1.0.0
*/ */
public class RandomValuePropertySource extends PropertySource<Random> { public class RandomValuePropertySource extends PropertySource<Random> {
@ -136,9 +137,9 @@ public class RandomValuePropertySource extends PropertySource<Random> {
} }
private Object getRandomBytes() { private Object getRandomBytes() {
byte[] bytes = new byte[32]; byte[] bytes = new byte[16];
getSource().nextBytes(bytes); getSource().nextBytes(bytes);
return DigestUtils.md5DigestAsHex(bytes); return HexFormat.of().withLowerCase().formatHex(bytes);
} }
public static void addToEnvironment(ConfigurableEnvironment environment) { public static void addToEnvironment(ConfigurableEnvironment environment) {

@ -37,6 +37,7 @@ import static org.mockito.Mockito.spy;
* *
* @author Dave Syer * @author Dave Syer
* @author Matt Benson * @author Matt Benson
* @author Moritz Halbritter
*/ */
class RandomValuePropertySourceTests { class RandomValuePropertySourceTests {
@ -192,4 +193,9 @@ class RandomValuePropertySourceTests {
RandomValuePropertySource.RANDOM_PROPERTY_SOURCE_NAME, "mockProperties"); RandomValuePropertySource.RANDOM_PROPERTY_SOURCE_NAME, "mockProperties");
} }
@Test
void randomStringIs32CharsLong() {
assertThat(this.source.getProperty("random.string")).asString().hasSize(32);
}
} }

Loading…
Cancel
Save