privatekey.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. var forge = require('node-forge');
  11. module.exports = PrivateKeyCollector;
  12. var collectEntropy_, privateKey_, pinEntropy_;
  13. /**
  14. * Add an entropy collector based on a private key. The amount of randomness
  15. * (entropy) depends on the pin that is used for Private Key generating.
  16. *
  17. * @function
  18. * @param pem
  19. * {string} private key, as string in PEM format.
  20. * @param entropy
  21. * the amount of randomness that can be extracted from the
  22. * Private Key.
  23. */
  24. function PrivateKeyCollector(
  25. collectEntropyCallback, privateKeyPem, pinEntropy) {
  26. if (!privateKeyPem) {
  27. throw new Error('A private key is required');
  28. }
  29. if (isNaN(pinEntropy) || (pinEntropy < 1)) {
  30. throw new Error('A positive PIN entropy is required');
  31. }
  32. collectEntropy_ = collectEntropyCallback;
  33. privateKey_ = forge.pki.privateKeyFromPem(privateKeyPem);
  34. pinEntropy_ = pinEntropy;
  35. }
  36. PrivateKeyCollector.prototype = {
  37. pkCollector: true,
  38. startCollector: function() {
  39. collectEntropy_(privateKey_.d, pinEntropy_);
  40. },
  41. stopCollector: function() {
  42. // just executed once, so no need to execute anything else
  43. return true;
  44. },
  45. getPrivateKey: function() {
  46. return privateKey_;
  47. }
  48. };