JSEncrypt.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. var _a;
  2. import { b64tohex, hex2b64 } from "./lib/jsbn/base64";
  3. import { JSEncryptRSAKey } from "./JSEncryptRSAKey";
  4. import { oaep_pad } from './lib/jsbn/rsa';
  5. import { rstr_sha256, rstr2hex } from './lib/jsbn/sha256';
  6. var version = typeof process !== "undefined" ? (_a = process.env) === null || _a === void 0 ? void 0 : _a.npm_package_version : undefined;
  7. /**
  8. *
  9. * @param {Object} [options = {}] - An object to customize JSEncrypt behaviour
  10. * possible parameters are:
  11. * - key {JSEncryptRSAKey} default: null
  12. * - default_key_size {number} default: 1024 the key size in bit
  13. * - default_public_exponent {string} default: '010001' the hexadecimal representation of the public exponent
  14. * - log {boolean} default: false whether log warn/error or not
  15. * @constructor
  16. */
  17. var JSEncrypt = /** @class */ (function () {
  18. function JSEncrypt(options) {
  19. if (options === void 0) { 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 = options.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 (key) {
  37. if (this.log && this.key) {
  38. console.warn("A key was already set, overriding existing.");
  39. }
  40. this.key = new JSEncryptRSAKey(key);
  41. }
  42. else if (!this.key && this.log) {
  43. console.error("A key was not set.");
  44. }
  45. };
  46. /**
  47. * Proxy method for setKey, for api compatibility
  48. * @see setKey
  49. * @public
  50. */
  51. JSEncrypt.prototype.setPrivateKey = function (privkey) {
  52. // Create the key.
  53. this.setKey(privkey);
  54. };
  55. /**
  56. * Proxy method for setKey, for api compatibility
  57. * @see setKey
  58. * @public
  59. */
  60. JSEncrypt.prototype.setPublicKey = function (pubkey) {
  61. // Sets the public key.
  62. this.setKey(pubkey);
  63. };
  64. /**
  65. * Proxy method for RSAKey object's decrypt, decrypt the string using the private
  66. * components of the rsa key object. Note that if the object was not set will be created
  67. * on the fly (by the getKey method) using the parameters passed in the JSEncrypt constructor
  68. * @param {string} str base64 encoded crypted string to decrypt
  69. * @return {string} the decrypted string
  70. * @public
  71. */
  72. JSEncrypt.prototype.decrypt = function (str) {
  73. // Return the decrypted string.
  74. try {
  75. return this.getKey().decrypt(b64tohex(str));
  76. }
  77. catch (ex) {
  78. return false;
  79. }
  80. };
  81. /**
  82. * Proxy method for RSAKey object's encrypt, encrypt the string using the public
  83. * components of the rsa key object. Note that if the object was not set will be created
  84. * on the fly (by the getKey method) using the parameters passed in the JSEncrypt constructor
  85. * @param {string} str the string to encrypt
  86. * @return {string} the encrypted string encoded in base64
  87. * @public
  88. */
  89. JSEncrypt.prototype.encrypt = function (str) {
  90. // Return the encrypted string.
  91. try {
  92. return hex2b64(this.getKey().encrypt(str));
  93. }
  94. catch (ex) {
  95. return false;
  96. }
  97. };
  98. /**
  99. * Proxy method for RSAKey object's encrypt with padding: RSA_PKCS1_OAEP_PADDING and oaepHash: sha256,
  100. * encrypt the string using the public
  101. * components of the rsa key object. Note that if the object was not set will be created
  102. * on the fly (by the getKey method) using the parameters passed in the JSEncrypt constructor
  103. * @param {string} str the string to encrypt
  104. * @return {string} the encrypted string encoded in base64
  105. * @public
  106. */
  107. JSEncrypt.prototype.encryptOAEP = function (str) {
  108. // Return the encrypted string.
  109. try {
  110. return hex2b64(this.getKey().encrypt(str, oaep_pad));
  111. }
  112. catch (ex) {
  113. return false;
  114. }
  115. };
  116. /**
  117. * Proxy method for RSAKey object's sign.
  118. * @param {string} str the string to sign
  119. * @param {function} digestMethod hash method
  120. * @param {string} digestName the name of the hash algorithm
  121. * @return {string} the signature encoded in base64
  122. * @public
  123. */
  124. JSEncrypt.prototype.sign = function (str, digestMethod, digestName) {
  125. if (digestMethod === void 0) { digestMethod = function (raw) { return raw; }; }
  126. if (digestName === void 0) { digestName = ""; }
  127. // return the RSA signature of 'str' in 'hex' format.
  128. try {
  129. return hex2b64(this.getKey().sign(str, digestMethod, digestName));
  130. }
  131. catch (ex) {
  132. return false;
  133. }
  134. };
  135. /**
  136. * Signs a string using the SHA-256 hash algorithm.
  137. * @param str the string to sign
  138. * @returns the base64 encoded signature or false on failure
  139. */
  140. JSEncrypt.prototype.signSha256 = function (str) {
  141. return this.sign(str, function (text) {
  142. return rstr2hex(rstr_sha256(text));
  143. }, "sha256");
  144. };
  145. /**
  146. * Proxy method for RSAKey object's verify.
  147. * @param {string} str the string to verify
  148. * @param {string} signature the signature encoded in base64 to compare the string to
  149. * @param {function} digestMethod hash method
  150. * @return {boolean} whether the data and signature match
  151. * @public
  152. */
  153. JSEncrypt.prototype.verify = function (str, signature, digestMethod) {
  154. if (digestMethod === void 0) { digestMethod = function (raw) { return raw; }; }
  155. // Return the decrypted 'digest' of the signature.
  156. try {
  157. return this.getKey().verify(str, b64tohex(signature), digestMethod);
  158. }
  159. catch (ex) {
  160. return false;
  161. }
  162. };
  163. /**
  164. * Verifies a string using the SHA-256 hash algorithm.
  165. * @param str the string to verify
  166. * @param signature the base64 encoded signature to compare against
  167. * @returns whether the signature is valid
  168. */
  169. JSEncrypt.prototype.verifySha256 = function (str, signature) {
  170. return this.verify(str, signature, function (text) {
  171. return rstr2hex(rstr_sha256(text));
  172. });
  173. };
  174. /**
  175. * Getter for the current JSEncryptRSAKey object. If it doesn't exists a new object
  176. * will be created and returned
  177. * @param {callback} [cb] the callback to be called if we want the key to be generated
  178. * in an async fashion
  179. * @returns {JSEncryptRSAKey} the JSEncryptRSAKey object
  180. * @public
  181. */
  182. JSEncrypt.prototype.getKey = function (cb) {
  183. // Only create new if it does not exist.
  184. if (!this.key) {
  185. // Get a new private key.
  186. this.key = new JSEncryptRSAKey();
  187. if (cb && {}.toString.call(cb) === "[object Function]") {
  188. this.key.generateAsync(this.default_key_size, this.default_public_exponent, cb);
  189. return;
  190. }
  191. // Generate the key.
  192. this.key.generate(this.default_key_size, this.default_public_exponent);
  193. }
  194. return this.key;
  195. };
  196. /**
  197. * Returns the pem encoded representation of the private key
  198. * If the key doesn't exists a new key will be created
  199. * @returns {string} pem encoded representation of the private key WITH header and footer
  200. * @public
  201. */
  202. JSEncrypt.prototype.getPrivateKey = function () {
  203. // Return the private representation of this key.
  204. return this.getKey().getPrivateKey();
  205. };
  206. /**
  207. * Returns the pem encoded representation of the private key
  208. * If the key doesn't exists a new key will be created
  209. * @returns {string} pem encoded representation of the private key WITHOUT header and footer
  210. * @public
  211. */
  212. JSEncrypt.prototype.getPrivateKeyB64 = function () {
  213. // Return the private representation of this key.
  214. return this.getKey().getPrivateBaseKeyB64();
  215. };
  216. /**
  217. * Returns the pem encoded representation of the public key
  218. * If the key doesn't exists a new key will be created
  219. * @returns {string} pem encoded representation of the public key WITH header and footer
  220. * @public
  221. */
  222. JSEncrypt.prototype.getPublicKey = function () {
  223. // Return the private representation of this key.
  224. return this.getKey().getPublicKey();
  225. };
  226. /**
  227. * Returns the pem encoded representation of the public key
  228. * If the key doesn't exists a new key will be created
  229. * @returns {string} pem encoded representation of the public key WITHOUT header and footer
  230. * @public
  231. */
  232. JSEncrypt.prototype.getPublicKeyB64 = function () {
  233. // Return the private representation of this key.
  234. return this.getKey().getPublicBaseKeyB64();
  235. };
  236. JSEncrypt.version = version;
  237. return JSEncrypt;
  238. }());
  239. export { JSEncrypt };