Skip to content
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

Use better methods for generating entropy #6

Merged
merged 3 commits into from
Aug 22, 2013
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Use crypto.getRandomValues for RNG if available
window.crypto.random is really old and depreciated.  Should use
crypto.getRandomValues instead.  If we are able to generate entropy from
crypto.getRandomValues, we should not add entropy from Math.random, as
it may be unreliable.
  • Loading branch information
ctso committed Aug 20, 2013
commit a9c99261bd6d68caa9e87188db50861e43c9e6ce
22 changes: 12 additions & 10 deletions jsbn/rng.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ if(rng_pool == null) {
rng_pool = new Array();
rng_pptr = 0;
var t;
if(navigator.appName == "Netscape" && navigator.appVersion < "5" && window.crypto) {
// Extract entropy (256 bits) from NS4 RNG if available
var z = window.crypto.random(32);
for(t = 0; t < z.length; ++t)
rng_pool[rng_pptr++] = z.charCodeAt(t) & 255;
}
while(rng_pptr < rng_psize) { // extract some randomness from Math.random()
t = Math.floor(65536 * Math.random());
rng_pool[rng_pptr++] = t >>> 8;
rng_pool[rng_pptr++] = t & 255;
if(window.crypto && window.crypto.getRandomValues) {
// Extract entropy (256 bits) from RNG if available
var randomBits = window.crypto.getRandomValues(new Uint32Array(32));
for (t = 0; t < randomBits.length; ++t)
rng_pool[rng_pptr++] = z[t] & 255;
} else {
// Otherwise, extract some randomness from Math.random()
while(rng_pptr < rng_psize) {
t = Math.floor(65536 * Math.random());
rng_pool[rng_pptr++] = t >>> 8;
rng_pool[rng_pptr++] = t & 255;
}
}
rng_pptr = 0;
rng_seed_time();
Expand Down