Math.random()的加密安全替换方法window.crypto.getRandomValues
Math.random()
window.crypto.getRandomValues
Crypto.getRandomValues() 方法让你可以获取符合密码学要求的安全的随机值。传入参数的数组被随机值填充(在加密意义上的随机)。
为了确保足够的性能,不使用真正的随机数生成器,但是它们正在使用具有足够熵值伪随机数生成器。它所使用的 PRNG 的实现与其他不同,但适用于加密的用途。该实现还需要使用具有足够熵的种子,如系统级熵源。
语法
cryptoObj.getRandomValues(typedArray);
参数
typedArray是一个基于整数的 TypedArray,它可以是 Int8Array、Uint8Array、Int16Array、 Uint16Array、 Int32Array 或者 Uint32Array。在数组中的所有的元素会被随机数重写。(注释:生成的随机数储存在 typedArray 数组上。)
例子
window.crypto.getRandomValues(new Int8Array(2))//Int8Array(2) [44, -3]
window.crypto.getRandomValues(new Uint8Array(2))//Uint8Array(2) [218, 119]
window.crypto.getRandomValues(new Int16Array(2))//Int16Array(2) [24582, -15808]
window.crypto.getRandomValues(new Uint16Array(2))//Uint16Array(2) [55391, 55756]
window.crypto.getRandomValues(new Int32Array(2))//Int32Array(2) [1574608122, -836595554]
window.crypto.getRandomValues(new Uint32Array(2))//Uint32Array(2) [1906545366, 2391348462]
Math.random()的等价表达式:window.crypto.getRandomValues(new Uint8Array(1)) * 0.001
详见:https://developer.mozilla.org/zh-CN/docs/Web/API/RandomSource/getRandomValues
Math.random()的加密安全替换方法window.crypto.getRandomValues的更多相关文章
- 通过window.crypto.getRandomValues获得一个大于零的随机数
window.crypto.getRandomValues(new Uint32Array(1))[0]; 浏览器支持情况如下: IE: no IE Mobile: no Firefox24+ Fir ...
- java中random的几个方法的使用Math.random()和random().
random java中我们有时候也需要使用使用random来产生随机数,下面我来简单的介绍下java中random的使用方法 第一种:Math.random() public static doub ...
- Random.nextInt()替换Math.random()
在项目中使用哪个随机数 文章参考 http://liukai.iteye.com/blog/433718 今天用了find bugs后查出来了个问题 Google了下 发现 Random.nextin ...
- Math.random 随机数方法
随机取数方法 Math.random() 表示0到1之间随机取一个数 <x< 小数 Math.random()* 表示0<x< parseInt(Math.random()*) ...
- Random类和Math.random()方法
一.Random类的定义Random类位于 java.util 包中,主要用于生成伪 随机数Random类将 种子数 作为随机算法的起源数字,计算生成伪随机数,其与生成的随机数字的区间无关创建Rand ...
- 生成随机数的几种方法、Math.random()随机数的生成、Random()的使用
第一种方法使用:System.currentTimeMillis(); final long l = System.currentTimeMillis(); final int rs = (int) ...
- javascript 中根据sort 方法随机数组 (Math.random)
var arr = [1,2,3,4,5,6,7,8,9,10]; function Arandom(a,b){ return (Math.random() > 0.5) ? 1 : -1;; ...
- Java利用Math.random()方法随机生成A-Z的字符
package reverse; import java.text.DecimalFormat; public class Reverse { public static void main(Stri ...
- Math.random引发的骗术,绝对是用随机数骗前端妹纸的最佳方法
我觉得今天我运气特好,今天我们来赌一赌,我们来搞个随机数,Math.floor(Math.random() * 10),如果这个数等于0到7,这个月的饭,我全请了,如果是8或9,你就请一个礼拜成不?于 ...
随机推荐
- windows powershell学习
PowerShell,从名字可以知道,他首先是一个shell,shell的意思就是和Linux的bash等一样.和原来的cmd一样就是在里边敲命令(可执行文件)使用: 而Power就意味他是一个功能强 ...
- MQTT教學(二):安裝MQTT伺服器Mosquitto,Windows系統篇
http://swf.com.tw/?p=1005 「認識MQTT」文章提到,MQTT的訊息全都透過稱為代理人(broker)的伺服器交流.本文將說明頗受歡迎的開放原始碼MQTT伺服器Mosquitt ...
- SVN创建分支/合并/切换使用
原文地址:https://blog.csdn.net/lisq037/article/details/17501327 最近接项目要求,要在svn主干上创建分支,用分支来进行程序的bug修改,而主干上 ...
- Oracle数据库启动报错,找不到数据文件(ORA-01157和ORA-01110)
数据库报了ORA-01157和ORA-01110错误,提示找不到一个数据文件. 1.启动数据库报错 在启动数据库过程中,报了ORA-01157和ORA-01110错误,提示找不到数据文件. SQL&g ...
- Docs-.NET-C#-指南-语言参考-关键字-值类型-:浮点数值类型
ylbtech-Docs-.NET-C#-指南-语言参考-关键字-值类型-:浮点数值类型 1.返回顶部 1. 浮点数值类型(C# 引用) 2019/10/22 “浮点类型”是“简单类型”的子集,可以使 ...
- Leetcode: Campus Bikes II
On a campus represented as a 2D grid, there are N workers and M bikes, with N <= M. Each worker a ...
- ubuntu 关于curses头文件问题
执行编译gcc -o badterm badterm.c -lcurses后报错情报如下:term.h: 没有那个文件或目录curses.h: 没有那个文件或目录很明显,程序找不到term.h和cur ...
- opencv4 mask_rcnn模型调(c++)
昨天有人问我关于调用mask_rcnn模型的问题,忽然想到最近三个月都没用opencv调用训练好的mask_rcnn模型了,今晚做个尝试,所以重新编译了 opencv4,跑个案例试试 #include ...
- ES6深入浅出-7 新版的类(上集)-2.介绍JS中的类
声明对象原型,公有属性. obj对象,它用一个属性__proto__记录了自己的原型 改掉它的原型为公有属性.那么obj这个对象及有了hi的方法.因为obj自己没有hi.那么就去自己的原型上去找了. ...
- linux用户解锁
pam_tally2 --user=username #查看 pam_tally2 --user=username --reset #重置