2
0

private-key.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 codec = require('scytl-codec');
  11. module.exports = ElGamalPrivateKey;
  12. /**
  13. * @class ElGamalPrivateKey
  14. * @classdesc Encapsulates an ElGamal private key. To instantiate this object,
  15. * use the method {@link ElGamalCryptographyService.newPrivateKey}.
  16. * @property {ZpSubgroup} group The Zp subgroup to which the exponents of this
  17. * private key are associated.
  18. * @property {Exponent[]} exponents The exponents that comprise this private
  19. * key.
  20. */
  21. function ElGamalPrivateKey(group, exponents) {
  22. this.group = group;
  23. Object.freeze(this.exponents = exponents);
  24. Object.freeze(this);
  25. }
  26. ElGamalPrivateKey.prototype = {
  27. /**
  28. * Serializes this object into a JSON string representation.
  29. * <p>
  30. * <b>IMPORTANT:</b> This serialization must be exactly the same as the
  31. * corresponding serialization in the library <code>cryptoLib</code>,
  32. * implemented in Java, since the two libraries are expected to communicate
  33. * with each other via these serializations.
  34. *
  35. * @function toJson
  36. * @memberof ElGamalPrivateKey
  37. * @returns {string} The JSON string representation of this object.
  38. */
  39. toJson: function() {
  40. var gB64 = codec.base64Encode(this.group.generator.value);
  41. var pB64 = codec.base64Encode(this.group.p);
  42. var qB64 = codec.base64Encode(this.group.q);
  43. var exponentsB64 = [];
  44. for (var i = 0; i < this.exponents.length; i++) {
  45. exponentsB64.push(codec.base64Encode(this.exponents[i].value));
  46. }
  47. return JSON.stringify({
  48. privateKey:
  49. {zpSubgroup: {g: gB64, p: pB64, q: qB64}, exponents: exponentsB64}
  50. });
  51. }
  52. };