2
0

hash-generator.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 = HashGenerator;
  11. /**
  12. * @class HashGenerator
  13. * @classdesc Encapsulates a hash generator needed for zero-knowledge proof
  14. * generation and verification.
  15. * @private
  16. * @param {MessageDigestService}
  17. * messageDigestService The message digest service to use.
  18. */
  19. function HashGenerator(messageDigestService) {
  20. var digester_ = messageDigestService.newDigester();
  21. /**
  22. * Generates the hash.
  23. *
  24. * @function generate
  25. * @memberof HashGenerator
  26. * @param {ZpGroupElement[]}
  27. * publicValues The public values.
  28. * @param {ZpGroupElement[]}
  29. * generatedValues The generated values.
  30. * @param {Uint8Array|string}
  31. * data Auxiliary data.
  32. * @returns {Uint8Array} The generated hash.
  33. */
  34. this.generate = function(publicValues, generatedValues, data) {
  35. digester_.update(elementsToString(publicValues));
  36. digester_.update(elementsToString(generatedValues));
  37. digester_.update(data);
  38. return digester_.digest();
  39. };
  40. function elementsToString(elements) {
  41. var str = '';
  42. for (var i = 0; i < elements.length; i++) {
  43. str += elements[i].value;
  44. }
  45. return str;
  46. }
  47. }