Skip to content

midworld/node-rsa

 
 

Repository files navigation

Node RSA

This module provides access to RSA public-key routines from OpenSSL. Support is limited to encryption with a public key, decryption with a private key. Implementation uses OpenSSL RSA keys encoded by using ASN.1 and Base64.

Implementation Notes

  • This module was originally implemented by Chris Andrews.
  • Configurable padding feature was implemented by Eric Laberge.
  • Katyo was found what native encoding feature has bugs, so base64 throws a segmentation fault. Therefore native encoding feature was replaced by implementation which uses NodeJS Buffer object for this purpose.
  • Support node-gyp build on Windows.
  • Support non-blocking encrypt/decrypt functions by Midworld Kim.

Building on Windows

  • C++ Compiler(such as MSVC), Python2.7 required.
  • and node-gyp.
    npm install node-gyp -g
        
  • You should take care of %PATH% and %PYTHON%.

Installation

npm install git+https://github.com/midworld/node-rsa.git

Usage

Importing

var RSA = require('rsa');

Initialization

  • Automated
    var options = {
      publicKey: "RSA Public Key Data (for encryption only)",
      privateKey: "RSA Private Key Data",
      passphrase: "RSA Private Key Passphrase (optional)",
      padding: "RSA Encription Padding (oaep by default)"
    };
    var keypair = RSA.createRsaKeypair(options);
        
  • Manual
    var keypair = new RSA.RsaKeypair();
    keypair.setPublicKey("RSA Public Key Data (for encryption only)");
    keypair.setPrivateKey("RSA Private Key Data", "RSA Private Key Passphrase (optional)");
    keypair.setPadding("RSA Encription Padding (oaep by default)");
        

Application

  • Encryption
    var encrypted = keypair.encryptSync("source data", "source encoding", "encrypted encoding");
    // or
    keypair.encrypt("source data", "source encoding", "encrypted encoding", function (err, encrypted) {});
        
  • Decryption
    var decrypted = keypair.decryptSync("encrypted data", "encrypted encoding", "decrypted encoding");
    // or
    keypair.decrypt("encrypted data", "encrypted encoding", "decrypted encoding", function (err, decrypted) {});
        

See test/test.js and test/getters.js.

Testing

E:\Work\node-rsa-midworld>node test\test.js
done

:

E:\Work\node-rsa-midworld>node test\async-test.js
blocking encrypt/decrypt test(msec): 2610
non-blocking encrypt/decrypt test(msec): 1149 2.27x faster

How to Make RSA Key

  • Private Key
    openssl genrsa -des -out private.pem 2048
        
  • Public key
    openssl rsa -pubout < private.pem > public.pem
        

Description

Padding

Next OpenSSL paddings supported:

PaddingOpenSSL constant
oaepRSA_PKCS1_OAEP_PADDING
pkcs1RSA_PKCS1_PADDING
sslv23RSA_SSLV23_PADDING
noneRSA_NO_PADDING

Note: RSA Encryption without padding insecure.

Keys

Keys must be in PEM format (ASN.1 and Base64 encoded).

Node Versions

This module should work on Node.js 0.8.x with node-gyp.

Licence

BSD, see LICENCE.

About

OpenSSL RSA library for nodejs

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 58.9%
  • JavaScript 34.3%
  • Python 4.6%
  • Shell 2.2%