@@ -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
0 commit comments