key-generator.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. module.exports = SecretKeyGenerator;
  11. /**
  12. * @class SecretKeyGenerator
  13. * @classdesc The secret key generator API. To instantiate this object, use the
  14. * method {@link SymmetricCryptographyService.newKeyGenerator}.
  15. * @hideconstructor
  16. * @param {Policy}
  17. * policy The cryptographic policy to use.
  18. * @param {SecureRandomService}
  19. * secureRandomService The secure random service to use.
  20. */
  21. function SecretKeyGenerator(policy, secureRandomService) {
  22. var randomGenerator_ = secureRandomService.newRandomGenerator();
  23. /**
  24. * Generates a secret key to use for symmetric encryption.
  25. *
  26. * @function nextEncryptionKey
  27. * @memberof SecretKeyGenerator
  28. * @returns {Uint8Array} The encryption secret key.
  29. */
  30. this.nextEncryptionKey = function() {
  31. return randomGenerator_.nextBytes(
  32. policy.symmetric.secretKey.encryption.lengthBytes);
  33. };
  34. /**
  35. * Generates a secret key to use for MAC generation.
  36. *
  37. * @function nextMacKey
  38. * @memberof SecretKeyGenerator
  39. * @returns {Uint8Array} The MAC secret key.
  40. */
  41. this.nextMacKey = function() {
  42. return randomGenerator_.nextBytes(
  43. policy.symmetric.secretKey.mac.lengthBytes);
  44. };
  45. }