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 SymmetricCryptographyService = require('./service');
  11. var validator = require('./input-validator');
  12. module.exports = {
  13. /**
  14. * Creates a new SymmetricCryptographyService object, which encapsulates a
  15. * symmetric 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 {SymmetricCryptographyService} The new
  28. * SymmetricCryptographyService 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 symmetric cipher to 32 bytes</caption>
  33. *
  34. * var cryptoPolicy = require('scytl-cryptopolicy');
  35. * var symmetric = require('scytl-symmetric');
  36. *
  37. * var myPolicy = cryptoPolicy.newInstance();
  38. *
  39. * myPolicy.symmetric.cipher.algorithm.AES_GCM.keyLengthBytes =
  40. * cryptoPolicy.options.symmetric.cipher.algorithm.AES_GCM.keyLengthBytes.KL_32;
  41. *
  42. * var symmetricService = symmetric.newService({policy: myPolicy});
  43. */
  44. newService: function(options) {
  45. checkData(options);
  46. return new SymmetricCryptographyService(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 symmetric cryptography service');
  55. }
  56. if (typeof options.secureRandomService !== 'undefined') {
  57. validator.checkIsObjectWithProperties(
  58. options.secureRandomService,
  59. 'Secure random service object provided to symmetric cryptography service');
  60. }
  61. }