testentropycollector.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. /** A mock entropy collector for test purposes */
  9. /*jshint node:true */
  10. 'use strict';
  11. module.exports = TestEntropyCollector;
  12. /**
  13. * The test entropy collector allows direct assigning of its data and entropy
  14. * counter. The collector is set to update entropy with the current data every
  15. * half second.
  16. *
  17. * @param {any} collectEntropyCallback
  18. */
  19. function TestEntropyCollector(collectEntropyCallback) {
  20. var that = this;
  21. var collectionTimer_;
  22. var collectEntropyCallback_ = collectEntropyCallback;
  23. this.entropyCounter = 0;
  24. this.data = '';
  25. var previousCounter_ = 0;
  26. var previousData_ = '';
  27. this.startCollector = function() {
  28. collectionTimer_ = setInterval(function() {
  29. // Only call the entropy collector if data has changed.
  30. if ((that.data !== previousData_) ||
  31. (that.entropyCounter !== previousCounter_)) {
  32. console.log(
  33. 'Collecting test entropy -- counter is now ' + that.entropyCounter);
  34. collectEntropyCallback_(that.data, that.entropyCounter);
  35. previousData_ = that.data;
  36. previousCounter_ = that.entropyCounter;
  37. }
  38. }, 500);
  39. };
  40. this.stopCollector = function() {
  41. clearInterval(collectionTimer_);
  42. };
  43. }