Chksum : Checksum calculation tools

更新时间:
2024-05-13
下载文档

Chksum : Checksum calculation tools

This module provides common checksum calculation functions, including internet chksum, various CRC algorithms, etc.

User can use the following code to import the Chksum module.

var Chksum = require('chksum');

Support

The following shows Chksum module APIs available for each permissions.

 User ModePrivilege Mode
Chksum
chksum.crc
chksum.nmea
chksum.inet

Chksum Class

new Chksum()

  • Returns: {Object} Chksum object.

Create a chksum object for computing internet chksum and nmea chksum.

Example

var chksum = new Chksum();

new Chksum(crcType[, lsb])

  • crcType {Integer} CRC algorithm type.
  • lsb {Boolean} Whether store the result as LSB. default: MSB.
  • Returns: {Object} Chksum object.

crcType can be the following value:

  • Chksum.CRC8
  • Chksum.CRC16
  • Chksum.CRC16_CCITT_1DOF
  • Chksum.CRC16_CCITT_FFFF
  • Chksum.CRC16_DNP
  • Chksum.CRC16_KERMIT
  • Chksum.CRC16_MODBUS
  • Chksum.CRC16_SICK
  • Chksum.CRC16_XMODEM
  • Chksum.CRC32
  • Chksum.CRC64_ECMA
  • Chksum.CRC64_WE

Create a chksum object for calculating CRC.

Example

var chksum = new Chksum(Chksum.CRC16);

Chksum Object

chksum.crc(outBuf, outOffset, string)

  • outBuf {Buffer} Output buffer.
  • outOffset {Integer} Output buffer position.
  • string {String} Input string.
  • Returns: {Integer} Chksum bytes length.

Calculate the CRC chksum of a string and store the result in the specified location.

Example

var chksum = new Chksum(Chksum.CRC16);
var buffer = new Buffer(2);

chksum.crc(buffer, 0, 'Test CRC');

chksum.crc(outBuf, outOffset, buffer[, offset[, length]])

  • outBuf {Buffer} Output buffer.
  • outOffset {Integer} Output buffer position.
  • buffer {Buffer} Input buffer.
  • offset {Integer} Input buffer offset. default: 0.
  • length {Integer} Input data length. default:buffer.length.
  • Returns: {Integer} Chksum bytes length.

Calculate the CRC chksum of a buffer and store the result in the specified location.

Example

var chksum = new Chksum(Chksum.CRC16);
var buffer = new Buffer(10);

buffer.fromString('abcdefgh');
chksum.crc(buffer, 8, buffer, 0, 8);

chksum.nmea(outBuf, outOffset, string)

  • outBuf {Buffer} Output buffer.
  • outOffset {Integer} Output buffer position.
  • string {String} Input string.
  • Returns: {Integer} Chksum bytes length. If success returns 3.

Calculate the NMEA chksum of a string and store the result in the specified location.

var chksum = new Chksum();
var buffer = new Buffer(3);

chksum.nmea(buffer, 0, 'Test NMEA');

chksum.inet(outBuf, outOffset, string)

  • outBuf {Buffer} Output buffer.
  • outOffset {Integer} Output buffer position.
  • string {String} Input string.
  • Returns: {Integer} Chksum bytes length. If success returns 2.

Calculate the internet chksum of a string and store the result in the specified location.

var chksum = new Chksum();
var buffer = new Buffer(2);

chksum.inet(buffer, 0, 'Test INET');

chksum.inet(outBuf, outOffset, buffer[, offset[, length]])

  • outBuf {Buffer} Output buffer.
  • outOffset {Integer} Output buffer position.
  • buffer {Buffer} Input buffer.
  • offset {Integer} Input buffer offset. default: 0.
  • length {Integer} Input data length. default:buffer.length.
  • Returns: {Integer} Chksum bytes length. If success returns 2.

Calculate the internet chksum of a buffer and store the result in the specified location.

Example

var chksum = new Chksum();
var buffer = new Buffer(10);

buffer.fromString('abcdefgh');
chksum.inet(buffer, 8, buffer, 0, 8);
文档内容是否对您有所帮助?
有帮助
没帮助