2
0

rsa-private-key.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 = RsaPrivateKey;
  13. /**
  14. * @class RsaPrivateKey
  15. * @classdesc Encapsulates an RSA private key. To instantiate this object, use
  16. * the method {@link AsymmetricCryptographyService.newRsaPrivateKey}
  17. * @property {number} n The modulus.
  18. * @property {number} e The public exponent.
  19. * @property {number} d The private exponent.
  20. * @property {number} p The first prime.
  21. * @property {number} q The second prime.
  22. * @property {number} dP The first exponent.
  23. * @property {number} dQ The second exponent.
  24. * @property {number} qInv The coefficient.
  25. */
  26. function RsaPrivateKey(params) {
  27. this.n = params.n;
  28. this.e = params.e;
  29. this.d = params.d;
  30. this.p = params.p;
  31. this.q = params.q;
  32. this.dP = params.dP;
  33. this.dQ = params.dQ;
  34. this.qInv = params.qInv;
  35. return Object.freeze(this);
  36. }
  37. RsaPrivateKey.prototype = {
  38. /**
  39. * Serializes this key into its PEM string representation.
  40. *
  41. * @function toPem
  42. * @memberof RsaPrivateKey
  43. * @returns {string} The PEM string representation of this key.
  44. */
  45. toPem: function() {
  46. var forgePrivateKey = forge.pki.rsa.setPrivateKey(
  47. this.n, this.e, this.d, this.p, this.q, this.dP, this.dQ, this.qInv);
  48. return forge.pki.privateKeyToPem(
  49. forgePrivateKey, constants.PEM_LINE_LENGTH);
  50. }
  51. };