How to use Stanford PRNG to generate a random string?

I need to create a secure 50 character random string in user browsers.

Looking at sjcl.prng I still have this:

$(document).ready(function () {

    sjcl.random = new sjcl.prng(8);

    sjcl.random.startCollectors();

    $("body").on('mousemove', function() {
        console.log(sjcl.random.getProgress(8));

        if(sjcl.random.isReady(8) === 2) {
            sjcl.random.stopCollectors();
            console.log(sjcl.random.randomWords(5,8));
        }
    });

});

After you move the mouse I get a byte array for some time as follows: [-579285364, 1099191484, 94979086, -1572161987, -570940948].

But what I'm looking for is an 50-character alphanumeric string. My knowledge on this topic is limited, and I'm looking for some help here.

+4
source share
2 answers

Here's how I solved it:

function createRandomString (callback, length) {
  var randomBase64String = '',
  checkReadyness;

  checkReadyness = setInterval(function () {
    console.log(length);
    if(sjcl.random.isReady(10)) {
      while(randomBase64String.length < length) {
        randomInt = sjcl.random.randomWords(1, 10)[0];
        randomBase64String += btoa(randomInt);
      }
      randomBase64String = randomBase64String.substr(0, length);
      callback(randomBase64String);
      clearInterval(checkReadyness);
    }
  }, 1);
}

However, this does not work in older browsers. Because I used window.btoa ().

+5
source
  • 39 , 0..255.

  • Base64. 52 . Javascript Base64.

  • ( , ) .

, Base64: a-z A-Z 0-9 -_ : a-z A-Z 0-9 +/

Base64 . RFC 4648.

+1

Source: https://habr.com/ru/post/1544522/


All Articles