@@ -6,15 +6,15 @@ var BigInteger = require('../libs/jsbn');
66var crypt = require ( 'crypto' ) ;
77var constants = require ( 'constants' ) ;
88var SIGN_INFO_HEAD = {
9- md2 : new Buffer ( '3020300c06082a864886f70d020205000410' , 'hex' ) ,
10- md5 : new Buffer ( '3020300c06082a864886f70d020505000410' , 'hex' ) ,
11- sha1 : new Buffer ( '3021300906052b0e03021a05000414' , 'hex' ) ,
12- sha224 : new Buffer ( '302d300d06096086480165030402040500041c' , 'hex' ) ,
13- sha256 : new Buffer ( '3031300d060960864801650304020105000420' , 'hex' ) ,
14- sha384 : new Buffer ( '3041300d060960864801650304020205000430' , 'hex' ) ,
15- sha512 : new Buffer ( '3051300d060960864801650304020305000440' , 'hex' ) ,
16- ripemd160 : new Buffer ( '3021300906052b2403020105000414' , 'hex' ) ,
17- rmd160 : new Buffer ( '3021300906052b2403020105000414' , 'hex' )
9+ md2 : Buffer . from ( '3020300c06082a864886f70d020205000410' , 'hex' ) ,
10+ md5 : Buffer . from ( '3020300c06082a864886f70d020505000410' , 'hex' ) ,
11+ sha1 : Buffer . from ( '3021300906052b0e03021a05000414' , 'hex' ) ,
12+ sha224 : Buffer . from ( '302d300d06096086480165030402040500041c' , 'hex' ) ,
13+ sha256 : Buffer . from ( '3031300d060960864801650304020105000420' , 'hex' ) ,
14+ sha384 : Buffer . from ( '3041300d060960864801650304020205000430' , 'hex' ) ,
15+ sha512 : Buffer . from ( '3051300d060960864801650304020305000440' , 'hex' ) ,
16+ ripemd160 : Buffer . from ( '3021300906052b2403020105000414' , 'hex' ) ,
17+ rmd160 : Buffer . from ( '3021300906052b2403020105000414' , 'hex' )
1818} ;
1919
2020var SIGN_ALG_TO_HASH_ALIASES = {
@@ -42,7 +42,7 @@ module.exports.makeScheme = function (key, options) {
4242 } ;
4343
4444 /**
45- * Pad input Buffer to encryptedDataLength bytes, and return new Buffer
45+ * Pad input Buffer to encryptedDataLength bytes, and return Buffer.from
4646 * alg: PKCS#1
4747 * @param buffer
4848 * @returns {Buffer }
@@ -55,20 +55,22 @@ 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- return this . pkcs0pad ( buffer ) ;
58+ filled = Buffer . alloc ( this . key . maxMessageLength - buffer . length ) ;
59+ filled . fill ( 0 ) ;
60+ return Buffer . concat ( [ filled , buffer ] ) ;
5961 }
6062
6163 /* Type 1: zeros padding for private key encrypt */
6264 if ( options . type === 1 ) {
63- filled = new Buffer ( this . key . encryptedDataLength - buffer . length - 1 ) ;
65+ filled = Buffer . alloc ( this . key . encryptedDataLength - buffer . length - 1 ) ;
6466 filled . fill ( 0xff , 0 , filled . length - 1 ) ;
6567 filled [ 0 ] = 1 ;
6668 filled [ filled . length - 1 ] = 0 ;
6769
6870 return Buffer . concat ( [ filled , buffer ] ) ;
6971 } else {
7072 /* random padding for public key encrypt */
71- filled = new Buffer ( this . key . encryptedDataLength - buffer . length ) ;
73+ filled = Buffer . alloc ( this . key . encryptedDataLength - buffer . length ) ;
7274 filled [ 0 ] = 0 ;
7375 filled [ 1 ] = 2 ;
7476 var rand = crypt . randomBytes ( filled . length - 3 ) ;
@@ -96,7 +98,13 @@ module.exports.makeScheme = function (key, options) {
9698
9799 if ( this . options . encryptionSchemeOptions && this . options . encryptionSchemeOptions . padding == constants . RSA_NO_PADDING ) {
98100 //RSA_NO_PADDING treated like JAVA left pad with zero character
99- return this . pkcs0unpad ( buffer ) ;
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 ;
100108 }
101109
102110 if ( buffer . length < 4 ) {
@@ -157,7 +165,7 @@ module.exports.makeScheme = function (key, options) {
157165 hashAlgorithm = SIGN_ALG_TO_HASH_ALIASES [ hashAlgorithm ] || hashAlgorithm ;
158166
159167 if ( signature_encoding ) {
160- signature = new Buffer ( signature , signature_encoding ) ;
168+ signature = Buffer . from ( signature , signature_encoding ) ;
161169 }
162170
163171 var hasher = crypt . createHash ( hashAlgorithm ) ;
@@ -180,7 +188,7 @@ module.exports.makeScheme = function (key, options) {
180188 * @returns {* }
181189 */
182190 Scheme . prototype . pkcs0pad = function ( buffer ) {
183- var filled = new Buffer ( this . key . maxMessageLength - buffer . length ) ;
191+ var filled = Buffer . alloc ( this . key . maxMessageLength - buffer . length ) ;
184192 filled . fill ( 0 ) ;
185193 return Buffer . concat ( [ filled , buffer ] ) ;
186194 } ;
@@ -214,7 +222,7 @@ module.exports.makeScheme = function (key, options) {
214222 throw Error ( 'Key is too short for signing algorithm (' + hashAlgorithm + ')' ) ;
215223 }
216224
217- var filled = new Buffer ( this . key . encryptedDataLength - data . length - 1 ) ;
225+ var filled = Buffer . alloc ( this . key . encryptedDataLength - data . length - 1 ) ;
218226 filled . fill ( 0xff , 0 , filled . length - 1 ) ;
219227 filled [ 0 ] = 1 ;
220228 filled [ filled . length - 1 ] = 0 ;
0 commit comments