随机数Random和SecureRandom
"Random" objects should be reused
- Bug
- Critical
- Main sources
- owasp-a6
- Available SinceNov 16, 2021
- SonarAnalyzer (Java)
- Constant/issue: 5min
Creating a new Random object each time a random value is needed is inefficient and may produce numbers which are not random depending on the JDK. For better efficiency and randomness, create a single Random, then store, and reuse it.
The Random() constructor tries to set the seed with a distinct value every time. However there is no guarantee that the seed will be random or even uniformly distributed. Some JDK will use the current time as seed, which makes the generated numbers not random at all.
This rule finds cases where a new Random is created each time a method is invoked and assigned to a local random variable.
Noncompliant Code Example
public void doSomethingCommon() {
Random rand = new Random(); // Noncompliant; new instance created with each invocation
int rValue = rand.nextInt();
//...
Compliant Solution
private Random rand = SecureRandom.getInstanceStrong(); // SecureRandom is preferred to Random
public void doSomethingCommon() {
int rValue = this.rand.nextInt();
//...
Exceptions
A class which uses a Random in its constructor or in a static main function and nowhere else will be ignored by this rule.
要修改的代码:
Random ran = new Random();
int num = ran.nextInt(99999);
修改为:
private Random rand; // SecureRandom is preferred to Random
{
try {
rand = SecureRandom.getInstanceStrong();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
int num = this.rand.nextInt(99999);
以上是sonarqube的修改建议,但是发布后遇到了阻塞问题,参考文章如下
于是,我改成了
private static final Random rand = new Random();
不再阻塞
随机数Random和SecureRandom的更多相关文章
- 真伪随机数 ——Random和SecureRandom
Random Random用来创建伪随机数.所谓伪随机数,是指只要给定一个初始的种子,产生的随机数序列是完全一样的. 要生成一个随机数,可以使用nextInt().nextLong().nextFlo ...
- Java如何生成随机数 - Random、ThreadLocalRandom、SecureRandom
Java7 的Random伪随机数和线程安全的ThreadLocalRandom 一.Random伪随机数: Random 类专门用于生成一个伪随机数,它有两个构造器: 一个构造器使用默认的种子(以当 ...
- (五)boost库之随机数random
(五)boost库之随机数random boost库为我们提供了许多的日常随机数生成器: 1.uniform_smallint:在小整数域内的均匀分布 2.uniform_int:在整数域上的均匀分布 ...
- Python 随机数 random
1. Python seed() 函数 seed() 方法改变随机数生成器的种子,可以在调用其他随机模块函数之前调用此函数. seed( )是不能直接访问的,需要导入 random 模块,然后 ...
- [Swift] 随机数 | Random numbers
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- JS对象随机数 random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数。 注意:返回一个大于或等于 0但小于1的符号为正的数值
随机数 random() random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数. 语法: Math.random(); 注意:返回一个大于或等于 0 但小于 1 ...
- 常用内置模块之collections模块、时间模块、随机数random模块
今日内容回顾 目录 今日内容回顾 包的具体使用 编程思想的转变 软件开发目录规范 常用内置模块之collections模块 常用内置模块之时间模块 常用内置模块之随机数random模块 报的具体使用 ...
- 随机数(random)
需求 Random rd=new Random(); 需要十以内的随机数 (0---10) System.out.println((int)((rd.nextDouble()*100)/10)); ...
- shell脚本获取随机数random
用C提供的取随机数的方法srand和rand, 前者是给后者设置随机数种子seed. ; srand(seed); // time(NULL) 通常使用时间做种子 rnd_num = rand(); ...
- Python之数学(math)和随机数(random)
math包包含了最基本的数学运算函数,如果想要更加高级的数学功能,可以使用标准库外的numpy和scipy库,他们不但支持数组和矩阵运算, 还有丰富的数学和物理方程可供使用 random包可以用来生成 ...
随机推荐
- Vulnhub:Five86-2靶机
kali:192.168.111.111 靶机:192.168.111.211 信息收集 端口扫描 nmap -A -v -sV -T5 -p- --script=http-enum 192.168. ...
- 【面试题】XSS攻击是什么?
XSS攻击是什么? 要点: XSS是跨站脚本攻击.向目标网站插入恶意代码.大量用户访问网站时运行恶意脚本获取信息 答: XSS是跨站脚本攻击(Cross Site Scripting),不写为CSS是 ...
- WDA学习(26):Phase Indicator使用
1.19 UI Element:Phase Indicator使用 本实例测试创建Phase Indicator; 运行结果: 1.创建Component,View: V_PHASE_IND; 2.创 ...
- vue element 日期范围选择器限制:只能选今天之前的时间 || 只能选今天之后的时间 || 选取今天往后三天内
举例:只能选今天或者今天之后的时间(如下图) <el-date-picker clearable v-model="form.limitTime" type="da ...
- Spring全家桶(一)之Spring总结
0. Spring常用注解汇总 Spring常用注解汇总 使用注解之前要开启自动扫描功能,其中base-package为需要扫描的包(含子包). <context:component-scan ...
- allure+junit5遇到的一些问题
java+junit5+allure 之前引testng,还比较顺利,见上一篇博客,然后testng的注解和junit不一样,感觉junit5更好用一些,所以尝试java+junit5+allure ...
- libvirtd升级
如果对图形化界面没有诉求,能够使用命令行进行虚拟化相关操作,建议使用系统自 带的libvirt-4.5.0. 由于libvirt-4.5.0代码存在bug,所以在使用virt-manager时会触发一 ...
- iOS第三方库汇总(转)
原文:http://mp.weixin.qq.com/s?__biz=MjM5OTM0MzIwMQ==&mid=2652551221&idx=1&sn=617f4d42bc52 ...
- 使用vue3对数据进行分页展示
要获取用户的滚动位置,可以在末尾添加一列空白节点.每当出现空白时意味着滑倒网页最底部,则进行渲染数据.可以使用getBoundingClientRect来判断是否在页面底部. getBoundingC ...
- Windows 与Docker
https://docs.microsoft.com/zh-cn/windows/wsl/install-manual#step-4---download-the-linux-kernel-updat ...