Gathering detailed insights and metrics for node-rsa
Gathering detailed insights and metrics for node-rsa
Gathering detailed insights and metrics for node-rsa
Gathering detailed insights and metrics for node-rsa
npm install node-rsa
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,389 Stars
247 Commits
205 Forks
31 Watching
7 Branches
18 Contributors
Updated on 24 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-8.8%
137,629
Compared to previous day
Last week
-0.3%
757,279
Compared to previous week
Last month
11.6%
3,149,561
Compared to previous month
Last year
8.7%
34,183,003
Compared to previous year
1
7
Node.js RSA library
Based on jsbn library from Tom Wu http://www-cs-students.stanford.edu/~tjw/jsbn/
1const NodeRSA = require('node-rsa'); 2const key = new NodeRSA({b: 512}); 3 4const text = 'Hello RSA!'; 5const encrypted = key.encrypt(text, 'base64'); 6console.log('encrypted: ', encrypted); 7const decrypted = key.decrypt(encrypted, 'utf8'); 8console.log('decrypted: ', decrypted);
1npm install node-rsa
Requires nodejs >= 8.11.1
1npm test
This library developed and tested primary for Node.js, but it still can work in browsers with browserify.
1const NodeRSA = require('node-rsa'); 2 3const key = new NodeRSA([keyData, [format]], [options]);
{string|buffer|object}
— parameters for generating key or the key in one of supported formats.{string}
— format for importing key. See more details about formats in Export/Import section.{object}
— additional settings.You can specify some options by second/third constructor argument, or over key.setOptions()
method.
'browser'
— will run pure js implementation of RSA algorithms.'node'
for nodejs >= 0.10.x or io.js >= 1.x
— provide some native methods like sign/verify and encrypt/decrypt.'pkcs1_oaep'
or 'pkcs1'
. Default 'pkcs1_oaep'
.'pkcs1'
or 'pss'
or 'scheme-hash' format string (eg 'pss-sha1'
). Default 'pkcs1-sha256'
, or, if chosen pss: 'pss-sha1'
.Notice: This lib supporting next hash algorithms:
'md5'
,'ripemd160'
,'sha1'
,'sha256'
,'sha512'
in browser and node environment and additional'md4'
,'sha'
,'sha224'
,'sha384'
in node only.
1const key = new NodeRSA();
1const key = new NodeRSA({b: 512});
Also you can use next method:
1key.generateKeyPair([bits], [exp]);
{int}
— key size in bits. 2048 by default.{int}
— public exponent. 65537 by default.1const key = new NodeRSA('-----BEGIN RSA PRIVATE KEY-----\n'+ 2 'MIIBOQIBAAJAVY6quuzCwyOWzymJ7C4zXjeV/232wt2ZgJZ1kHzjI73wnhQ3WQcL\n'+ 3 'DFCSoi2lPUW8/zspk0qWvPdtp6Jg5Lu7hwIDAQABAkBEws9mQahZ6r1mq2zEm3D/\n'+ 4 'VM9BpV//xtd6p/G+eRCYBT2qshGx42ucdgZCYJptFoW+HEx/jtzWe74yK6jGIkWJ\n'+ 5 'AiEAoNAMsPqwWwTyjDZCo9iKvfIQvd3MWnmtFmjiHoPtjx0CIQCIMypAEEkZuQUi\n'+ 6 'pMoreJrOlLJWdc0bfhzNAJjxsTv/8wIgQG0ZqI3GubBxu9rBOAM5EoA4VNjXVigJ\n'+ 7 'QEEk1jTkp8ECIQCHhsoq90mWM/p9L5cQzLDWkTYoPI49Ji+Iemi2T5MRqwIgQl07\n'+ 8 'Es+KCn25OKXR/FJ5fu6A6A+MptABL3r8SEjlpLc=\n'+ 9 '-----END RSA PRIVATE KEY-----');
1key.importKey(keyData, [format]); 2key.exportKey([format]);
{string|buffer}
— may be:
{string}
— format id for export/import.Format string composed of several parts: scheme-[key_type]-[output_type]
Scheme — NodeRSA supports multiple format schemes for import/export keys:
'pkcs1'
— public key starts from '-----BEGIN RSA PUBLIC KEY-----'
header and private key starts from '-----BEGIN RSA PRIVATE KEY-----'
header'pkcs8'
— public key starts from '-----BEGIN PUBLIC KEY-----'
header and private key starts from '-----BEGIN PRIVATE KEY-----'
header'openssh'
— public key starts from 'ssh-rsa'
header and private key starts from '-----BEGIN OPENSSH PRIVATE KEY-----'
header'components'
— use it for import/export key from/to raw components (see example below). For private key, importing data should contain all private key components, for public key: only public exponent (e
) and modulus (n
). All components (except e
) should be Buffer, e
could be Buffer or just normal Number.Key type — can be 'private'
or 'public'
. Default 'private'
Output type — can be:
'pem'
— Base64 encoded string with header and footer. Used by default.'der'
— Binary encoded key data.Notice: For import, if keyData is PEM string or buffer containing string, you can do not specify format, but if you provide keyData as DER you must specify it in format string.
Shortcuts and examples
'private'
or 'pkcs1'
or 'pkcs1-private'
== 'pkcs1-private-pem'
— private key encoded in pcks1 scheme as pem string.'public'
or 'pkcs8-public'
== 'pkcs8-public-pem'
— public key encoded in pcks8 scheme as pem string.'pkcs8'
or 'pkcs8-private'
== 'pkcs8-private-pem'
— private key encoded in pcks8 scheme as pem string.'pkcs1-der'
== 'pkcs1-private-der'
— private key encoded in pcks1 scheme as binary buffer.'pkcs8-public-der'
— public key encoded in pcks8 scheme as binary buffer.Code example
1const keyData = '-----BEGIN PUBLIC KEY----- ... -----END PUBLIC KEY-----'; 2key.importKey(keyData, 'pkcs8'); 3const publicDer = key.exportKey('pkcs8-public-der'); 4const privateDer = key.exportKey('pkcs1-der');
1key.importKey({ 2 n: Buffer.from('0086fa9ba066685845fc03833a9699c8baefb53cfbf19052a7f10f1eaa30488cec1ceb752bdff2df9fad6c64b3498956e7dbab4035b4823c99a44cc57088a23783', 'hex'), 3 e: 65537, 4 d: Buffer.from('5d2f0dd982596ef781affb1cab73a77c46985c6da2aafc252cea3f4546e80f40c0e247d7d9467750ea1321cc5aa638871b3ed96d19dcc124916b0bcb296f35e1', 'hex'), 5 p: Buffer.from('00c59419db615e56b9805cc45673a32d278917534804171edcf925ab1df203927f', 'hex'), 6 q: Buffer.from('00aee3f86b66087abc069b8b1736e38ad6af624f7ea80e70b95f4ff2bf77cd90fd', 'hex'), 7 dmp1: Buffer.from('008112f5a969fcb56f4e3a4c51a60dcdebec157ee4a7376b843487b53844e8ac85', 'hex'), 8 dmq1: Buffer.from('1a7370470e0f8a4095df40922a430fe498720e03e1f70d257c3ce34202249d21', 'hex'), 9 coeff: Buffer.from('00b399675e5e81506b729a777cc03026f0b2119853dfc5eb124610c0ab82999e45', 'hex') 10}, 'components'); 11const publicComponents = key.exportKey('components-public'); 12console.log(publicComponents); 13 14/* 15{ n: <Buffer 00 86 fa 9b a0 66 68 58 45 fc 03 83 3a 96 99 c8 ba ef b5 3c fb f1 90 52 a7 f1 0f 1e aa 30 48 8c ec 1c eb 75 2b df f2 df 9f ad 6c 64 b3 49 89 56 e7 db ... >, 16 e: 65537 17} 18*/
If you want to only import the public key use 'components-public'
as an option:
1key.importKey({ 2 n: Buffer.from('0086fa9ba066685845fc03833a9699c8baefb53cfbf19052a7f10f1eaa30488cec1ceb752bdff2df9fad6c64b3498956e7dbab4035b4823c99a44cc57088a23783', 'hex'), 3 e: 65537, 4}, 'components-public');
1key.isPrivate(); 2key.isPublic([strict]);
strict — {boolean}
— if true method will return false if key pair have private exponent. Default false
.
1key.isEmpty();
Return true
if key pair doesn't have any data.
1key.getKeySize();
Return key size in bits.
1key.getMaxMessageSize();
Return max data size for encrypt in bytes.
1key.encrypt(buffer, [encoding], [source_encoding]); 2key.encryptPrivate(buffer, [encoding], [source_encoding]); // use private key for encryption
Return encrypted data.
{buffer}
— data for encrypting, may be string, Buffer, or any object/array. Arrays and objects will encoded to JSON string first.{string}
— encoding for output result, may be 'buffer'
, 'binary'
, 'hex'
or 'base64'
. Default 'buffer'
.{string}
— source encoding, works only with string buffer. Can take standard Node.js Buffer encodings (hex, utf8, base64, etc). 'utf8'
by default.1key.decrypt(buffer, [encoding]); 2key.decryptPublic(buffer, [encoding]); // use public key for decryption
Return decrypted data.
{buffer}
— data for decrypting. Takes Buffer object or base64 encoded string.{string}
— encoding for result string. Can also take 'buffer'
for raw Buffer object, or 'json'
for automatic JSON.parse result. Default 'buffer'
.Notice:
encryptPrivate
anddecryptPublic
using only pkcs1 padding type 1 (not random)
1key.sign(buffer, [encoding], [source_encoding]);
Return signature for buffer. All the arguments are the same as for encrypt
method.
1key.verify(buffer, signature, [source_encoding], [signature_encoding])
Return result of check, true
or false
.
{buffer}
— data for check, same as encrypt
method.{string}
— signature for check, result of sign
method.{string}
— same as for encrypt
method.{string}
— encoding of given signature. May be 'buffer'
, 'binary'
, 'hex'
or 'base64'
. Default 'buffer'
.Questions, comments, bug reports, and pull requests are all welcome.
importKey()
now returns this
new Buffer()
call as deprecated was replaced by Buffer.from
& Buffer.alloc
.sha
(was removed in node ~10). sha1
, sha256
and others still works.no padding
scheme will padded data with zeros on all environments.PKCS1 no padding
scheme support.encryptPrivate
and decryptPublic
now using only pkcs1 (type 1) padding..encryptPrivate()
and .decryptPublic()
methods..exportPrivate()
and .exportPublic()
was replaced by .exportKey([format])
.
.exportKey()
returns private key as .exportPrivate()
, if you need public key from .exportPublic()
you must specify format as 'public'
or 'pkcs8-public-pem'
..importKey(key, [format])
now has second argument..getPublicPEM()
method was renamed to .exportPublic()
.getPrivatePEM()
method was renamed to .exportPrivate()
.loadFromPEM()
method was renamed to .importKey()
'md5'
, 'ripemd160'
, 'sha1'
, 'sha256'
, 'sha512'
hash algorithms in both environments
and additional 'md4'
, 'sha'
, 'sha224'
, 'sha384'
for nodejs env.options.signingAlgorithm
was renamed to options.signingScheme
encryptingScheme
option.key.options
now mark as private. Added key.setOptions(options)
method.fs.readFileSync()
output).isEmpty()
method.options.signingAlgorithm
now takes only hash-algorithm name..getKeySize()
and .getMaxMessageSize()
methods..loadFromPublicPEM
and .loadFromPrivatePEM
methods marked as private.Copyright (c) 2014 rzcoder
Permission 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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE 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.
Copyright (c) 2003-2005 Tom Wu
All Rights Reserved.
Permission 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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
In addition, the following condition applies:
All redistributions must retain an intact copy of this copyright notice and disclaimer.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 4/11 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
license file not detected
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
21 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More