File tree Expand file tree Collapse file tree 3 files changed +29
-5
lines changed Expand file tree Collapse file tree 3 files changed +29
-5
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,10 @@ module.exports = (function() {
4747 bits = bits || 2048 ;
4848 exp = 65537 ;
4949
50+ if ( bits % 2 == 1 ) {
51+ throw Error ( 'Key size must be even.' )
52+ }
53+
5054 this . keyPair . generate ( bits , exp . toString ( 16 ) ) ;
5155 this . $recalculateCache ( ) ;
5256 return this ;
@@ -235,6 +239,14 @@ module.exports = (function() {
235239 return this . $cache . publicPEM ;
236240 } ;
237241
242+ NodeRSA . prototype . getKeySize = function ( ) {
243+ return this . keyPair . keySize ;
244+ } ;
245+
246+ NodeRSA . prototype . getMaxMessageSize = function ( ) {
247+ return this . keyPair . maxMessageLength ;
248+ } ;
249+
238250 /**
239251 * Preparing given data for encrypting/signing. Just make new/return Buffer object.
240252 *
Original file line number Diff line number Diff line change @@ -303,6 +303,10 @@ module.exports.Key = (function() {
303303 return m . toBuffer ( ) . toString ( 'hex' ) == hash . toString ( 'hex' ) ;
304304 } ;
305305
306+ Object . defineProperty ( RSAKey . prototype , 'keySize' , {
307+ get : function ( ) { return this . cache . keyBitLength ; }
308+ } ) ;
309+
306310 Object . defineProperty ( RSAKey . prototype , 'encryptedDataLength' , {
307311 get : function ( ) { return this . cache . keyByteLength ; }
308312 } ) ;
@@ -318,6 +322,10 @@ module.exports.Key = (function() {
318322 this . cache = this . cache || { } ;
319323 // Bit & byte length
320324 this . cache . keyBitLength = this . n . bitLength ( ) ;
325+ if ( this . cache . keyBitLength % 2 == 1 ) {
326+ this . cache . keyBitLength = this . cache . keyBitLength + 1 ;
327+ }
328+
321329 this . cache . keyByteLength = ( this . cache . keyBitLength + 6 ) >> 3 ;
322330 } ;
323331
Original file line number Diff line number Diff line change @@ -58,11 +58,15 @@ describe("NodeRSA", function(){
5858 describe ( "Work with keys" , function ( ) {
5959
6060 describe ( "Generating keys" , function ( ) {
61- for ( var i in keySizes ) {
62- it ( "should make key pair " + keySizes [ i ] . b + "-bit length and public exponent is " + ( keySizes [ i ] . e || 65537 ) , function ( ) {
63- generatedKeys . push ( new NodeRSA ( { b : keySizes [ i ] . b , e : keySizes [ i ] . e } ) ) ;
64- assert . instanceOf ( generatedKeys [ generatedKeys . length - 1 ] . keyPair , Object ) ;
65- } ) ;
61+ for ( var size in keySizes ) {
62+ ( function ( size ) {
63+ it ( "should make key pair " + size . b + "-bit length and public exponent is " + ( size . e || 65537 ) , function ( ) {
64+ generatedKeys . push ( new NodeRSA ( { b : size . b , e : size . e } ) ) ;
65+ assert . instanceOf ( generatedKeys [ generatedKeys . length - 1 ] . keyPair , Object ) ;
66+ assert . equal ( generatedKeys [ generatedKeys . length - 1 ] . getKeySize ( ) , size . b ) ;
67+ assert . equal ( generatedKeys [ generatedKeys . length - 1 ] . getMaxMessageSize ( ) , ( size . b / 8 - 11 ) ) ;
68+ } ) ;
69+ } ) ( keySizes [ size ] ) ;
6670 }
6771 } ) ;
6872
You can’t perform that action at this time.
0 commit comments