index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 ElGamalCryptographyService = require('./service');
  11. var validator = require('./input-validator');
  12. module.exports = {
  13. /**
  14. * Creates a new ElGamalCryptographyService object, which encapsulates an
  15. * ElGamal cryptography service.
  16. *
  17. * @function newService
  18. * @global
  19. * @param {Object}
  20. * [options] An object containing optional arguments.
  21. * @param {SecureRandomService}
  22. * [options.secureRandomService=Created internally] The secure
  23. * random service to use.
  24. * @param {MathematicalService}
  25. * [options.mathematicalService=Created internally] The
  26. * mathematical service to use.
  27. * @returns {ElGamalCryptographyService} The new ElGamalCryptographyService
  28. * object.
  29. * @throws {Error}
  30. * If the input data validation fails.
  31. * @example <caption> How to use a provided secure random service, that was
  32. * initialized with a chosen seed</caption>
  33. *
  34. * var elGamal = require('scytl-elgamal');
  35. * var secureRandom = require('scytl-securerandom');
  36. *
  37. * var mySecureRandomService = secureRandom.newService({prngSeed: mySeed});
  38. *
  39. * var elGamalService = elGamal.newService({secureRandomService:
  40. * mySecureRandomService});
  41. */
  42. newService: function(options) {
  43. checkData(options);
  44. return new ElGamalCryptographyService(options);
  45. }
  46. };
  47. function checkData(options) {
  48. options = options || {};
  49. if (typeof options.secureRandomService !== 'undefined') {
  50. validator.checkIsObjectWithProperties(
  51. options.secureRandomService,
  52. 'Secure random service object provided to ElGamal cryptography service');
  53. }
  54. if (typeof options.mathematicalService !== 'undefined') {
  55. validator.checkIsObjectWithProperties(
  56. options.mathematicalService,
  57. 'Mathematical service object provided to ElGamal cryptography service');
  58. }
  59. }