diff --git a/CHANGELOG.md b/CHANGELOG.md index 1509652ad..e4436e4e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ * 【core 】 DateUtil.beginOfHour(pr#269@Gitee) * 【core 】 MapUtil增加sortByValue(pr#259@Gitee) * 【core 】 TypeUtil修正hasTypeVeriable为hasTypeVariable +* 【core 】 RandomUtil.getRandom改为new SecureRandom,避免阻塞 ### Bug修复 * 【core 】 修复FileUtil.move以及PathUtil.copy等无法自动创建父目录的问题(issue#I2CKTI@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java index 2e77c2818..e9ba1b01d 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java @@ -94,8 +94,25 @@ public class RandomUtil { * @param seed 随机数种子 * @return {@link SecureRandom} * @since 5.5.2 + * @see #createSecureRandom(byte[]) */ public static SecureRandom getSecureRandom(byte[] seed) { + return createSecureRandom(seed); + } + + /** + * 获取SHA1PRNG的{@link SecureRandom},类提供加密的强随机数生成器 (RNG)
+ * 注意:此方法获取的是伪随机序列发生器PRNG(pseudo-random number generator),在Linux下噪声生成时可能造成较长时间停顿。
+ * see: http://ifeve.com/jvm-random-and-entropy-source/ + * + *

+ * 相关说明见:https://stackoverflow.com/questions/137212/how-to-solve-slow-java-securerandom + * + * @param seed 随机数种子 + * @return {@link SecureRandom} + * @since 5.5.8 + */ + public static SecureRandom getSHA1PRNGRandom(byte[] seed) { SecureRandom random; try { random = SecureRandom.getInstance("SHA1PRNG"); @@ -330,7 +347,7 @@ public class RandomUtil { * @return 随机元素 */ public static T randomEle(List list, int limit) { - if (list.size() < limit){ + if (list.size() < limit) { limit = list.size(); } return list.get(randomInt(limit)); @@ -358,7 +375,7 @@ public class RandomUtil { * @since 3.3.0 */ public static T randomEle(T[] array, int limit) { - if (array.length < limit){ + if (array.length < limit) { limit = array.length; } return array[randomInt(limit)];