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 = ;
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;//var networkDataBuffer = baUtil.parseHexStringToByteArr( '5B 31 32 33 34 35 5D ' ); // same with above, space will ignoreif networkDataBuffer // Print the binary data console; console; console;else console;
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 = keyString 'utf-8' beforeEncryptBuffer = '<Here is my scret text, please DO NOT tell anybody about this!!!>' 'utf-8' encryptedBuffer decryptedBuffer; console;console;console;console; encryptedBuffer = baUtil;//encryptedBuffer = baUtil.encryptByAES256( beforeEncryptBuffer, keyString ); if encryptedBuffer console; console;else logger; decryptedBuffer = baUtil;//decryptedBuffer = baUtil.decryptByAES256( encryptedBuffer, keyString ); if decryptedBuffer console; console;else console;