1.0.1 • Published 9 years ago
byte-array-util v1.0.1
byte-array-util
Encapsulate the handling between Buffer(Binary) and Hex data(String), very useful for situation that processing TCP/UDP data stream, e.g. handling the binary hardware protocol data, support AES 256 algorithm encrypt and decrypt.
Installation
$ npm install byte-array-util
Usage
Usage:
var baUtil = require( 'byte-array-util' );
Data converting
/**
* 'parseFromHexString' can get a Buffer instance from some HEX format string, it
* will ignore all the space inside the HEX string which actually HEX string not
* need that. For example we re-create a binary buffer from log file which has
* the HEX data string below:
*
* 0x5B 0x31 0x32 0x33 0x34 0x35 0x5D <-- Equal to Ascii/UTF-8 String --> [12345]
*
*
* 'toHexString' can convert a Buffer instance( Binary ) into HEX format string,
* if you want to add some separate string between each HEX string( 2 bytes ), you
* can set 'needSeparator' to true and provide the 'separator' param to what you
* want.
*/
var networkDataBuffer = baUtil.parseFromHexString( '5B31323334355D' );
//var networkDataBuffer = baUtil.parseHexStringToByteArr( '5B 31 32 33 34 35 5D ' ); // same with above, space will ignore
if ( networkDataBuffer ){
// Print the binary data
console.log( 'networkDataBuffer( HEX ): ' + baUtil.toHexString( networkDataBuffer ) );
console.log( 'networkDataBuffer( HEX with space separate ): ' + baUtil.toHexString( networkDataBuffer, true, ' ' ) );
console.log( 'networkDataBuffer( UTF-8 String ): ' + networkDataBuffer.toString( 'utf-8' ) );
}
else{
console.log( 'networkDataBuffer: null' );
}
Binary data encrypt and decrypt
/**
* Binary buffer also can be encrypted( before you send over network ) and be
* decrypted( after you got encrypted data from network ):
*
* 'encryptByAES256'
* 'decryptByAES256'
*/
var keyString = '#%@_My Secret Key!()*',
keyStringBuffer = new Buffer( keyString, 'utf-8' ),
beforeEncryptBuffer = new Buffer( '<Here is my scret text, please DO NOT tell anybody about this!!!>', 'utf-8' ),
encryptedBuffer,
decryptedBuffer;
console.log( 'keyStringBuffer(Hex): \t\t' + baUtil.toHexString( keyStringBuffer, false, null ) );
console.log( 'keyStringBuffer(String):\t' + keyStringBuffer.toString( 'utf-8' ) );
console.log( 'beforeEncryptBuffer(Hex):\t' + baUtil.toHexString( beforeEncryptBuffer, false, null ) );
console.log( 'beforeEncryptBuffer(String):\t' + beforeEncryptBuffer.toString( 'utf-8' ) );
encryptedBuffer = baUtil.encryptByAES256( beforeEncryptBuffer, keyStringBuffer );
//encryptedBuffer = baUtil.encryptByAES256( beforeEncryptBuffer, keyString );
if ( encryptedBuffer ) {
console.log( '\nafterEncryptedBuffer(Hex):\t' + baUtil.toHexString( encryptedBuffer, false, null ) );
console.log( 'afterEncryptedBuffer(String):\t' + encryptedBuffer.toString( 'utf-8' ) );
}
else {
logger.logSetting( '\nafterEncryptedBuffer', 'null' );
}
decryptedBuffer = baUtil.decryptByAES256( encryptedBuffer, keyStringBuffer );
//decryptedBuffer = baUtil.decryptByAES256( encryptedBuffer, keyString );
if ( decryptedBuffer ){
console.log( '\nafterDecryptedBuffer(Hex):\t' + baUtil.toHexString( decryptedBuffer, false, null ) );
console.log( 'afterDecryptedBuffer(String):\t' + decryptedBuffer.toString( 'utf-8' ) );
}
else{
console.log( '\nafterDecryptedBuffer', 'null' );
}