How to generate random value using Window.crypto getRandomValues()

by Anish

Posted on Sunday september 11, 2018

Referefce

This sample chapter extracted from the book, Cryptography for JavaScript Developers.


The Window.crypto read-only property returns the Crypto object associated to the global object. This object allows web pages access to certain cryptographic related services.

Using the Window.crypto property to access the getRandomValues() method.

The Javascript Code

<script>
genRandomNumbers = function getRandomNumbers() {
  if (window.crypto || window.msCrypto) 
  {
  var array = new Uint32Array(10);
  window.crypto.getRandomValues(array);
 
  var randText = document.getElementById("myRandText");
  randText.innerHTML = "The random numbers are: "
  for (var i = 0; i < array.length; i++) {
    randText.innerHTML += array[i] + " \n";
  }
} 
    else throw new Error("Your browser can't generate secure random numbers");
}
</script>

The Html

<p id="myRandText">The random numbers are: </p>
<button type="button" onClick='genRandomNumbers()'>Generate 10 random numbers</button>

The Result

The random numbers are:

Broswer Support for window.crypto

The following browser has the support of window.crypto

enter image description here


Download the sample code here

Thanku for reading !!! Give a Share for Support

Asking for donation sound bad to me, so i'm raising fund from by offering all my Nine book for just $9



python Cryptography Topics
Topics
For Coffee/ Beer/ Amazon Bill and further development of the project Support by Purchasing, The Modern Cryptography CookBook for Just $9 Coupon Price

Kubernetes for DevOps

Hello Dockerfile

Cryptography for Python Developers

Cryptography for JavaScript Developers

Go lang ryptography for Developers

Here