Skip to content

Commit 9ce6449

Browse files
committed
signing wip
1 parent 273957f commit 9ce6449

File tree

3 files changed

+73
-43
lines changed

3 files changed

+73
-43
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,19 @@ key.isPublic([strict]);
7171

7272
### Encrypting/decrypting
7373
```js
74-
key.encrypt(buffer, [source_encoding], [output_encoding]);
74+
key.encrypt(buffer, [encoding], [source_encoding]);
7575
```
7676
**buffer** - data for encrypting, may be string, Buffer, or any object/array. Arrays and objects will encoded to JSON string first.<br/>
77+
**encoding** - encoding for output result, may be 'buffer', 'binary', 'hex' or 'base64'. Default *buffer*.
7778
**source_encoding** - source encoding, works only with string buffer. Can take standard Node.js Buffer encodings (hex, utf8, base64, etc). *Utf8* by default.<br/>
78-
**output_encoding** - encoding for output result, can also take 'buffer' to return Buffer object. Default *base64*.
7979

8080
```js
8181
key.decrypt(buffer, [encoding]);
8282
```
83-
8483
**buffer** - data for decrypting. Takes Buffer object or base64 encoded string.<br/>
85-
**encoding** - encoding for result string. Can also take 'buffer' for raw Buffer object, or 'json' for automatic JSON.parse result.
84+
**encoding** - encoding for result string. Can also take 'buffer' for raw Buffer object, or 'json' for automatic JSON.parse result. Default 'buffer'.
85+
86+
### Signing/Verifying
8687

8788
## Contributing
8889

src/NodeRSA.js

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,17 @@ module.exports = (function() {
129129
* Encrypting data method
130130
*
131131
* @param buffer {string|number|object|array|Buffer} - data for encrypting. Object and array will convert to JSON string.
132+
* @param encoding {string} - optional. Encoding for output result, may be 'buffer', 'binary', 'hex' or 'base64'. Default 'buffer'.
132133
* @param source_encoding {string} - optional. Encoding for given string. Default utf8.
133-
* @param output_encoding {string} - optional. Encoding for output result, can also take 'buffer' to return Buffer object. Default base64.
134134
* @returns {string|Buffer}
135135
*/
136-
NodeRSA.prototype.encrypt = function(buffer, source_encoding, output_encoding) {
137-
var res = null;
136+
NodeRSA.prototype.encrypt = function(buffer, encoding, source_encoding) {
137+
var res = this.keyPair.encrypt(this.$getDataForEcrypt(buffer, source_encoding));
138138

139-
if (_.isString(buffer) || _.isNumber(buffer)) {
140-
res = this.keyPair.encrypt(new Buffer('' + buffer, source_encoding || 'utf8'));
141-
} else if (Buffer.isBuffer(buffer)) {
142-
res = this.keyPair.encrypt(buffer);
143-
} else if (_.isObject(buffer)) {
144-
res = this.keyPair.encrypt(new Buffer(JSON.stringify(buffer)));
145-
}
146-
147-
if (output_encoding == 'buffer') {
139+
if (encoding == 'buffer' || !encoding) {
148140
return res;
149141
} else {
150-
return res.toString(output_encoding || 'base64');
142+
return res.toString(encoding);
151143
}
152144
};
153145

@@ -159,31 +151,22 @@ module.exports = (function() {
159151
* @returns {Buffer|object|string}
160152
*/
161153
NodeRSA.prototype.decrypt = function(buffer, encoding) {
162-
encoding = encoding || 'utf8';
163-
164154
buffer = _.isString(buffer) ? new Buffer(buffer, 'base64') : buffer;
165-
var res = this.keyPair.decrypt(buffer);
166-
167-
if (encoding == 'buffer') {
168-
return res;
169-
} else if (encoding == 'json') {
170-
return JSON.parse(res.toString());
171-
} else {
172-
return res.toString(encoding);
173-
}
155+
return this.$getDecryptedData(this.keyPair.decrypt(buffer), encoding);
174156
};
175157

176158
/**
177159
* Signing data
178160
*
179-
* @param buffer - data for signing
180-
* @param encoding - output encoding. May be 'buffer', 'binary', 'hex' or 'base64'. Default 'buffer'.
181-
* @returns {*}
161+
* @param buffer {string|number|object|array|Buffer} - data for signing. Object and array will convert to JSON string.
162+
* @param encoding {string} - optional. Encoding for output result, may be 'buffer', 'binary', 'hex' or 'base64'. Default 'buffer'.
163+
* @param source_encoding {string} - optional. Encoding for given string. Default utf8.
164+
* @returns {string|Buffer}
182165
*/
183-
NodeRSA.prototype.sign = function(buffer, encoding) {
166+
NodeRSA.prototype.sign = function(buffer, encoding, source_encoding) {
184167
encoding = (!encoding || encoding == 'buffer' ? null : encoding)
185168
var signer = crypt.createSign('RSA-SHA256');
186-
signer.update(buffer);
169+
signer.update(this.$getDataForEcrypt(buffer, source_encoding));
187170
return signer.sign(this.getPrivatePEM(), encoding);
188171
}
189172

@@ -192,7 +175,7 @@ module.exports = (function() {
192175
*
193176
* @param buffer - signed data
194177
* @param signature
195-
* @param signature_encoding - encoding of given signature. May be 'buffer', 'binary', 'hex' or 'base64'. Default 'buffer'.
178+
* @param signature_encoding - optional. Encoding of given signature. May be 'buffer', 'binary', 'hex' or 'base64'. Default 'buffer'.
196179
* @returns {*}
197180
*/
198181
NodeRSA.prototype.verify = function(buffer, signature, signature_encoding) {
@@ -210,6 +193,44 @@ module.exports = (function() {
210193
return this.$cache.publicPEM
211194
}
212195

196+
/**
197+
* Preparing given data for encrypting/signing. Just make new/return Buffer object.
198+
*
199+
* @param buffer {string|number|object|array|Buffer} - data for encrypting. Object and array will convert to JSON string.
200+
* @param source_encoding {string} - optional. Encoding for given string. Default utf8.
201+
* @returns {Buffer}
202+
*/
203+
NodeRSA.prototype.$getDataForEcrypt = function(buffer, encoding) {
204+
if (_.isString(buffer) || _.isNumber(buffer)) {
205+
return new Buffer('' + buffer, encoding || 'utf8');
206+
} else if (Buffer.isBuffer(buffer)) {
207+
return buffer;
208+
} else if (_.isObject(buffer)) {
209+
return new Buffer(JSON.stringify(buffer));
210+
} else {
211+
throw Error("Unexpected data type")
212+
}
213+
}
214+
215+
/**
216+
*
217+
* @param buffer {Buffer} - decrypted data.
218+
* @param encoding - optional. Encoding for result output. May be 'buffer', 'json' or any of Node.js Buffer supported encoding.
219+
* @returns {*}
220+
*/
221+
NodeRSA.prototype.$getDecryptedData = function(buffer, encoding) {
222+
encoding = encoding || 'buffer';
223+
224+
if (encoding == 'buffer') {
225+
return buffer;
226+
} else if (encoding == 'json') {
227+
return JSON.parse(buffer.toString());
228+
} else {
229+
return buffer.toString(encoding);
230+
}
231+
};
232+
233+
213234
/**
214235
* private
215236
* Recalculating properties
@@ -224,6 +245,10 @@ module.exports = (function() {
224245
* @returns {string} private PEM string
225246
*/
226247
NodeRSA.prototype.$makePrivatePEM = function() {
248+
if (!this.isPrivate()) {
249+
return null;
250+
}
251+
227252
var n = this.keyPair.n.toBuffer();
228253
var d = this.keyPair.d.toBuffer();
229254
var p = this.keyPair.p.toBuffer();
@@ -257,6 +282,10 @@ module.exports = (function() {
257282
* @returns {string} public PEM string
258283
*/
259284
NodeRSA.prototype.$makePublicPEM = function() {
285+
if (!this.isPublic()) {
286+
return null;
287+
}
288+
260289
var n = this.keyPair.n.toBuffer();
261290
var length = n.length + 512; // magic
262291

test/tests.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,40 +118,40 @@ describe('NodeRSA', function(){
118118

119119
describe('Encrypting', function(){
120120
it('.encrypt() should return Buffer object', function(){
121-
encryptedBuffer = nodeRSA.encrypt(dataForEncrypt, null, 'buffer');
121+
encryptedBuffer = nodeRSA.encrypt(dataForEncrypt, 'buffer');
122122
assert(Buffer.isBuffer(encryptedBuffer));
123123
});
124124

125125
it('.encrypt() should return base64 encrypted string', function(){
126-
encrypted = nodeRSA.encrypt(dataForEncrypt);
126+
encrypted = nodeRSA.encrypt(dataForEncrypt, 'base64');
127127
assert.isString(encrypted);
128128
assert.match(encrypted, /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/);
129129
});
130130

131131
it('.encrypt() should return encrypted Buffer for long message', function(){
132-
encryptedLong = nodeRSA.encrypt(longDataForEncrypt, null, 'buffer');
132+
encryptedLong = nodeRSA.encrypt(longDataForEncrypt, 'buffer');
133133
assert(Buffer.isBuffer(encryptedLong));
134134
});
135135

136136
it('.encrypt() for js object. Should return Buffer object', function(){
137-
encryptedJSON = nodeRSA.encrypt(JSONForEncrypt, null, 'buffer');
137+
encryptedJSON = nodeRSA.encrypt(JSONForEncrypt, 'buffer');
138138
assert(Buffer.isBuffer(encryptedJSON));
139139
});
140140
});
141141

142142
describe('Decrypting', function(){
143143
it('.decrypt() should return decrypted Buffer', function(){
144-
decrypted = nodeRSA.decrypt(encryptedBuffer, 'buffer');
144+
decrypted = nodeRSA.decrypt(encryptedBuffer);
145145
assert(Buffer.isBuffer(decrypted));
146146
});
147147

148148
it('.decrypt() should return decrypted string', function(){
149-
decrypted = nodeRSA.decrypt(new Buffer(encrypted, 'base64'));
149+
decrypted = nodeRSA.decrypt(new Buffer(encrypted, 'base64'), 'utf8');
150150
assert.isString(decrypted);
151151
});
152152

153153
it('.decrypt() should return decrypted string for long message', function(){
154-
decryptedLong = nodeRSA.decrypt(encryptedLong);
154+
decryptedLong = nodeRSA.decrypt(encryptedLong, 'utf8');
155155
assert.isString(decryptedLong);
156156
});
157157

@@ -178,8 +178,8 @@ describe('NodeRSA', function(){
178178

179179
var signed = {};
180180

181-
for(var i in dataForSign) {
182-
var sign = dataForSign[i];
181+
for(var i in dataBundle) {
182+
var sign = dataBundle[i];
183183
var signature = null;
184184

185185
it('should signed '+i, function(){

0 commit comments

Comments
 (0)