Browse Source

change SecureRandom

Looly 5 years ago
parent
commit
e0ac328b19
2 changed files with 20 additions and 2 deletions
  1. 1 0
      CHANGELOG.md
  2. 19 2
      hutool-core/src/main/java/cn/hutool/core/util/RandomUtil.java

+ 1 - 0
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)

+ 19 - 2
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)<br>
+	 * 注意:此方法获取的是伪随机序列发生器PRNG(pseudo-random number generator),在Linux下噪声生成时可能造成较长时间停顿。<br>
+	 * see: http://ifeve.com/jvm-random-and-entropy-source/
+	 *
+	 * <p>
+	 * 相关说明见: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> T randomEle(List<T> 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> T randomEle(T[] array, int limit) {
-		if (array.length < limit){
+		if (array.length < limit) {
 			limit = array.length;
 		}
 		return array[randomInt(limit)];