service.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 SecretKeyGenerator = require('./key-generator');
  11. var SymmetricCipher = require('./cipher');
  12. var MacHandler = require('./mac-handler');
  13. var cryptoPolicy = require('scytl-cryptopolicy');
  14. var secureRandom = require('scytl-securerandom');
  15. module.exports = SymmetricCryptographyService;
  16. /**
  17. * @class SymmetricCryptographyService
  18. * @classdesc The symmetric cryptography service API. To instantiate this
  19. * object, use the method {@link newService}.
  20. * @hideconstructor
  21. * @param {Object}
  22. * [options] An object containing optional arguments.
  23. * @param {Policy}
  24. * [options.policy=Default policy] The cryptographic policy to use.
  25. * @param {SecureRandomService}
  26. * [options.secureRandomService=Created internally] The secure random
  27. * service to use.
  28. */
  29. function SymmetricCryptographyService(options) {
  30. options = options || {};
  31. var policy_;
  32. if (options.policy) {
  33. policy_ = options.policy;
  34. } else {
  35. policy_ = cryptoPolicy.newInstance();
  36. }
  37. var secureRandomService_;
  38. if (options.secureRandomService) {
  39. secureRandomService_ = options.secureRandomService;
  40. } else {
  41. secureRandomService_ = secureRandom.newService();
  42. }
  43. /**
  44. * Creates a new SecretKeyGenerator object for generating secret keys.
  45. *
  46. * @function newKeyGenerator
  47. * @memberof SymmetricCryptographyService
  48. * @returns {SecretKeyGenerator} The new SecretKeyGenerator object.
  49. */
  50. this.newKeyGenerator = function() {
  51. return new SecretKeyGenerator(policy_, secureRandomService_);
  52. };
  53. /**
  54. * Creates a new SymmetricCipher object for symmetrically encrypting or
  55. * decrypting data. It must be initialized with a secret key.
  56. *
  57. * @function newCipher
  58. * @memberof SymmetricCryptographyService
  59. * @returns {SymmetricCipher} The new SymmetricCipher object.
  60. */
  61. this.newCipher = function() {
  62. return new SymmetricCipher(policy_, secureRandomService_);
  63. };
  64. /**
  65. * Creates a new MacHandler object for generating or verifying a MAC. It
  66. * must be initialized with a secret key.
  67. *
  68. * @function newMacHandler
  69. * @memberof SymmetricCryptographyService
  70. * @returns {MacHandler} The new MacHandler object.
  71. */
  72. this.newMacHandler = function() {
  73. return new MacHandler(policy_);
  74. };
  75. }