lrc

package module
v0.0.0-...-2a956a5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 11, 2021 License: MIT Imports: 3 Imported by: 0

README

This Go package implements two simple hash algorithms, confusingly known under the same name, Longitudinal Redundancy Check (LRC). The hash sum of both algorithms is just a single byte.

The first algorithm, also known as BCC (for block check character) calculates its single sum byte simply as the XOR of all input bytes. This is the algorithm defined in ISO 1155.

The second algorithm calculates its single sum byte as the 2's complement of the arithmetic sum (mod 2⁸) of the input bytes. This is the algorithm used by the popular Modbus standard, as specified in Modbus over Serial Line, section 6.2.1.

Documentation

Overview

Package lrc implements two simple hash algorithms, confusingly known under the same name, Longitudinal Redundancy Check (LRC). The hash sum of both algorithms is just a single byte.

The first algorithm, also known as BCC (for block check character) calculates its single sum byte simply as the XOR of all input bytes. This is the algorithm defined in ISO 1155.

The second algorithm calculates its single sum byte as the 2's complement of the arithmetic sum (mod 2⁸) of the input bytes. This is the algorithm used by the popular Modbus standard, as specified in Modbus over Serial Line, section 6.2.1.

The BCC and LRC types in this package implement the hash.Hash, encoding.BinaryMarshaler and encoding.BinaryUnmarshaler interfaces.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BCC

type BCC byte

BCC implements the block check character hash, which is simply the XOR of all input bytes. The zero value is ready to use.

func (BCC) BlockSize

func (BCC) BlockSize() int

BlockSize implements hash.Hash.BlockSize.

func (BCC) MarshalBinary

func (b BCC) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler.MarshalBinary.

func (*BCC) Reset

func (b *BCC) Reset()

Reset implements hash.Hash.Reset.

func (BCC) Size

func (BCC) Size() int

Size implements hash.Hash.Size.

func (BCC) Sum

func (b BCC) Sum(buf []byte) []byte

Sum implements hash.Hash.Sum.

func (BCC) Sum8

func (b BCC) Sum8() uint8

Sum8 returns the 8-bit BCC hash accumulated so far.

func (*BCC) UnmarshalBinary

func (b *BCC) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler.UnmarshalBinary.

func (*BCC) Write

func (b *BCC) Write(buf []byte) (int, error)

Write implements io.Writer.Write for hash.Hash.

type LRC

type LRC byte

LRC implements the Modbus longitudinal redundancy check hash, which is simply the 2's complements of the sum (mod 2⁸) of all input bytes. The zero value is ready to use.

func (LRC) BlockSize

func (LRC) BlockSize() int

BlockSize implements hash.Hash.BlockSize.

func (LRC) HexSum

func (b LRC) HexSum(buf []byte) []byte

HexSum appends the current hash to buf in hexadecimal format and returns the resulting slice. It does not change the underlying hash state. The hexadecimal representation is high-nibble first, using upper case hexadecimal digits. This format conforms to the specification of the ASCII transmission mode in the Modbus over serial line specification (section 2.5.2).

func (LRC) MarshalBinary

func (b LRC) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler.MarshalBinary.

func (*LRC) Reset

func (b *LRC) Reset()

Reset implements hash.Hash.Reset.

func (LRC) Size

func (LRC) Size() int

Size implements hash.Hash.Size.

func (LRC) Sum

func (b LRC) Sum(buf []byte) []byte

Sum implements hash.Hash.Sum.

func (LRC) Sum8

func (b LRC) Sum8() uint8

Sum8 returns the 8-bit LRC hash accumulated so far.

func (*LRC) UnmarshalBinary

func (b *LRC) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler.UnmarshalBinary.

func (*LRC) Write

func (b *LRC) Write(buf []byte) (int, error)

Write implements io.Writer.Write for hash.Hash.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL