@@ -27,7 +27,7 @@ var ACCOUNT_PUBLIC = 35;
2727var PUBLICKEY_LENGTH = 33 ;
2828
2929function eciesEncrypt ( message , publicKey ) {
30- var plainMsgBuf = new Buffer ( message , 'utf8' ) ;
30+ var plainMsgBuf = Buffer . from ( message , 'utf8' ) ;
3131
3232 if ( ! publicKey )
3333 throw new Error ( 'eciesEncrypt publicKey is undefined' ) ;
@@ -63,7 +63,7 @@ function eciesEncrypt(message, publicKey) {
6363 //console.log('[hmacKey] ', new Buffer(hmacKey, 'hex'));
6464 var iv = crypto . randomBytes ( IVLength ) ;
6565 var D = hmac ( hmacKey , plainMsgBuf ) ;
66- var cipher = crypto . createCipheriv ( 'aes-256-cbc' , new Buffer ( aesKey ) , iv ) ;
66+ var cipher = crypto . createCipheriv ( 'aes-256-cbc' , Buffer . from ( aesKey ) , iv ) ;
6767 var plainBuf = Buffer . concat ( [ new Buffer ( D ) , plainMsgBuf ] ) ;
6868 //console.log('[decryptedBytes] ', plainBuf.toString('hex')+" len="+plainBuf.length);
6969 var encryptedBytes = cipher . update ( plainBuf ) ;
@@ -78,7 +78,7 @@ function eciesDecrypt(messageHex, privateKey) {
7878 var level = 256 ;
7979 var Rb_len = 33 ;
8080 var D_len = level >> 3 ;
81- var cipherText = new Buffer ( messageHex , 'hex' ) ;
81+ var cipherText = Buffer . from ( messageHex , 'hex' ) ;
8282 var ct_len = cipherText . length ;
8383
8484 if ( ct_len < Rb_len + IVLength + D_len + AESBlockLength )
@@ -106,12 +106,12 @@ function eciesDecrypt(messageHex, privateKey) {
106106 //console.log('[aesKey] ', new Buffer(aesKey, 'hex'));
107107 //console.log('[hmacKey] ', new Buffer(hmacKey, 'hex'));
108108
109- var cipher = crypto . createDecipheriv ( 'aes-256-cbc' , new Buffer ( aesKey ) , iv ) ;
109+ var cipher = crypto . createDecipheriv ( 'aes-256-cbc' , Buffer . from ( aesKey ) , iv ) ;
110110 var decryptedBytes = cipher . update ( EM ) ;
111111 decryptedBytes = Buffer . concat ( [ decryptedBytes , cipher . final ( ) ] ) ;
112112 //console.log('[decryptedBytes] ', decryptedBytes.toString('hex')+" len="+decryptedBytes.length);
113113 var D = decryptedBytes . slice ( 0 , D_len ) ;
114- var plainMsgBuf = new Buffer ( decryptedBytes . slice ( D_len ) ) ;
114+ var plainMsgBuf = Buffer . from ( decryptedBytes . slice ( D_len ) ) ;
115115
116116 var recoveredD = hmac ( hmacKey , plainMsgBuf ) ;
117117 //debug('recoveredD: ', new Buffer(recoveredD).toString('hex'));
@@ -188,8 +188,8 @@ function bytesToBits(bytes) {
188188
189189function paddingPass ( password , keyLen ) {
190190 if ( password . length < keyLen ) {
191- var pass = new Buffer ( password ) ;
192- var retByte = new Buffer ( keyLen ) ;
191+ var pass = Buffer . from ( password ) ;
192+ var retByte = Buffer . alloc ( keyLen ) ;
193193 var byteToPad = keyLen - password . length ;
194194 for ( var i = 0 ; i < keyLen ; i ++ ) {
195195 if ( i < pass . length )
@@ -210,10 +210,10 @@ function paddingPass(password,keyLen){
210210 */
211211var aesEncrypt = function ( secret , plaintext ) {
212212 var secretPadded = paddingPass ( secret , AESKeyLength ) ;
213- var aesKey = new Buffer ( secretPadded , 'utf8' ) ;
213+ var aesKey = Buffer . from ( secretPadded , 'utf8' ) ;
214214 var iv = aesKey . slice ( 0 , IVLength ) ;
215215 var cipher = crypto . createCipheriv ( 'aes-256-cbc' , aesKey , iv ) ;
216- var plainBuf = new Buffer ( plaintext , 'utf8' ) ;
216+ var plainBuf = Buffer . from ( plaintext , 'utf8' ) ;
217217 var encryptedBytes = cipher . update ( plainBuf ) ;
218218 encryptedBytes = Buffer . concat ( [ encryptedBytes , cipher . final ( ) ] ) ;
219219 return encryptedBytes . toString ( 'hex' ) ; ;
@@ -227,9 +227,9 @@ var aesEncrypt = function(secret, plaintext) {
227227 */
228228var aesDecrypt = function ( secret , encryptedHex ) {
229229 var secretPadded = paddingPass ( secret , AESKeyLength ) ;
230- var aesKey = new Buffer ( secretPadded , 'utf8' ) ;
230+ var aesKey = Buffer . from ( secretPadded , 'utf8' ) ;
231231 var iv = aesKey . slice ( 0 , IVLength ) ;
232- var encryptedBuf = new Buffer ( encryptedHex , 'hex' ) ;
232+ var encryptedBuf = Buffer . from ( encryptedHex , 'hex' ) ;
233233 var cipher = crypto . createDecipheriv ( 'aes-256-cbc' , aesKey , iv ) ;
234234 var decryptedBytes = cipher . update ( encryptedBuf ) ;
235235 decryptedBytes = Buffer . concat ( [ decryptedBytes , cipher . final ( ) ] ) ;
@@ -243,7 +243,7 @@ function simpleEncrypt(plainBytes,publicKey,ephPrivKey){
243243 var aesKey = kdfOutput . slice ( 0 , AESKeyLength ) ;
244244
245245 var iv = crypto . randomBytes ( IVLength ) ;
246- var cipher = crypto . createCipheriv ( 'aes-256-cbc' , new Buffer ( aesKey ) , iv ) ;
246+ var cipher = crypto . createCipheriv ( 'aes-256-cbc' , Buffer . from ( aesKey ) , iv ) ;
247247 //console.log('[decryptedBytes] ', plainBuf.toString('hex')+" len="+plainBuf.length);
248248 var encryptedBytes = cipher . update ( plainBytes ) ;
249249 encryptedBytes = Buffer . concat ( [ encryptedBytes , cipher . final ( ) ] ) ;
@@ -259,7 +259,7 @@ function simpleDecrypt(cipherBytes,privateKey,ephPubKey){
259259 var kdfOutput = hashjs . sha512 ( ) . update ( Z . toArray ( ) ) . digest ( ) ;
260260 //var kdfOutput = hkdf(Z.toArray(), ECIESKDFOutput, null, null);
261261 var aesKey = kdfOutput . slice ( 0 , AESKeyLength ) ;
262- var cipher = crypto . createDecipheriv ( 'aes-256-cbc' , new Buffer ( aesKey ) , iv ) ;
262+ var cipher = crypto . createDecipheriv ( 'aes-256-cbc' , Buffer . from ( aesKey ) , iv ) ;
263263 var decryptedBytes = cipher . update ( EM ) ;
264264 decryptedBytes = Buffer . concat ( [ decryptedBytes , cipher . final ( ) ] ) ;
265265
@@ -332,8 +332,8 @@ var encryptText = function(plainText,listPublic){
332332 var MultiEncrypt = root . lookup ( "MultiEncrypt" ) ;
333333 //create a new message
334334 var data = { } ;
335- data . publicOther = new Buffer ( ephPublicKey , 'hex' ) ;
336- data . cipher = new Buffer ( aesCipher , 'hex' ) ;
335+ data . publicOther = Buffer . from ( ephPublicKey , 'hex' ) ;
336+ data . cipher = Buffer . from ( aesCipher , 'hex' ) ;
337337 data . hashTokenPair = [ ] ;
338338 for ( var i = 0 ; i < listPubHash . length ; i ++ ) {
339339 var tokenPair = {
@@ -358,7 +358,7 @@ var encryptText = function(plainText,listPublic){
358358var decryptText = function ( cipherText , secret ) {
359359 return new Promise ( function ( resolve , reject ) {
360360 //Zlib-decompress
361- var cipherBytes = new Buffer ( cipherText , 'hex' ) ;
361+ var cipherBytes = Buffer . from ( cipherText , 'hex' ) ;
362362 zlibDeCompressText ( cipherBytes ) . then ( function ( res ) {
363363 ProtoBuf . load ( "proto/MultiEncrypt.proto" , function ( err , root ) {
364364 if ( err ) throw err ;
@@ -373,7 +373,7 @@ var decryptText = function(cipherText,secret){
373373 var listPubHashToken = message . hashTokenPair ;
374374 for ( var i = 0 ; i < listPubHashToken . length ; i ++ ) {
375375 var hashTokenPair = listPubHashToken [ i ] ;
376- if ( _ . isEqual ( hashTokenPair . publicHash , new Buffer ( sPubHashSelf ) ) ) {
376+ if ( _ . isEqual ( hashTokenPair . publicHash , Buffer . from ( sPubHashSelf ) ) ) {
377377 var ephPrivKey = Secp256k1 . keyFromPrivate ( privateKey , 'hex' ) ;
378378 var pubKey = Secp256k1 . keyFromPublic ( pubOther ) ;
379379 password = simpleDecrypt ( hashTokenPair . token , ephPrivKey , pubKey ) ;
0 commit comments