如何将 ArrayBuffer 的特定范围设置为随机数?
How do I set specific ranges of an ArrayBuffer to random numbers?
我想将 16 字节 ArrayBuffer
的前 29 位设置为 [0, 2^28 - 1]
范围内的随机值,与系统字节顺序。
以下是我的尝试,但这(可能)设置了 all 32 位。我能做些什么?有没有办法做类似 randomNumberHere & 0x1FFFFFFF
?
的事情
let buffer = new ArrayBuffer(16);
let view = new Uint32Array(buffer.slice(0, 4));
window.crypto.getRandomValues(view);
您可以在 ArrayBuffer 上使用按位操作数
buffer & 0xE0000000 ^ window.crypto.getRandomValues(view) & 0x1FFFFFFF
可以使用位运算
function PrintBuffer(buffer){
var dataview = new DataView(buffer, 0);
var dataSize = buffer.byteLength;
var index =0;
var str =""
while( index <dataSize){
str +="_" + dataview.getUint8(index).toString(2).padStart(8, '0');
index++;
}
//console.log(str);
return str;
}
let buffer = new ArrayBuffer(16);
// Copy the values into the array starting at index 3
//const uint8 = new Uint8Array(buffer);
//uint8.set([1, 2, 3,4,5,6,7,8,9,10,11,12,13,14,15,16], 0);
let view = new Uint32Array(buffer.slice(0, 4));
window.crypto.getRandomValues(view);
// Before Update
$('.root').prepend("<strong>Result</strong>.");
console.log(":Before");
console.log("Orignal ArrayBuffer : " + PrintBuffer(buffer));
console.log("Spliced Encrypted ArrayBuffer : " +PrintBuffer(view.buffer));
/* **************************************************
* Bits udate
***************************************************/
const NumberOfBits = 29 // This bit count
const octet =8
const quotient = (NumberOfBits - NumberOfBits % octet) / octet;
const remainder = NumberOfBits % octet;
var encryptedView = new DataView(view.buffer, 0);
var orignalBufferView = new DataView(buffer);
for(var i=0;i<quotient;i++){
this.orignalBufferView.setInt8(i, encryptedView.getUint8(i));
}
if(remainder>0){
// remainder=2;
var oldVal = orignalBufferView.getUint8(quotient);
var newVal = encryptedView.getUint8(quotient);
const maskForOldValue = new Uint8Array([1,3,7,15,31,63,127,255]);
const maskForNewValue = new Uint8Array([128,192,224,240,248,252,254]);
oldVal = oldVal & maskForOldValue[7-remainder];
newVal = newVal & maskForNewValue[remainder-1];
orignalBufferView.setInt8(quotient, oldVal | newVal);
}
/***************************************************
*
*************************************************-*/
//After update
console.log(":After");
console.log("Modified ArrayBuffer : " + PrintBuffer(buffer));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="root">
</div>
这是我所做的。更长一点,但也更直接。
import crypto from 'crypto';
let buffer = new ArrayBuffer(16);
let randomValue = crypto.randomInt(0, (1 << 29) - 1);
let view = new DataView(buffer);
view.setUint32(0, randomValue);
我想将 16 字节 ArrayBuffer
的前 29 位设置为 [0, 2^28 - 1]
范围内的随机值,与系统字节顺序。
以下是我的尝试,但这(可能)设置了 all 32 位。我能做些什么?有没有办法做类似 randomNumberHere & 0x1FFFFFFF
?
let buffer = new ArrayBuffer(16);
let view = new Uint32Array(buffer.slice(0, 4));
window.crypto.getRandomValues(view);
您可以在 ArrayBuffer 上使用按位操作数
buffer & 0xE0000000 ^ window.crypto.getRandomValues(view) & 0x1FFFFFFF
可以使用位运算
function PrintBuffer(buffer){
var dataview = new DataView(buffer, 0);
var dataSize = buffer.byteLength;
var index =0;
var str =""
while( index <dataSize){
str +="_" + dataview.getUint8(index).toString(2).padStart(8, '0');
index++;
}
//console.log(str);
return str;
}
let buffer = new ArrayBuffer(16);
// Copy the values into the array starting at index 3
//const uint8 = new Uint8Array(buffer);
//uint8.set([1, 2, 3,4,5,6,7,8,9,10,11,12,13,14,15,16], 0);
let view = new Uint32Array(buffer.slice(0, 4));
window.crypto.getRandomValues(view);
// Before Update
$('.root').prepend("<strong>Result</strong>.");
console.log(":Before");
console.log("Orignal ArrayBuffer : " + PrintBuffer(buffer));
console.log("Spliced Encrypted ArrayBuffer : " +PrintBuffer(view.buffer));
/* **************************************************
* Bits udate
***************************************************/
const NumberOfBits = 29 // This bit count
const octet =8
const quotient = (NumberOfBits - NumberOfBits % octet) / octet;
const remainder = NumberOfBits % octet;
var encryptedView = new DataView(view.buffer, 0);
var orignalBufferView = new DataView(buffer);
for(var i=0;i<quotient;i++){
this.orignalBufferView.setInt8(i, encryptedView.getUint8(i));
}
if(remainder>0){
// remainder=2;
var oldVal = orignalBufferView.getUint8(quotient);
var newVal = encryptedView.getUint8(quotient);
const maskForOldValue = new Uint8Array([1,3,7,15,31,63,127,255]);
const maskForNewValue = new Uint8Array([128,192,224,240,248,252,254]);
oldVal = oldVal & maskForOldValue[7-remainder];
newVal = newVal & maskForNewValue[remainder-1];
orignalBufferView.setInt8(quotient, oldVal | newVal);
}
/***************************************************
*
*************************************************-*/
//After update
console.log(":After");
console.log("Modified ArrayBuffer : " + PrintBuffer(buffer));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="root">
</div>
这是我所做的。更长一点,但也更直接。
import crypto from 'crypto';
let buffer = new ArrayBuffer(16);
let randomValue = crypto.randomInt(0, (1 << 29) - 1);
let view = new DataView(buffer);
view.setUint32(0, randomValue);