digitalenvelope.service.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. cryptolib.modules.digitalenvelope = cryptolib.modules.digitalenvelope || {};
  9. /**
  10. * Defines the digital envelope service.
  11. *
  12. * @namespace digitalenvelope
  13. */
  14. cryptolib.modules.digitalenvelope.service = function(box) {
  15. 'use strict';
  16. box.digitalenvelope = box.digitalenvelope || {};
  17. box.digitalenvelope.service = {};
  18. var digitalEnvelopeFactory;
  19. cryptolib('digitalenvelope', function(box) {
  20. digitalEnvelopeFactory =
  21. new box.digitalenvelope.factory.DigitalEnvelopeFactory();
  22. });
  23. /**
  24. * Creates a digital envelope, using one or more public keys, each of whose
  25. * corresponding private key can be used to open the envelope and retrieve
  26. * the data.
  27. *
  28. * @function createDigitalEnvelope
  29. * @param dataBase64
  30. * {string} the data to store in the digital envelope, as a
  31. * string in Base64 encoded format.
  32. * @param publicKeysPem
  33. * {string[]} a list of one or more public keys used to generate
  34. * the digital envelope, as an array of strings in PEM format.
  35. * @return {DigitalEnvelope} the newly created digital envelope.
  36. */
  37. box.digitalenvelope.service.createDigitalEnvelope = function(
  38. dataBase64, publicKeysPem) {
  39. var generator = digitalEnvelopeFactory.getDigitalEnvelopeGenerator();
  40. return generator.generate(dataBase64, publicKeysPem);
  41. };
  42. /**
  43. * Opens an existing digital envelope and retrieves the data that it
  44. * contains. The envelope can be opened with any private key corresponding
  45. * to a public key that was used to generate the envelope.
  46. *
  47. * @function openDigitalEnvelope
  48. * @param digitalEnvelope
  49. * {DigitalEnvelope} the digital envelope to open.
  50. * @param privateKeyPem
  51. * {string} the private key used to open the digital envelope, as
  52. * a string in PEM format.
  53. * @return {string} the data contained in the digital envelope, as a string
  54. * in Base64 encoded format.
  55. */
  56. box.digitalenvelope.service.openDigitalEnvelope = function(
  57. digitalEnvelope, privateKeyPem) {
  58. var opener = digitalEnvelopeFactory.getDigitalEnvelopeOpener();
  59. return opener.open(digitalEnvelope, privateKeyPem);
  60. };
  61. };