index.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 DigitalEnvelopeService = require('./service');
  11. var validator = require('./input-validator');
  12. module.exports = {
  13. /**
  14. * Creates a new DigitalEnvelopeService object, which encapsulates a digital
  15. * envelope 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 {SymmetricCryptographyService}
  25. * [options.symmetricCryptographyService=Created internally] The
  26. * symmetric cryptography service to use.
  27. * @param {AsymmetricCryptographyService}
  28. * [options.asymmetricCryptographyService=Created internally] The
  29. * asymmetric cryptography service to use.
  30. * @returns {DigitalEnvelopeService} The new DigitalEnvelopeService object.
  31. * @throws {Error}
  32. * If the input data validation fails.
  33. * @example <caption> How to use a cryptographic policy that sets the key
  34. * length of the symmetric cipher used by the digital envelope to
  35. * 32 bytes</caption>
  36. *
  37. * var cryptoPolicy = require('scytl-cryptopolicy');
  38. * var digitalEnvelope = require('scytl-digitalenvelope');
  39. *
  40. * var myPolicy = cryptoPolicy.newInstance();
  41. *
  42. * myPolicy.digitalEnvelope.symmetric.cipher.algorithm.AES_GCM.keyLengthBytes=
  43. * cryptoPolicy.options.digitalEnvelope.symmetric.cipher.algorithm.AES_GCM.keyLengthBytes.KL_32;
  44. *
  45. * var digitalEnvelopeService = digitalEnvelope.newService({policy:
  46. * myPolicy});
  47. */
  48. newService: function(options) {
  49. checkData(options);
  50. return new DigitalEnvelopeService(options);
  51. }
  52. };
  53. function checkData(options) {
  54. options = options || {};
  55. if (typeof options.policy !== 'undefined') {
  56. validator.checkIsObjectWithProperties(
  57. options.policy,
  58. 'Cryptographic policy provided to digital envelope service');
  59. }
  60. if (typeof options.symmetricCryptographyService !== 'undefined') {
  61. validator.checkIsObjectWithProperties(
  62. options.symmetricCryptographyService,
  63. 'Symmetric cryptography service object provided to digital envelope service');
  64. }
  65. if (typeof options.asymmetricCryptographyService !== 'undefined') {
  66. validator.checkIsObjectWithProperties(
  67. options.asymmetricCryptographyService,
  68. 'Asymmetric cryptography service object provided to digital envelope service');
  69. }
  70. }