-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
web3.utils.randomHex does not produce consistent length strings #1490
Comments
Same issue here.
And if the number (ranged 0 - 255) is less than 16, Given that the number of bytes is large enough, the probability to have missing This is a serious problem, since it greatly decreases the probability of having digit I've tested calling
As you can see, the probability of having Don't know how to open PR for the |
Work Around `generateNew(){
}` |
@theAngryLoop |
@nnkken, var randomBytes = cryptoLib.getRandomValues(new Uint8Array(size));
var returnValue = '0x'+ Array.from(randomBytes).map(function(arr){ return arr.toString(16); }).join(''); I see this code here: https://github.com/pwall-org/pwall/blob/master/js/web3-eth-accounts.js This not working in old browsers. var randomBytes = cryptoLib.getRandomValues(new Uint8Array(size));
//console.log(randomBytes); //[130, 247, 4, 241, 77, 175, 138, 55, ...]
/*
//there was been an error for me:
//when browser trying to call Array.from() function
//Uncaught TypeError: undefined is not a function
var returnValue = '0x' + Array.from(randomBytes).map(function (arr) {
return arr.toString(16);
}).join('');
//console.log(returnValue); //may be 0xHEXSTRING, but not working and give me a throw error
*/
//so I used this code
var returnValue = '0x';
for(var i=0;i<=randomBytes.length-1;i++){
returnValue += randomBytes[i].toString(16);
}
//console.log(returnValue);//0x + hex string working normally, and no any errors.
//... continue script... So now you can fix this to ensure backward compatibility. |
|
@rstormsf, this need to include largest functions: |
This seems to work correctly: const cryptoObj = window.crypto || window.msCrypto; // for IE 11
export function getRandomHex(size) {
const randomBytes = cryptoObj.getRandomValues(new Uint8Array(size));
let returnValue = '0x';
for (let i = 0; i <= randomBytes.length - 1; i += 1) {
let bStr = randomBytes[i].toString(16);
if (bStr.length === 1) {
bStr = `0${bStr}`;
}
returnValue += bStr;
}
return returnValue;
} |
On Chrome 65 (OSX) using web3 1.0.0-beta.33, it seems web3.utils.randomHex does not produce consistent length hex strings. Also tested on Firefox.
The text was updated successfully, but these errors were encountered: