Skip to content

Commit 1a56ce0

Browse files
committed
long message implementation (wip)
1 parent 3c53700 commit 1a56ce0

File tree

5 files changed

+256
-148
lines changed

5 files changed

+256
-148
lines changed

README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Node-RSA
22

3-
Pure JavaScript Node.js RSA library
4-
No needed OpenSSL
3+
Node.js RSA library
4+
Based on jsbn library from Tom Wu http://www-cs-students.stanford.edu/~tjw/jsbn/
5+
6+
* Pure JavaScript
7+
* No needed OpenSSL
8+
* Supports long messages for encrypt/decrypt
59

6-
Based on jsbn library from Tom Wu http://www-cs-students.stanford.edu/~tjw/jsbn/ and node.js adaptation https://github.com/eschnou/node-bignumber
710

811
## Building and Installing
912

@@ -47,32 +50,51 @@ var key = new NodeRSA('-----BEGIN RSA PRIVATE KEY-----\n'+
4750
Also you can use next methods:
4851

4952
```js
50-
key.generateKeyPair(bits, exp); // exp = 65537 by default
51-
key.loadFromPEM();
53+
key.generateKeyPair([bits], [exp]); // exp = 65537 by default
54+
key.loadFromPEM(pem_string);
5255
```
5356

5457
### Export keys
5558
```js
5659
key.toPrivatePEM();
57-
key.toPublicPEM();
60+
key.toPublicPEM([strict]);
5861
```
62+
* **strict** - if true method will return false if key pair have private exponent. Default *false*.
5963

6064
### Test key
6165
```js
6266
key.isPrivate();
6367
key.isPublic();
6468
```
6569

70+
### Encrypting/decrypting
71+
```js
72+
key.encrypt(buffer, [source_encoding], [output_encoding]);
73+
```
74+
* **buffer** - data for encrypting, may be string, Buffer, or any object/array. Arrays and objects will encoded to JSON string first.
75+
* **source_encoding** - source encoding, works only with string buffer. Can take standard Node.js Buffer encodings (hex, utf8, base64, etc). *Utf8* by default.
76+
* **output_encoding** - encoding for output result, can also take 'buffer' to return Buffer object. Default *base64*.
77+
78+
```js
79+
key.decrypt(buffer, [encoding]);
80+
```
81+
82+
* **buffer** - data for decrypting. Takes Buffer object.
83+
* **encoding** - encoding for result string. Can also take 'buffer' for raw Buffer object, or 'json' for automatic JSON.parse result.
84+
6685

6786
## Contributing
6887

6988
Questions, comments, bug reports, and pull requests are all welcome.
7089

71-
## License For NodeRSA.js
90+
## License for NodeRSA.js
91+
92+
Copyright (c) 2014 rzcoder
93+
All Rights Reserved.
7294

7395
BSD
7496

75-
## Licensing For Code used in rsa.js and jsbn.js
97+
## Licensing for code used in rsa.js and jsbn.js
7698

7799
Copyright (c) 2003-2005 Tom Wu
78100
All Rights Reserved.

src/NodeRSA.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*!
22
* RSA library for Node.js
33
*
44
* Copyright (c) 2014 rzcoder
@@ -177,28 +177,29 @@ module.exports = (function() {
177177

178178
/**
179179
* Check if keypair contains public key
180+
* @param strict {boolean} - public key only, return false if have private exponent
180181
*/
181-
NodeRSA.prototype.isPublic = function() {
182-
return this.keyPair.n && this.keyPair.e;
182+
NodeRSA.prototype.isPublic = function(strict) {
183+
return this.keyPair.n && this.keyPair.e && !(strict && this.keyPair.d);
183184
};
184185

185186
/**
186187
* Encrypting data method
187188
*
188-
* @param buf {string|number|object|array|Buffer} - data for encoding. Object and array will convert to JSON string.
189+
* @param buffer {string|number|object|array|Buffer} - data for encrypting. Object and array will convert to JSON string.
189190
* @param source_encoding {string} - optional. Encoding for given string. Default utf8.
190191
* @param output_encoding {string} - optional. Encoding for output result, can also take 'buffer' to return Buffer object. Default base64.
191192
* @returns {string|Buffer}
192193
*/
193-
NodeRSA.prototype.encrypt = function(buf, source_encoding, output_encoding) {
194+
NodeRSA.prototype.encrypt = function(buffer, source_encoding, output_encoding) {
194195
var res = null;
195196

196-
if (_.isString(buf) || _.isNumber(buf)) {
197-
res = this.keyPair.encrypt(new Buffer('' + buf, source_encoding || 'utf8'));
198-
} else if (Buffer.isBuffer(buf)) {
199-
res = this.keyPair.encrypt(buf);
200-
} else if (_.isObject(buf)) {
201-
res = this.keyPair.encrypt(new Buffer(JSON.stringify(buf)));
197+
if (_.isString(buffer) || _.isNumber(buffer)) {
198+
res = this.keyPair.encrypt(new Buffer('' + buffer, source_encoding || 'utf8'));
199+
} else if (Buffer.isBuffer(buffer)) {
200+
res = this.keyPair.encrypt(buffer);
201+
} else if (_.isObject(buffer)) {
202+
res = this.keyPair.encrypt(new Buffer(JSON.stringify(buffer)));
202203
}
203204

204205
if (output_encoding == 'buffer') {
@@ -211,13 +212,13 @@ module.exports = (function() {
211212
/**
212213
* Decrypting data method
213214
*
214-
* @param buf {Buffer} - buffer to decrypt
215+
* @param buffer {Buffer} - buffer for decrypting
215216
* @param encoding - encoding for result string, can also take 'json' or 'buffer' for the automatic conversion of this type
216-
* @returns {Buffer|string}
217+
* @returns {Buffer|object|string}
217218
*/
218-
NodeRSA.prototype.decrypt = function(buf, encoding) {
219+
NodeRSA.prototype.decrypt = function(buffer, encoding) {
219220
encoding = encoding || 'utf8';
220-
var res = this.keyPair.decrypt(buf);
221+
var res = this.keyPair.decrypt(buffer);
221222

222223
if (encoding == 'buffer') {
223224
return res;

0 commit comments

Comments
 (0)