2
0

index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 AsymmetricCryptographyService = require('./service');
  11. var validator = require('./input-validator');
  12. module.exports = {
  13. /**
  14. * Creates a new AsymmetricCryptographyService object, which encapsulates an
  15. * asymmetric cryptography service.
  16. *
  17. * @function newService
  18. * @global
  19. * @param {Object}
  20. * [options] An object containing optional arguments.
  21. * @param {Policy}
  22. * [options.policy=Default policy] The cryptographic policy to
  23. * use.
  24. * @param {SecureRandomService}
  25. * [options.secureRandomService=Created internally] The secure
  26. * random service to use.
  27. * @returns {AsymmetricCryptographyService} The new
  28. * AsymmetricCryptographyService object.
  29. * @throws {Error}
  30. * If the input data validation fails.
  31. * @example <caption> How to use a cryptographic policy that sets the key
  32. * length of the asymmetric cipher to 3072 bits</caption>
  33. *
  34. * var cryptoPolicy = require('scytl-cryptopolicy');
  35. * var asymmetric = require('scytl-asymmetric');
  36. *
  37. * var myPolicy = cryptoPolicy.newInstance();
  38. *
  39. * myPolicy.asymmetric.keyPair.encryption.keyLength =
  40. * cryptoPolicy.options.asymmetric.keyPair.encryption.keyLength.KL_3072;
  41. *
  42. * var asymmetricService = asymmetric.newService({policy: myPolicy});
  43. */
  44. newService: function(options) {
  45. checkData(options);
  46. return new AsymmetricCryptographyService(options);
  47. }
  48. };
  49. function checkData(options) {
  50. options = options || {};
  51. if (typeof options.policy !== 'undefined') {
  52. validator.checkIsObjectWithProperties(
  53. options.policy,
  54. 'Cryptographic policy provided to asymmetric cryptography service');
  55. }
  56. if (typeof options.secureRandomService !== 'undefined') {
  57. validator.checkIsObjectWithProperties(
  58. options.secureRandomService,
  59. 'Secure random service object provided to asymmetric cryptography service');
  60. }
  61. }