@@ -55,9 +55,7 @@ module.exports.makeScheme = function (key, options) {
5555 }
5656 if ( this . options . encryptionSchemeOptions && this . options . encryptionSchemeOptions . padding == constants . RSA_NO_PADDING ) {
5757 //RSA_NO_PADDING treated like JAVA left pad with zero character
58- filled = new Buffer ( this . key . maxMessageLength - buffer . length ) ;
59- filled . fill ( 0 ) ;
60- return Buffer . concat ( [ filled , buffer ] ) ;
58+ return this . pkcs0pad ( buffer ) ;
6159 }
6260
6361 /* Type 1: zeros padding for private key encrypt */
@@ -98,13 +96,7 @@ module.exports.makeScheme = function (key, options) {
9896
9997 if ( this . options . encryptionSchemeOptions && this . options . encryptionSchemeOptions . padding == constants . RSA_NO_PADDING ) {
10098 //RSA_NO_PADDING treated like JAVA left pad with zero character
101- var unPad ;
102- if ( typeof buffer . lastIndexOf == "function" ) { //patch for old node version
103- unPad = buffer . slice ( buffer . lastIndexOf ( '\0' ) + 1 , buffer . length ) ;
104- } else {
105- unPad = buffer . slice ( String . prototype . lastIndexOf . call ( buffer , '\0' ) + 1 , buffer . length ) ;
106- }
107- return unPad ;
99+ return this . pkcs0unpad ( buffer ) ;
108100 }
109101
110102 if ( buffer . length < 4 ) {
@@ -181,6 +173,31 @@ module.exports.makeScheme = function (key, options) {
181173 }
182174 } ;
183175
176+ /**
177+ * PKCS#1 zero pad input buffer to max data length
178+ * @param hashBuf
179+ * @param hashAlgorithm
180+ * @returns {* }
181+ */
182+ Scheme . prototype . pkcs0pad = function ( buffer ) {
183+ var filled = new Buffer ( this . key . maxMessageLength - buffer . length ) ;
184+ filled . fill ( 0 ) ;
185+ return Buffer . concat ( [ filled , buffer ] ) ;
186+
187+ return filled ;
188+ } ;
189+
190+ Scheme . prototype . pkcs0unpad = function ( buffer ) {
191+ var unPad ;
192+ if ( typeof buffer . lastIndexOf == "function" ) { //patch for old node version
193+ unPad = buffer . slice ( buffer . lastIndexOf ( '\0' ) + 1 , buffer . length ) ;
194+ } else {
195+ unPad = buffer . slice ( String . prototype . lastIndexOf . call ( buffer , '\0' ) + 1 , buffer . length ) ;
196+ }
197+
198+ return unPad ;
199+ } ;
200+
184201 /**
185202 * PKCS#1 pad input buffer to max data length
186203 * @param hashBuf
0 commit comments