@@ -6,15 +6,15 @@ var BigInteger = require('../libs/jsbn');
6
6
var crypt = require ( 'crypto' ) ;
7
7
var constants = require ( 'constants' ) ;
8
8
var 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' )
18
18
} ;
19
19
20
20
var SIGN_ALG_TO_HASH_ALIASES = {
@@ -42,7 +42,7 @@ module.exports.makeScheme = function (key, options) {
42
42
} ;
43
43
44
44
/**
45
- * Pad input Buffer to encryptedDataLength bytes, and return new Buffer
45
+ * Pad input Buffer to encryptedDataLength bytes, and return Buffer.from
46
46
* alg: PKCS#1
47
47
* @param buffer
48
48
* @returns {Buffer }
@@ -55,20 +55,22 @@ module.exports.makeScheme = function (key, options) {
55
55
}
56
56
if ( this . options . encryptionSchemeOptions && this . options . encryptionSchemeOptions . padding == constants . RSA_NO_PADDING ) {
57
57
//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 ] ) ;
59
61
}
60
62
61
63
/* Type 1: zeros padding for private key encrypt */
62
64
if ( options . type === 1 ) {
63
- filled = new Buffer ( this . key . encryptedDataLength - buffer . length - 1 ) ;
65
+ filled = Buffer . alloc ( this . key . encryptedDataLength - buffer . length - 1 ) ;
64
66
filled . fill ( 0xff , 0 , filled . length - 1 ) ;
65
67
filled [ 0 ] = 1 ;
66
68
filled [ filled . length - 1 ] = 0 ;
67
69
68
70
return Buffer . concat ( [ filled , buffer ] ) ;
69
71
} else {
70
72
/* random padding for public key encrypt */
71
- filled = new Buffer ( this . key . encryptedDataLength - buffer . length ) ;
73
+ filled = Buffer . alloc ( this . key . encryptedDataLength - buffer . length ) ;
72
74
filled [ 0 ] = 0 ;
73
75
filled [ 1 ] = 2 ;
74
76
var rand = crypt . randomBytes ( filled . length - 3 ) ;
@@ -96,7 +98,13 @@ module.exports.makeScheme = function (key, options) {
96
98
97
99
if ( this . options . encryptionSchemeOptions && this . options . encryptionSchemeOptions . padding == constants . RSA_NO_PADDING ) {
98
100
//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 ;
100
108
}
101
109
102
110
if ( buffer . length < 4 ) {
@@ -157,7 +165,7 @@ module.exports.makeScheme = function (key, options) {
157
165
hashAlgorithm = SIGN_ALG_TO_HASH_ALIASES [ hashAlgorithm ] || hashAlgorithm ;
158
166
159
167
if ( signature_encoding ) {
160
- signature = new Buffer ( signature , signature_encoding ) ;
168
+ signature = Buffer . from ( signature , signature_encoding ) ;
161
169
}
162
170
163
171
var hasher = crypt . createHash ( hashAlgorithm ) ;
@@ -180,7 +188,7 @@ module.exports.makeScheme = function (key, options) {
180
188
* @returns {* }
181
189
*/
182
190
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 ) ;
184
192
filled . fill ( 0 ) ;
185
193
return Buffer . concat ( [ filled , buffer ] ) ;
186
194
@@ -216,7 +224,7 @@ module.exports.makeScheme = function (key, options) {
216
224
throw Error ( 'Key is too short for signing algorithm (' + hashAlgorithm + ')' ) ;
217
225
}
218
226
219
- var filled = new Buffer ( this . key . encryptedDataLength - data . length - 1 ) ;
227
+ var filled = Buffer . alloc ( this . key . encryptedDataLength - data . length - 1 ) ;
220
228
filled . fill ( 0xff , 0 , filled . length - 1 ) ;
221
229
filled [ 0 ] = 1 ;
222
230
filled [ filled . length - 1 ] = 0 ;
0 commit comments