window.crypto.getRandomValues 伪造实现

发布时间 2023-07-07 08:41:08作者: AngDH

 

 

 

 

function getRandomValues(array) {
        let rand_max = 256;
     
  
if (array instanceof Uint8Array || array instanceof Int8Array) {
rand_max = 256;
}else if (array instanceof Uint16Array || array instanceof Int16Array){
rand_max = 65536;
}else if (array instanceof Uint32Array || array instanceof Int32Array){
rand_max = 4294967296;
};

for (let i = 0; i < array.length; i++) { array[i] = getRandomInt(0, rand_max); } }; function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; };
const randomArray = new Uint8Array(4);
getRandomValues(randomArray);
console.log(randomArray);