{"name":"node-rsa","version":"2.0.0","description":"RSA library for Node.js and browsers","type":"module","engines":{"node":">=20"},"main":"./dist/index.node.cjs","module":"./dist/index.node.js","browser":"./dist/index.browser.js","types":"./dist/index.node.d.ts","exports":{".":{"types":"./dist/index.node.d.ts","browser":{"import":"./dist/index.browser.js"},"node":{"import":"./dist/index.node.js","require":"./dist/index.node.cjs"},"default":"./dist/index.browser.js"},"./package.json":"./package.json"},"sideEffects":false,"scripts":{"build":"tsup","test":"vitest run","test:watch":"vitest","bench":"vitest bench --run --config=vitest.bench.config.ts","lint":"biome check .","lint:fix":"biome check --write .","format":"biome format --write .","typecheck":"tsc --noEmit","check":"npm run typecheck && npm run lint && npm run test && npm run build && npm run check:bundle-size","check:bundle-size":"node scripts/check-bundle-size.mjs"},"repository":{"type":"git","url":"git+https://github.com/rzcoder/node-rsa.git"},"keywords":["rsa","crypto","asymmetric","encryption","decryption","sign","verify","pkcs1","oaep","pss","typescript","browser"],"author":{"name":"rzcoder"},"license":"MIT","bugs":{"url":"https://github.com/rzcoder/node-rsa/issues"},"homepage":"https://github.com/rzcoder/node-rsa","dependencies":{"@noble/hashes":"^2.2.0"},"devDependencies":{"@biomejs/biome":"^2.4.15","@types/node":"^22.10.0","chai":"^6.2.2","tsup":"^8.5.1","typescript":"^6.0.3","vitest":"^4.1.6"},"gitHead":"54798624ba5b15521763c6d1ca69b93115a61d67","_id":"node-rsa@2.0.0","_nodeVersion":"24.15.0","_npmVersion":"11.12.1","dist":{"integrity":"sha512-7yfzccmBd5rm7pALJf709tPwLL987WvdT7LC/QK5Oxa5S2aabdBPjxRDxhWKkbfCc9/FiC82lwSc+ulGZHH44g==","shasum":"f55c99fb3ffef08b24bab414b7d2f24b30d12081","tarball":"http://registry-npm.ywwl.com/node-rsa/download/node-rsa-2.0.0.tgz","fileCount":14,"unpackedSize":1443177,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/node-rsa@2.0.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCICZjQNwcoIltKaBORbIfcdS7nRKrHZc9sp+5VBuvezikAiANIMOHOacqZRTvkRW0OcRF2IvJDn4Yq01R8gBT2xWxYQ=="}],"size":312037,"noattachment":false,"key":"/node-rsa/-/node-rsa-2.0.0.tgz"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:472bf738-3fbe-4ae3-aca4-f6df691925dc"}},"directories":{},"maintainers":[{"name":"rzcoder","email":"rzcoder@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/node-rsa_2.0.0_1779586904003_0.7904495691341162"},"_hasShrinkwrap":false,"readme":"# Node-RSA\n\n[![npm version](https://img.shields.io/npm/v/node-rsa.svg)](https://www.npmjs.com/package/node-rsa)\n[![CI](https://github.com/rzcoder/node-rsa/actions/workflows/ci.yml/badge.svg)](https://github.com/rzcoder/node-rsa/actions/workflows/ci.yml)\n[![npm downloads](https://img.shields.io/npm/dm/node-rsa.svg)](https://www.npmjs.com/package/node-rsa)\n[![license](https://img.shields.io/npm/l/node-rsa.svg)](https://github.com/rzcoder/node-rsa/blob/master/LICENSE)\n\nRSA library for Node.js and browsers.\n\n* Pure TypeScript\n* Works in Node.js and modern browsers (no Buffer/crypto polyfills needed)\n* Generating keys\n* Encrypting and decrypting, with long-message support\n* Signing and verifying\n\n## Table of contents\n\n* [Example](#example)\n* [Installing](#installing)\n* [Usage](#usage)\n  * [Create instance](#create-instance)\n  * [Import/Export keys](#importexport-keys)\n  * [Properties](#properties)\n  * [Encrypting/Decrypting](#encryptingdecrypting)\n  * [Signing/Verifying](#signingverifying)\n* [Browser usage](#browser-usage)\n* [Security notes](#security-notes)\n* [Migrating](#migrating)\n* [Changelog](#changelog)\n* [License](#license)\n* [Acknowledgements](#acknowledgements)\n\n## Example\n\n```ts\nimport NodeRSA from 'node-rsa';\n\nconst key = new NodeRSA({ b: 2048 });\n\nconst text = 'Hello RSA!';\nconst encrypted = key.encrypt(text, 'base64');\nconsole.log('encrypted:', encrypted);\nconst decrypted = key.decrypt(encrypted, 'utf8');\nconsole.log('decrypted:', decrypted);\n```\n\n## Installing\n\n```shell\nnpm install node-rsa\n```\n> <sub>Requires Node.js >= 20. For browsers, any bundler with conditional-exports support (Vite, Webpack 5, Rollup, esbuild, Parcel) picks the browser entry automatically.</sub>\n\n### Testing\n\n```shell\nnpm test\n```\n\n## Usage\n\n### Create instance\n\n```ts\nimport NodeRSA from 'node-rsa';\n\nconst key = new NodeRSA([keyData, [format]], [options]);\n```\n\n* `keyData` — `string | Uint8Array | object` — key data in one of the supported formats, or a generation spec.\n* `format` — `string` — format id for importing the key. See [Import/Export](#importexport-keys).\n* `options` — `object` — additional settings (below).\n\n#### Options\n\nYou can pass options as the second/third constructor argument, or later via `key.setOptions()`.\n\n* `environment` — `'node'` or `'browser'`. Auto-detected from the loaded bundle; the option mainly exists to force the pure-JS engine on Node (`setOptions({ environment: 'browser' })`), bypassing the `node:crypto` fast path — useful if you need PSS with a custom MGF.\n* `bigIntImpl` — `'native'` or `'jsbn'`. The browser bundle defaults to native ES2020 `BigInt`; the Node bundle uses jsbn. Switch only **before** importing/generating a key; switching on a populated instance throws.\n* `signingScheme` — scheme used for `sign` / `verify`. One of `'pss'` (default), `'pkcs1'`, or a `'scheme-hash'` shorthand (e.g. `'pkcs1-sha512'`). Object form is also accepted: `{ scheme: 'pss', hash: 'sha256', saltLength?: number, mgf?: MaskGenerationFunction }`. Default hash is `sha256`.\n* `encryptionScheme` — padding scheme for `encrypt` / `decrypt`. One of `'pkcs1_oaep'` (default) or `'pkcs1'`. Object form: `{ scheme: 'pkcs1_oaep', hash: 'sha1', mgf?, label? }`. Default OAEP hash is `sha1`.\n\n> *Note:* Supported hash algorithms are `'md5'`, `'ripemd160'`, `'sha1'`, `'sha256'`, `'sha512'` in both environments, plus `'md4'`, `'sha224'`, `'sha384'` on Node. `'md4'` additionally requires running Node with `--openssl-legacy-provider`.\n\n#### Creating an \"empty\" key\n\n```ts\nconst key = new NodeRSA();\n```\n\n#### Generate new 2048-bit key\n\n```ts\nconst key = new NodeRSA({ b: 2048 });\n```\n\nOr:\n\n```ts\nkey.generateKeyPair([bits], [exp]);\n```\n\n* `bits` — `number` — key size in bits. 2048 by default.\n* `exp` — `number` — public exponent. 65537 by default.\n\n#### Load key from PEM string\n\n```ts\nconst key = new NodeRSA(\n  '-----BEGIN RSA PRIVATE KEY-----\\n' +\n  'MIIBOQIBAAJAVY6quuzCwyOWzymJ7C4zXjeV/232wt2ZgJZ1kHzjI73wnhQ3WQcL\\n' +\n  'DFCSoi2lPUW8/zspk0qWvPdtp6Jg5Lu7hwIDAQABAkBEws9mQahZ6r1mq2zEm3D/\\n' +\n  'VM9BpV//xtd6p/G+eRCYBT2qshGx42ucdgZCYJptFoW+HEx/jtzWe74yK6jGIkWJ\\n' +\n  'AiEAoNAMsPqwWwTyjDZCo9iKvfIQvd3MWnmtFmjiHoPtjx0CIQCIMypAEEkZuQUi\\n' +\n  'pMoreJrOlLJWdc0bfhzNAJjxsTv/8wIgQG0ZqI3GubBxu9rBOAM5EoA4VNjXVigJ\\n' +\n  'QEEk1jTkp8ECIQCHhsoq90mWM/p9L5cQzLDWkTYoPI49Ji+Iemi2T5MRqwIgQl07\\n' +\n  'Es+KCn25OKXR/FJ5fu6A6A+MptABL3r8SEjlpLc=\\n' +\n  '-----END RSA PRIVATE KEY-----',\n);\n```\n\n### Import/Export keys\n\n```ts\nkey.importKey(keyData, [format]);\nkey.exportKey([format]);\n```\n\n* `keyData` — may be:\n    * PEM string (or a `Uint8Array`/`Buffer` containing one)\n    * `Uint8Array` containing raw DER\n    * object with raw key components\n* `format` — `string` — format id for import/export.\n\n#### Format string syntax\n\n`scheme-[key_type]-[output_type]`\n\n**Scheme** — node-rsa supports several:\n\n  * `'pkcs1'` — public PEM starts with `-----BEGIN RSA PUBLIC KEY-----`, private with `-----BEGIN RSA PRIVATE KEY-----`.\n  * `'pkcs8'` — public PEM starts with `-----BEGIN PUBLIC KEY-----`, private with `-----BEGIN PRIVATE KEY-----`.\n  * `'openssh'` — public starts with `ssh-rsa`, private with `-----BEGIN OPENSSH PRIVATE KEY-----`.\n  * `'components'` — raw modulus/exponent and CRT params. For a private key all components must be present; for a public key only `n` and `e`. All components are `Uint8Array` except `e`, which may be `Uint8Array` or a plain `number`.\n\n**Key type** — `'private'` (default) or `'public'`.\n\n**Output type**:\n\n * `'pem'` — base64 PEM string with header/footer. Used by default.\n * `'der'` — `Uint8Array` of binary DER.\n\n> *Note:* For import, if `keyData` is a PEM string (or a `Uint8Array` containing PEM), you can omit `format`. If it's raw DER, you must specify the format string.\n\n**Shortcuts and examples**\n\n * `'private'` ≡ `'pkcs1'` ≡ `'pkcs1-private'` ≡ `'pkcs1-private-pem'` — private key, PKCS#1, PEM.\n * `'public'` ≡ `'pkcs8-public'` ≡ `'pkcs8-public-pem'` — public key, PKCS#8, PEM.\n * `'pkcs8'` ≡ `'pkcs8-private'` ≡ `'pkcs8-private-pem'` — private key, PKCS#8, PEM.\n * `'pkcs1-der'` ≡ `'pkcs1-private-der'` — private key, PKCS#1, binary DER.\n * `'pkcs8-public-der'` — public key, PKCS#8, binary DER.\n\n**Code example**\n\n```ts\nconst keyData = '-----BEGIN PUBLIC KEY----- ... -----END PUBLIC KEY-----';\nkey.importKey(keyData, 'pkcs8');\nconst publicDer = key.exportKey('pkcs8-public-der');\nconst privateDer = key.exportKey('pkcs1-der');\n```\n\n```ts\nconst hex = (s: string) =>\n  Uint8Array.from(s.match(/.{2}/g)!.map((b) => parseInt(b, 16)));\n\nkey.importKey({\n  n: hex('0086fa9ba066685845fc03833a9699c8baefb53cfbf19052a7f10f1eaa30488cec1ceb752bdff2df9fad6c64b3498956e7dbab4035b4823c99a44cc57088a23783'),\n  e: 65537,\n  d: hex('5d2f0dd982596ef781affb1cab73a77c46985c6da2aafc252cea3f4546e80f40c0e247d7d9467750ea1321cc5aa638871b3ed96d19dcc124916b0bcb296f35e1'),\n  p: hex('00c59419db615e56b9805cc45673a32d278917534804171edcf925ab1df203927f'),\n  q: hex('00aee3f86b66087abc069b8b1736e38ad6af624f7ea80e70b95f4ff2bf77cd90fd'),\n  dmp1: hex('008112f5a969fcb56f4e3a4c51a60dcdebec157ee4a7376b843487b53844e8ac85'),\n  dmq1: hex('1a7370470e0f8a4095df40922a430fe498720e03e1f70d257c3ce34202249d21'),\n  coeff: hex('00b399675e5e81506b729a777cc03026f0b2119853dfc5eb124610c0ab82999e45'),\n}, 'components');\n\nconst publicComponents = key.exportKey('components-public');\nconsole.log(publicComponents);\n\n/*\n{ n: Uint8Array(65) [0, 134, 250, 155, 160, 102, 104, 88, 69, 252, 3, 131, 58, ... ],\n  e: 65537 }\n*/\n```\n\nTo import only the public part use `'components-public'`:\n\n```ts\nkey.importKey({\n  n: hex('0086fa9ba066685845fc03833a9699c8baefb53cfbf19052a7f10f1eaa30488cec1ceb752bdff2df9fad6c64b3498956e7dbab4035b4823c99a44cc57088a23783'),\n  e: 65537,\n}, 'components-public');\n```\n\n> *Note:* `Buffer` is a `Uint8Array` subclass on Node, so any code that passes `Buffer.from(...)` still works — the types document the cross-platform shape.\n\n### Properties\n\n#### Key testing\n```ts\nkey.isPrivate();\nkey.isPublic([strict]);\n```\n`strict` — `boolean` — if `true`, returns `false` when the key pair also contains a private exponent. Default `false`.\n\n```ts\nkey.isEmpty();\n```\nReturns `true` if the instance has no key data.\n\n#### Key info\n```ts\nkey.getKeySize();\n```\nReturns key size in bits.\n\n```ts\nkey.getMaxMessageSize();\n```\nReturns the max data size for a single encrypt operation, in bytes (scheme-dependent).\n\n### Encrypting/Decrypting\n\n```ts\nkey.encrypt(buffer, [encoding], [source_encoding]);\nkey.encryptPrivate(buffer, [encoding], [source_encoding]); // encrypt with private key\n```\nReturns the encrypted data.\n\n* `buffer` — data to encrypt. May be `string`, `Uint8Array` (or `Buffer` on Node), `number`, plain object, or array. Objects and arrays are JSON-stringified first.\n* `encoding` — output encoding: `'buffer'` (default — returns `Uint8Array`), `'binary'`, `'hex'`, or `'base64'`.\n* `source_encoding` — only used when `buffer` is a string; how to interpret its bytes. Accepts `'utf8'` (default), `'hex'`, `'base64'`, `'binary'`.\n\n```ts\nkey.decrypt(buffer, [encoding]);\nkey.decryptPublic(buffer, [encoding]); // decrypt with public key\n```\nReturns the decrypted data.\n\n* `buffer` — `Uint8Array` or base64-encoded string.\n* `encoding` — output: `'buffer'` (default, raw `Uint8Array`), `'utf8'`, `'hex'`, `'base64'`, `'binary'`, or `'json'` (UTF-8 decoded + `JSON.parse`).\n\n> *Note:* `encryptPrivate` / `decryptPublic` always use PKCS#1 v1.5 type-1 padding (deterministic), regardless of the configured `encryptionScheme`.\n\n### Signing/Verifying\n```ts\nkey.sign(buffer, [encoding], [source_encoding]);\n```\nReturns the signature. All arguments behave like `encrypt`.\n\n```ts\nkey.verify(buffer, signature, [source_encoding], [signature_encoding]);\n```\nReturns `true` / `false`.\n\n* `buffer` — data that was signed; same shape as for `encrypt`.\n* `signature` — `Uint8Array` or string, as produced by `sign`.\n* `source_encoding` — encoding for `buffer` if it's a string. Default `'utf8'`.\n* `signature_encoding` — encoding of `signature`. One of `'buffer'` (default), `'binary'`, `'hex'`, `'base64'`.\n\n## Browser usage\n\nThe browser bundle (`dist/index.browser.js`) is published as ESM only and contains no Node-builtin imports — you don't need to polyfill Buffer, crypto, or process. Bundlers that honour the `\"browser\"` export condition (Vite, Webpack 5, Rollup, esbuild, Parcel) pick it up automatically. The bundle weighs ~114 KB raw / ~28 KB gzipped.\n\nIf your bundler doesn't resolve conditional exports, import the browser entry directly:\n\n```ts\nimport NodeRSA from 'node-rsa/dist/index.browser.js';\n```\n\n## Security notes\n\n* **PKCS#1 v1.5 encryption** (`encryptionScheme: 'pkcs1'`) is vulnerable to Bleichenbacher-style padding-oracle attacks when used to decrypt attacker-controlled ciphertexts. The library closes the internal differential timing channel but cannot eliminate the binary valid/invalid oracle inherent to the scheme. Use the default `'pkcs1_oaep'` for new code and for any path that touches attacker-controlled ciphertext.\n\n## Migrating\n\nMigrating from 1.x? See [MIGRATION.md](MIGRATION.md) for the behaviour-change summary and step-by-step walkthrough.\n\n## Changelog\n\nRelease notes and per-version changes are tracked in [CHANGELOG.md](CHANGELOG.md).\n\n## License\n\nCopyright (c) 2014 rzcoder\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n## Licensing for code used in rsa.ts and jsbn\n\nCopyright (c) 2003-2005 Tom Wu\nAll Rights Reserved.\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\nTHE SOFTWARE IS PROVIDED \"AS-IS\" AND WITHOUT WARRANTY OF ANY KIND,\nEXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY\nWARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.\n\nIN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,\nINDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER\nRESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF\nTHE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT\nOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n\nIn addition, the following condition applies:\n\nAll redistributions must retain an intact copy of this copyright notice\nand disclaimer.\n\n## Acknowledgements\n\n* Tom Wu — original [jsbn](http://www-cs-students.stanford.edu/~tjw/jsbn/) BigInteger and RSA implementations\n* Paul Miller — [@noble/hashes](https://github.com/paulmillr/noble-hashes) v2.x, audited synchronous hash functions (MD5, RIPEMD-160, SHA-1/2 family) used by the browser bundle\n","publish_time":1779586904229,"_cnpm_publish_time":1779586904229,"dist-tags":{"latest":"2.0.0","next":"2.0.0-rc.0"}}