primitives.service.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.primitives = cryptolib.modules.primitives || {};
  9. /**
  10. * Defines primitives services.
  11. * @namespace primitives/service
  12. */
  13. cryptolib.modules.primitives.service = function(box) {
  14. 'use strict';
  15. box.primitives = box.primitives || {};
  16. /**
  17. * A module that holds primitives API.
  18. * @exports primitives/service
  19. */
  20. box.primitives.service = {};
  21. var securerandomFactory, messageDigestFactory, secretKeyGeneratorFactory;
  22. cryptolib(
  23. 'primitives.securerandom', 'commons.exceptions',
  24. 'primitives.messagedigest', 'primitives.derivation', function(box) {
  25. securerandomFactory =
  26. new box.primitives.securerandom.factory.SecureRandomFactory();
  27. messageDigestFactory =
  28. new box.primitives.messagedigest.factory.MessageDigestFactory();
  29. secretKeyGeneratorFactory =
  30. new box.primitives.derivation.factory.SecretKeyGeneratorFactory();
  31. });
  32. /**
  33. * Provides a random bytes generator.
  34. * @function
  35. * @returns {primitives/securerandom.CryptoScytlRandomBytes} a CryptoRandomBytes object.
  36. */
  37. box.primitives.service.getCryptoRandomBytes = function() {
  38. return securerandomFactory.getCryptoRandomBytes();
  39. };
  40. /**
  41. * Provides a random integer generator.
  42. * @function
  43. * @returns {primitives/securerandom.CryptoScytlRandomInteger} a CryptoRandomInteger object.
  44. */
  45. box.primitives.service.getCryptoRandomInteger = function() {
  46. return securerandomFactory.getCryptoRandomInteger();
  47. };
  48. /**
  49. * Generates a hash for the given data.
  50. * @function
  51. * @parameter {array} ArrayDataBase64 the input data for the hash.
  52. * @returns {string} the hash, as a string in Base64 encoded format.
  53. */
  54. box.primitives.service.getHash = function(ArrayDataBase64) {
  55. var hashFunction = messageDigestFactory.getCryptoMessageDigest();
  56. hashFunction.start();
  57. hashFunction.update(ArrayDataBase64);
  58. return hashFunction.digest();
  59. };
  60. /**
  61. * Provides a derived secret key generator.
  62. * @function
  63. * @returns {primitives/derivation.CryptoForgePBKDFSecretKeyGenerator} a the pbkdf Generator.
  64. */
  65. box.primitives.service.getPbkdfSecretKeyGenerator = function() {
  66. return secretKeyGeneratorFactory.createPBKDF();
  67. };
  68. };