之前写随机数的时候一直用SecureRandom.getInstanceStrong()方法生成SecureRandom实例,进而调用其各种next方法。突然有一次,发现next方法卡住了, 每一次调用都需要四五分钟。google之,发现网上也有很多其他开发人员在反馈这个问题,有的说在启动的时候提案加    。但是有时候项目不是自己部署的,不是你想用什么命令就用什么命令,想替换什么文件就替换什么文件,比如某安某寿。研究SecureRandom.getInstanceStrong()方法的源码,发现最终调用的是SecureRandom.getInstance()方法,getInstance()方法的入参是Security.getProperty("securerandom.strongAlgorithms")的返回值。经测试,Security.getProperty("securerandom.strongAlgorithms")的返回值在不同平台是不一样。在windows jdk中,返回值是Windows-PRNG:SunMSCAPI,SHA1PRNG:SUN,进SecureRandom.getInstanceStrong()方法,发现最终调用的是SecureRandom.getInstance("Windows-PRNG", "SunMSCAPI"),即算法algorithm是Windows-PRNG,provider是SunMSCAPI。在mac jdk中,返回值是NativePRNGBlocking:SUN,最终调用的是SecureRandom.getInstance("NativePRNGBlocking", "SUN")。在linux jdk中,返回值是。

研究Security的getProperty(String key)静态方法:

读取的是JAVA_HOME/jre/lib/security目录中的java.security属性文件中的内容。可以很容易的在各平台看到securerandom.strongAlgorithms的值。

如果不用SecureRandom.getInstanceStrong()方法的话,可以用什么呢?可以用new SecureRandom(),也可以用getInstance()方法的一堆重载来生成SecureRandom实例。

new SecureRandom(),最终调用的是SecureRandom.getInstance("SHA1PRNG")。

在windows平台jdk的java.security文件中,有这样一段描述:

# Sun Provider SecureRandom seed source.
#
# Select the primary source of seed data for the "SHA1PRNG" and
# "NativePRNG" SecureRandom implementations in the "Sun" provider.
# (Other SecureRandom implementations might also use this property.)
#
# On Unix-like systems (for example, Solaris/Linux/MacOS), the
# "NativePRNG" and "SHA1PRNG" implementations obtains seed data from
# special device files such as file:/dev/random.
#
# On Windows systems, specifying the URLs "file:/dev/random" or
# "file:/dev/urandom" will enable the native Microsoft CryptoAPI seeding
# mechanism for SHA1PRNG.
#
# By default, an attempt is made to use the entropy gathering device
# specified by the "securerandom.source" Security property. If an
# exception occurs while accessing the specified URL:
#
# SHA1PRNG:
# the traditional system/thread activity algorithm will be used.
#
# NativePRNG:
# a default value of /dev/random will be used. If neither
# are available, the implementation will be disabled.
# "file" is the only currently supported protocol type.
#
# The entropy gathering device can also be specified with the System
# property "java.security.egd". For example:
#
# % java -Djava.security.egd=file:/dev/random MainClass
#
# Specifying this System property will override the
# "securerandom.source" Security property.
#
# In addition, if "file:/dev/random" or "file:/dev/urandom" is
# specified, the "NativePRNG" implementation will be more preferred than
# SHA1PRNG in the Sun provider.
#
securerandom.source=file:/dev/random

#
# A list of known strong SecureRandom implementations.
#
# To help guide applications in selecting a suitable strong
# java.security.SecureRandom implementation, Java distributions should
# indicate a list of known strong implementations using the property.
#
# This is a comma-separated list of algorithm and/or algorithm:provider
# entries.
#
securerandom.strongAlgorithms=Windows-PRNG:SunMSCAPI,SHA1PRNG:SUN

在mac平台jdk的java.security文件中,描述如下:

# Sun Provider SecureRandom seed source.

#

# Select the primary source of seed data for the "SHA1PRNG" and

# "NativePRNG" SecureRandom implementations in the "Sun" provider.

# (Other SecureRandom implementations might also use this property.)

#

# On Unix-like systems (for example, Solaris/Linux/MacOS), the

# "NativePRNG" and "SHA1PRNG" implementations obtains seed data from

# special device files such as file:/dev/random.

#

# On Windows systems, specifying the URLs "file:/dev/random" or

# "file:/dev/urandom" will enable the native Microsoft CryptoAPI seeding

# mechanism for SHA1PRNG.

#

# By default, an attempt is made to use the entropy gathering device

# specified by the "securerandom.source" Security property.  If an

# exception occurs while accessing the specified URL:

#

#     SHA1PRNG:

#         the traditional system/thread activity algorithm will be used.

#

#     NativePRNG:

#         a default value of /dev/random will be used.  If neither

#         are available, the implementation will be disabled.

#         "file" is the only currently supported protocol type.

#

# The entropy gathering device can also be specified with the System

# property "java.security.egd". For example:

#

#   % java -Djava.security.egd=file:/dev/random MainClass

#

# Specifying this System property will override the

# "securerandom.source" Security property.

#

# In addition, if "file:/dev/random" or "file:/dev/urandom" is

# specified, the "NativePRNG" implementation will be more preferred than

# SHA1PRNG in the Sun provider.

#

securerandom.source=file:/dev/random

#

# A list of known strong SecureRandom implementations.

#

# To help guide applications in selecting a suitable strong

# java.security.SecureRandom implementation, Java distributions should

# indicate a list of known strong implementations using the property.

#

# This is a comma-separated list of algorithm and/or algorithm:provider

# entries.

#

securerandom.strongAlgorithms=NativePRNGBlocking:SUN

在linux平台jdk的java.security文件中,描述如下:

# Sun Provider SecureRandom seed source.

#

# Select the primary source of seed data for the "SHA1PRNG" and

# "NativePRNG" SecureRandom implementations in the "Sun" provider.

# (Other SecureRandom implementations might also use this property.)

#

# On Unix-like systems (for example, Solaris/Linux/MacOS), the

# "NativePRNG" and "SHA1PRNG" implementations obtains seed data from

# special device files such as file:/dev/random.

#

# On Windows systems, specifying the URLs "file:/dev/random" or

# "file:/dev/urandom" will enable the native Microsoft CryptoAPI seeding

# mechanism for SHA1PRNG.

#

# By default, an attempt is made to use the entropy gathering device

# specified by the "securerandom.source" Security property.  If an

# exception occurs while accessing the specified URL:

#

#     SHA1PRNG:

#         the traditional system/thread activity algorithm will be used.

#

#     NativePRNG:

#         a default value of /dev/random will be used.  If neither

#         are available, the implementation will be disabled.

#         "file" is the only currently supported protocol type.

#

# The entropy gathering device can also be specified with the System

# property "java.security.egd". For example:

#

#   % java -Djava.security.egd=file:/dev/random MainClass

#

# Specifying this System property will override the

# "securerandom.source" Security property.

#

# In addition, if "file:/dev/random" or "file:/dev/urandom" is

# specified, the "NativePRNG" implementation will be more preferred than

# SHA1PRNG in the Sun provider.

#

securerandom.source=file:/dev/random

#

# A list of known strong SecureRandom implementations.

#

# To help guide applications in selecting a suitable strong

# java.security.SecureRandom implementation, Java distributions should

# indicate a list of known strong implementations using the property.

#

# This is a comma-separated list of algorithm and/or algorithm:provider

# entries.

#

securerandom.strongAlgorithms=NativePRNGBlocking:SUN

在commons-lang3.jar包中,提供了两个随机工具类,RandomUtils和RandomStringUtils,可以使用。RandomUtils用于生成一个随机数字,而RandomStringUtils主要是生成一个随机字符串。这俩工具类底层利用的都是Random类,而没有用SecureRandom。

SecureRandom的坑的更多相关文章

  1. 使用 SecureRandom 产生随机数采坑记录

    公众号「码海」欢迎关注 背景 我们的项目工程里经常在每个函数需要用到 Random 的地方定义一下 Random 变量(如下) public void doSomethingCommon() { Ra ...

  2. Android应用安全开发之浅谈加密算法的坑

      <Android应用安全开发之浅谈加密算法的坑> 作者:阿里移动安全@伊樵,@舟海 阿里聚安全,一站式解决应用开发安全问题     Android开发中,难免会遇到需要加解密一些数据内 ...

  3. [解决]Linux Tomcat启动慢--Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [236,325] milliseconds

    一.背景 今天部署项目到tomcat,执行./startup.sh命令之后,访问项目迟迟加载不出来,查看日志又没报错(其实是我粗心了,当时tomcat日志还没打印完),一开始怀疑是阿里云主机出现问题, ...

  4. 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密

    你真的了解字典(Dictionary)吗?   从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...

  5. 【解决】Linux Tomcat启动慢--Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [236,325] milliseconds

    一.背景 今天部署项目到tomcat,执行./startup.sh命令之后,访问项目迟迟加载不出来,查看日志又没报错(其实是我粗心了,当时tomcat日志还没打印完),一开始怀疑是阿里云主机出现问题, ...

  6. 当年用httpclient时踩过的那些坑

    一.前言 httpclient是java开发中最常用的工具之一,通常大家会使用其中比较基础的api去调用远程.长期开发爬虫,会接触httpclient不常用的api,同时会遇到各式各样的坑,本文将总结 ...

  7. 服务器端 CentOS 下配置 JDK 和 Tonmcat 踩坑合集

    一.配置 JDK 时,在 /etc/profile 文件下配置环境变量,添加   #java environment export JAVA_HOME=/usr/java/jdk- export CL ...

  8. 微信公众号支付备忘及填坑之路-java

    一.背景 最近公司给第三方开发了一个公众号,其中最重要的功能是支付,由于是第一次开发,遇到的坑特别的多,截止我写博客时,支付已经完成,在这里我把遇到的坑记录一下(不涉及退款).不得不吐槽一下,腾讯这么 ...

  9. 如何一步一步用DDD设计一个电商网站(九)—— 小心陷入值对象持久化的坑

    阅读目录 前言 场景1的思考 场景2的思考 避坑方式 实践 结语 一.前言 在上一篇中(如何一步一步用DDD设计一个电商网站(八)—— 会员价的集成),有一行注释的代码: public interfa ...

随机推荐

  1. 手写BP(反向传播)算法

    BP算法为深度学习中参数更新的重要角色,一般基于loss对参数的偏导进行更新. 一些根据均方误差,每层默认激活函数sigmoid(不同激活函数,则更新公式不一样) 假设网络如图所示: 则更新公式为: ...

  2. 双系统正确卸载Ubuntu系统

    双系统正确卸载Ubuntu系统  安装系统后由于显卡驱动问题,无法开机,从而只能卸载重装,重装过程如下. 第一步:下载需要的工具包,这里我用的是MBRfix, 可以直接从我分享的网盘链接下载,密码gw ...

  3. scrapy 启动

    虚拟环境安装好了之后,scrapy 框架安装好了以后: workon article_spider   (项目名称) scrapy startproject  Article Spider 工程目录 ...

  4. poj1011(DFS+剪枝)

    题目链接:https://vjudge.net/problem/POJ-1011 题意:给定n(<=64)条木棍的长度(<=50),将这些木棍刚好拼成长度一样的若干条木棍,求拼出的可能的最 ...

  5. Spring(六)--Spring配置文件之间的关系

    Spring配置文件之间的关系 1.需要的实体类 2.需要的xml文件 3.测试类 未完待续!!!

  6. 中值滤波器(平滑空间滤波器)基本原理及Python实现

    1. 基本原理 一种典型的非线性滤波器就是中值滤波器,它使用像素的一个领域内的灰度的中值来代替该像素的值.中值滤波器通常是处理椒盐噪声的一种有效的手段. 2. 测试结果 图源自skimage 3. 代 ...

  7. c++中的四种智能指针

    c++中的四种智能指针 写惯了python,golang再来写c++总觉得头大,很大一个原因就是他没有一个GC机制. 不过c++中提供了智能指针,也不是不能用,李姐万岁! auto_ptr, shar ...

  8. AGC015E Mr.Aoki Incubator

    atcoder luogu 首先可以考虑给一个人\(A\)染色.其他人被染色,要么被本来在后面的速度更快的人染色,要么被在前面的更慢的人染色.然后假设一个速度比最开始那个人慢的人\(B\)最后被染色了 ...

  9. python实现建造者模式

    python实现建造者模式 前言 无论是在现实世界中还是在软件系统中,都存在一些复杂的对象,它们拥有多个组成部分,如汽车,它包括车轮.方向盘.发送机等各种部件.而对于大多数用户而言,无须知道这些部件的 ...

  10. CentOS 7.4下源码编译安装配置LAMP环境详解

    CentOS 7.4搭建LAMP,LAMP:Linux.Apache.MySQL.PHP. 目录:第一部分 准备工作第二部分 安装Apache服务第三部分 安装MySQL服务第四部分 搭建PHP运行环 ...