JSEncrypt.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. var _a;
  2. import { b64tohex, hex2b64 } from "./lib/jsbn/base64";
  3. import { JSEncryptRSAKey } from "./JSEncryptRSAKey";
  4. var version = typeof process !== 'undefined'
  5. ? (_a = process.env) === null || _a === void 0 ? void 0 : _a.npm_package_version
  6. : undefined;
  7. /**
  8. *
  9. * @param {Object} [options = {}] - An object to customize JSEncrypt behaviour
  10. * possible parameters are:
  11. * - default_key_size {number} default: 1024 the key size in bit
  12. * - default_public_exponent {string} default: '010001' the hexadecimal representation of the public exponent
  13. * - log {boolean} default: false whether log warn/error or not
  14. * @constructor
  15. */
  16. var JSEncrypt = /** @class */ (function () {
  17. function JSEncrypt(options) {
  18. if (options === void 0) { options = {}; }
  19. options = options || {};
  20. this.default_key_size = options.default_key_size
  21. ? parseInt(options.default_key_size, 10)
  22. : 1024;
  23. this.default_public_exponent = options.default_public_exponent || "010001"; // 65537 default openssl public exponent for rsa key type
  24. this.log = options.log || false;
  25. // The private and public key.
  26. this.key = null;
  27. }
  28. /**
  29. * Method to set the rsa key parameter (one method is enough to set both the public
  30. * and the private key, since the private key contains the public key paramenters)
  31. * Log a warning if logs are enabled
  32. * @param {Object|string} key the pem encoded string or an object (with or without header/footer)
  33. * @public
  34. */
  35. JSEncrypt.prototype.setKey = function (key) {
  36. if (this.log && this.key) {
  37. console.warn("A key was already set, overriding existing.");
  38. }
  39. this.key = new JSEncryptRSAKey(key);
  40. };
  41. /**
  42. * Proxy method for setKey, for api compatibility
  43. * @see setKey
  44. * @public
  45. */
  46. JSEncrypt.prototype.setPrivateKey = function (privkey) {
  47. // Create the key.
  48. this.setKey(privkey);
  49. };
  50. /**
  51. * Proxy method for setKey, for api compatibility
  52. * @see setKey
  53. * @public
  54. */
  55. JSEncrypt.prototype.setPublicKey = function (pubkey) {
  56. // Sets the public key.
  57. this.setKey(pubkey);
  58. };
  59. /**
  60. * Proxy method for RSAKey object's decrypt, decrypt the string using the private
  61. * components of the rsa key object. Note that if the object was not set will be created
  62. * on the fly (by the getKey method) using the parameters passed in the JSEncrypt constructor
  63. * @param {string} str base64 encoded crypted string to decrypt
  64. * @return {string} the decrypted string
  65. * @public
  66. */
  67. JSEncrypt.prototype.decrypt = function (str) {
  68. // Return the decrypted string.
  69. try {
  70. return this.getKey().decrypt(b64tohex(str));
  71. }
  72. catch (ex) {
  73. return false;
  74. }
  75. };
  76. /**
  77. * Proxy method for RSAKey object's encrypt, encrypt the string using the public
  78. * components of the rsa key object. Note that if the object was not set will be created
  79. * on the fly (by the getKey method) using the parameters passed in the JSEncrypt constructor
  80. * @param {string} str the string to encrypt
  81. * @return {string} the encrypted string encoded in base64
  82. * @public
  83. */
  84. JSEncrypt.prototype.encrypt = function (str) {
  85. // Return the encrypted string.
  86. try {
  87. return hex2b64(this.getKey().encrypt(str));
  88. }
  89. catch (ex) {
  90. return false;
  91. }
  92. };
  93. /**
  94. * Proxy method for RSAKey object's sign.
  95. * @param {string} str the string to sign
  96. * @param {function} digestMethod hash method
  97. * @param {string} digestName the name of the hash algorithm
  98. * @return {string} the signature encoded in base64
  99. * @public
  100. */
  101. JSEncrypt.prototype.sign = function (str, digestMethod, digestName) {
  102. // return the RSA signature of 'str' in 'hex' format.
  103. try {
  104. return hex2b64(this.getKey().sign(str, digestMethod, digestName));
  105. }
  106. catch (ex) {
  107. return false;
  108. }
  109. };
  110. /**
  111. * Proxy method for RSAKey object's verify.
  112. * @param {string} str the string to verify
  113. * @param {string} signature the signature encoded in base64 to compare the string to
  114. * @param {function} digestMethod hash method
  115. * @return {boolean} whether the data and signature match
  116. * @public
  117. */
  118. JSEncrypt.prototype.verify = function (str, signature, digestMethod) {
  119. // Return the decrypted 'digest' of the signature.
  120. try {
  121. return this.getKey().verify(str, b64tohex(signature), digestMethod);
  122. }
  123. catch (ex) {
  124. return false;
  125. }
  126. };
  127. /**
  128. * Getter for the current JSEncryptRSAKey object. If it doesn't exists a new object
  129. * will be created and returned
  130. * @param {callback} [cb] the callback to be called if we want the key to be generated
  131. * in an async fashion
  132. * @returns {JSEncryptRSAKey} the JSEncryptRSAKey object
  133. * @public
  134. */
  135. JSEncrypt.prototype.getKey = function (cb) {
  136. // Only create new if it does not exist.
  137. if (!this.key) {
  138. // Get a new private key.
  139. this.key = new JSEncryptRSAKey();
  140. if (cb && {}.toString.call(cb) === "[object Function]") {
  141. this.key.generateAsync(this.default_key_size, this.default_public_exponent, cb);
  142. return;
  143. }
  144. // Generate the key.
  145. this.key.generate(this.default_key_size, this.default_public_exponent);
  146. }
  147. return this.key;
  148. };
  149. /**
  150. * Returns the pem encoded representation of the private key
  151. * If the key doesn't exists a new key will be created
  152. * @returns {string} pem encoded representation of the private key WITH header and footer
  153. * @public
  154. */
  155. JSEncrypt.prototype.getPrivateKey = function () {
  156. // Return the private representation of this key.
  157. return this.getKey().getPrivateKey();
  158. };
  159. /**
  160. * Returns the pem encoded representation of the private key
  161. * If the key doesn't exists a new key will be created
  162. * @returns {string} pem encoded representation of the private key WITHOUT header and footer
  163. * @public
  164. */
  165. JSEncrypt.prototype.getPrivateKeyB64 = function () {
  166. // Return the private representation of this key.
  167. return this.getKey().getPrivateBaseKeyB64();
  168. };
  169. /**
  170. * Returns the pem encoded representation of the public key
  171. * If the key doesn't exists a new key will be created
  172. * @returns {string} pem encoded representation of the public key WITH header and footer
  173. * @public
  174. */
  175. JSEncrypt.prototype.getPublicKey = function () {
  176. // Return the private representation of this key.
  177. return this.getKey().getPublicKey();
  178. };
  179. /**
  180. * Returns the pem encoded representation of the public key
  181. * If the key doesn't exists a new key will be created
  182. * @returns {string} pem encoded representation of the public key WITHOUT header and footer
  183. * @public
  184. */
  185. JSEncrypt.prototype.getPublicKeyB64 = function () {
  186. // Return the private representation of this key.
  187. return this.getKey().getPublicBaseKeyB64();
  188. };
  189. JSEncrypt.version = version;
  190. return JSEncrypt;
  191. }());
  192. export { JSEncrypt };