index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 MessageDigestService = require('./service');
  11. var validator = require('./input-validator');
  12. module.exports = {
  13. /**
  14. * Creates a new MessageDigestService object, which encapsulates a message
  15. * digiest 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. * @returns {MessageDigestService} The new MessageDigestService object.
  25. * @throws {Error}
  26. * If the input data validation fails.
  27. * @example <caption> How to use a cryptographic policy that sets the
  28. * message digest hash algorithm to <code>SHA512/224</code></caption>
  29. *
  30. * var cryptoPolicy = require('scytl-cryptopolicy'); var messageDigest =
  31. * require('scytl-messagedigest');
  32. *
  33. * var myPolicy = cryptoPolicy.newInstance();
  34. *
  35. * myPolicy.primitives.messageDigest.algorithm =
  36. * cryptoPolicy.options.primitives.messageDigest.algorithm.SHA512_224;
  37. *
  38. * var messageDigestService = messageDigest.newService({policy: myPolicy});
  39. */
  40. newService: function(options) {
  41. checkData(options);
  42. return new MessageDigestService(options);
  43. }
  44. };
  45. function checkData(options) {
  46. options = options || {};
  47. if (typeof options.policy !== 'undefined') {
  48. validator.checkIsObjectWithProperties(
  49. options.policy,
  50. 'Cryptographic policy provided to message digest service');
  51. }
  52. }