getrandomvalues.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. /*!
  2. {
  3. "name": "getRandomValues",
  4. "property": "getrandomvalues",
  5. "caniuse": "getrandomvalues",
  6. "tags": ["crypto"],
  7. "authors": ["komachi"],
  8. "notes": [{
  9. "name": "W3C Editor’s Draft Spec",
  10. "href": "https://w3c.github.io/webcrypto/#Crypto-interface-methods"
  11. }],
  12. "polyfills": ["polycrypt"]
  13. }
  14. !*/
  15. /* DOC
  16. Detects support for the window.crypto.getRandomValues method for generating cryptographically secure random numbers
  17. */
  18. define(['Modernizr', 'prefixed', 'is'], function(Modernizr, prefixed, is) {
  19. // In Safari <=5.0 `window.crypto` exists (for some reason) but is `undefined`, so we have to check
  20. // it’s truthy before checking for existence of `getRandomValues`
  21. var crypto = prefixed('crypto', window);
  22. var supportsGetRandomValues;
  23. // Safari 6.0 supports crypto.getRandomValues, but does not return the array,
  24. // which is required by the spec, so we need to actually check.
  25. if (crypto && 'getRandomValues' in crypto && 'Uint32Array' in window) {
  26. var array = new Uint32Array(10);
  27. var values = crypto.getRandomValues(array);
  28. supportsGetRandomValues = values && is(values[0], 'number');
  29. }
  30. Modernizr.addTest('getrandomvalues', !!supportsGetRandomValues);
  31. });