2
0

rsa-public-key.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 constants = require('./constants');
  11. var forge = require('node-forge');
  12. module.exports = RsaPublicKey;
  13. /**
  14. * @class RsaPublicKey
  15. * @classdesc Encapsulates an RSA public key. To instantiate this object, use
  16. * the method {@link AsymmetricCryptographyService.newRsaPublicKey}.
  17. * @property {number} n The modulus.
  18. * @property {number} e The public exponent.
  19. */
  20. function RsaPublicKey(params) {
  21. this.n = params.n;
  22. this.e = params.e;
  23. return Object.freeze(this);
  24. }
  25. RsaPublicKey.prototype = {
  26. /**
  27. * Serializes this key into its PEM string representation.
  28. *
  29. * @function toPem
  30. * @memberof RsaPublicKey
  31. * @returns {string} The PEM string representation of this key.
  32. */
  33. toPem: function() {
  34. var forgePublicKey = forge.pki.rsa.setPublicKey(this.n, this.e);
  35. return forge.pki.publicKeyToPem(forgePublicKey, constants.PEM_LINE_LENGTH);
  36. }
  37. };