webkitrandom.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true */
  9. 'use strict';
  10. module.exports = WebKitRandomCollector;
  11. var collectEntropy_, globalCrypto_, entropyQuantity_;
  12. function WebKitRandomCollector(
  13. collectEntropyCallback, cryptoApi, entropyQuantity) {
  14. collectEntropy_ = collectEntropyCallback;
  15. globalCrypto_ = cryptoApi;
  16. entropyQuantity_ = entropyQuantity;
  17. }
  18. WebKitRandomCollector.prototype = {
  19. startCollector: function() {
  20. var numPositions = entropyQuantity_ / 32;
  21. // get cryptographically strong entropy in Webkit
  22. var ab = new Uint32Array(numPositions);
  23. globalCrypto_.getRandomValues(ab);
  24. var data = '';
  25. for (var i = 0; i < ab.length; i++) {
  26. data += '' + (ab[i]);
  27. }
  28. collectEntropy_(data, entropyQuantity_);
  29. },
  30. stopCollector: function() {
  31. // just executed once, so no need to execute anything else
  32. return true;
  33. }
  34. };