1515 * limitations under the License.
1616 */
1717
18- import * as crypto from 'crypto'
18+ import * as Crypto from 'crypto'
1919import * as Async from 'async'
2020import * as Stream from 'stream'
2121import * as Net from 'net'
@@ -36,20 +36,20 @@ export interface pairConnect {
3636export const encrypt = ( text : Buffer , masterkey : string , CallBack ) => {
3737 let salt = null
3838 Async . waterfall ( [
39- next => crypto . randomBytes ( 64 , next ) ,
39+ next => Crypto . randomBytes ( 64 , next ) ,
4040 ( _salt , next ) => {
4141 salt = _salt
42- crypto . pbkdf2 ( masterkey , salt , 2145 , 32 , 'sha512' , next )
42+ Crypto . pbkdf2 ( masterkey , salt , 2145 , 32 , 'sha512' , next )
4343 }
4444 ] , ( err , derivedKey ) => {
4545 if ( err )
4646 return CallBack ( err )
4747
48- crypto . randomBytes ( 12 , ( err1 , iv ) => {
48+ Crypto . randomBytes ( 12 , ( err1 , iv ) => {
4949 if ( err1 )
5050 return CallBack ( err1 )
5151
52- const cipher = crypto . createCipheriv ( 'aes-256-gcm' , derivedKey , iv ) ;
52+ const cipher = Crypto . createCipheriv ( 'aes-256-gcm' , derivedKey , iv ) ;
5353
5454 let _text = Buffer . concat ( [ Buffer . alloc ( 4 , 0 ) , text ] )
5555 _text . writeUInt32BE ( text . length , 0 )
@@ -82,13 +82,13 @@ export const decrypt = ( data: Buffer, masterkey, CallBack ) => {
8282 const tag = data . slice ( 76 , 92 ) ;
8383 const text = data . slice ( 92 ) ;
8484 // derive key using; 32 byte key length
85- crypto . pbkdf2 ( masterkey , salt , 2145 , 32 , 'sha512' , ( err , derivedKey ) => {
85+ Crypto . pbkdf2 ( masterkey , salt , 2145 , 32 , 'sha512' , ( err , derivedKey ) => {
8686
8787 if ( err )
8888 return CallBack ( err )
8989 // AES 256 GCM Mode
9090 try {
91- const decipher = crypto . createDecipheriv ( 'aes-256-gcm' , derivedKey , iv )
91+ const decipher = Crypto . createDecipheriv ( 'aes-256-gcm' , derivedKey , iv )
9292 decipher . setAuthTag ( tag )
9393 const decrypted = Buffer . concat ( [ decipher . update ( text ) , decipher . final ( ) ] )
9494 const leng = decrypted . slice ( 4 , 4 + decrypted . readUInt32BE ( 0 ) )
@@ -145,14 +145,14 @@ export class encryptStream extends Stream.Transform {
145145 }
146146 private init ( callback ) {
147147 return Async . waterfall ( [
148- next => crypto . randomBytes ( 64 , next ) ,
148+ next => Crypto . randomBytes ( 64 , next ) ,
149149 ( _salt , next ) => {
150150 this . salt = _salt
151- crypto . randomBytes ( 12 , next )
151+ Crypto . randomBytes ( 12 , next )
152152 } ,
153153 ( _iv , next ) => {
154154 this . iv = _iv
155- crypto . pbkdf2 ( this . password , this . salt , 2145 , 32 , 'sha512' , next )
155+ Crypto . pbkdf2 ( this . password , this . salt , 2145 , 32 , 'sha512' , next )
156156 }
157157 ] , ( err , derivedKey ) => {
158158
@@ -171,15 +171,16 @@ export class encryptStream extends Stream.Transform {
171171 return this . _transform ( chunk , encode , cb )
172172 } )
173173 }
174- const cipher = crypto . createCipheriv ( 'aes-256-gcm' , this . derivedKey , this . iv )
174+ const cipher = Crypto . createCipheriv ( 'aes-256-gcm' , this . derivedKey , this . iv )
175175
176176 let _text = Buffer . concat ( [ Buffer . alloc ( 4 , 0 ) , chunk ] )
177177
178178 _text . writeUInt32BE ( chunk . length , 0 )
179-
179+
180180 if ( chunk . length < this . random ) {
181- _text = Buffer . concat ( [ _text , Buffer . allocUnsafe ( Math . random ( ) * 1000 ) ] )
181+ _text = Buffer . concat ( [ _text , Crypto . randomBytes ( Math . round ( Math . random ( ) * 5000 ) ) ] )
182182 }
183+
183184 const _buf = Buffer . concat ( [ cipher . update ( _text ) , cipher . final ( ) ] )
184185 const _buf1 = Buffer . concat ( [ cipher . getAuthTag ( ) , _buf ] )
185186
@@ -213,7 +214,7 @@ export class decryptStream extends Stream.Transform {
213214 private derivedKey : Buffer = null
214215 private _decrypt ( _text : Buffer ) {
215216
216- const decipher = crypto . createDecipheriv ( 'aes-256-gcm' , this . derivedKey , this . iv )
217+ const decipher = Crypto . createDecipheriv ( 'aes-256-gcm' , this . derivedKey , this . iv )
217218 decipher . setAuthTag ( _text . slice ( 0 , 16 ) )
218219 try {
219220 const _buf = Buffer . concat ( [ decipher . update ( _text . slice ( 16 ) ) , decipher . final ( ) ] )
@@ -233,7 +234,7 @@ export class decryptStream extends Stream.Transform {
233234 public _First ( chunk : Buffer , CallBack : ( err ?: Error , text ?: Buffer ) => void ) {
234235 this . salt = chunk . slice ( 0 , 64 ) ;
235236 this . iv = chunk . slice ( 64 , 76 ) ;
236- return crypto . pbkdf2 ( this . password , this . salt , 2145 , 32 , 'sha512' , ( err , derivedKey ) => {
237+ return Crypto . pbkdf2 ( this . password , this . salt , 2145 , 32 , 'sha512' , ( err , derivedKey ) => {
237238 if ( err ) {
238239 console . log ( `decryptStream crypto.pbkdf2 ERROR: ${ err . message } ` )
239240 return CallBack ( err )
0 commit comments