Promotic

CalcCheckSum - method of the PmBuffer object

Description:
Returns the data block checksum (hash).
Syntax:
Variant CalcCheckSum(Long nOffset, Long nSize, String sType)
Parameters:
nOffset(Long) Specifies the position of read value in the data block.
The read value must be inside the data block. The item is read either whole or not read at all.
>= 0 - Index (in bytes, zero-based index) in the data block.
-4 - The internal automatic position is used (see the AutoOffset property). It points behind the last read or written value.
nSize(Long) The size of read data block.
The -1 value (default) means that the data block is read from the defined starting position to the end.
sType(String) Check sum type.
"crc1" - CRC-16-CCITT, 2 bytes Cyclical Redundancy Check with CCITT polynomial [0x1021 = x16+x12+x5+1].
Cyclical Redundancy Check of the array value bytes.
The method returns a value of type Integer (i.e. 2 bytes value).
This checksum is used for example in communication by the protocol PmSBUS.
"crc2" - CRC-16-IBM, 2 bytes Cyclical Redundancy Check with CCITT polynomial [0xA001 = x16+x15+x2+1].
Cyclical Redundancy Check of the array value bytes.
The method returns a value of type Integer (i.e. 2 bytes value).
This checksum is used for example in communication by the protocol Modbus.
"crc3" - CRC-16-IBM, 2 bytes Cyclical Redundancy Check with CCITT polynomial [0x8005 = x16+x15+x2+1].
Cyclical Redundancy Check of the array value bytes.
The method returns a value of type Integer (i.e. 2 bytes value).
This checksum is used for example in communication by the protocol PmABradleyDF1.
"crc4" - CRC-16, 2 bytes Cyclical Redundancy Check with CCITT polynomial [x16+x13+x12+x10+x8+x6+x5+x2+1].
Cyclical Redundancy Check of the array value bytes.
The method returns a value of type Integer (i.e. 2 bytes value).
This checksum is used for example in communication by the protocol DNP3.
"sum1" - Byte values sum.
The method returns a value of type Byte (sum modulo 256).
This checksum is used for example in communication by the protocol PmIEC8705, PmMBus, PmAdam, PmTeco, PmInmat66.
"sum2" - Byte values sum.
The method returns a value of type Integer (i.e. 2 bytes value) (sum modulo 65536).
"xor1" - Bit XOR of all byte values.
The method returns a value of type Byte.
This checksum is used for example in communication by the protocol Pm3964.
"md5" - MD5 hash of all byte values.
The MD5 hash is 16 bytes, making the resulting HexaString 32 characters long.
The method returns a value of type String.
"sha256" - SHA-256 hash of all byte values.
The SHA-256 hash is 32 bytes, making the resulting HexaString 64 characters long.
The method returns a value of type String.
"sha512" - SHA-512 hash of all byte values.
The SHA-512 hash is 64 bytes, making the resulting HexaString 128 characters long.
The method returns a value of type String.
Note:
This method is not functional in Web panels.

Caution: The internal automatic position (see the AutoOffset property) IS NOT CHANGED by this operation.
Example1:
Creating a data block by continuous writing of 4, 2 and 1-byte integer value (7 bytes in total) and the detecting the check sum "crc1" of the whole data block.
JavaScriptSelect and copy to clipboard

var oBuf = Pm.CreatePmBuffer();
oBuf.SetInt32(-2, 0x10203040);
oBuf.SetInt16(-2, 0x5060);
oBuf.SetUint8(-2, 0x70);
var n = oBuf.CalcCheckSum(0, -1, "crc1");
Example2:
Creating a data block with a string coded in utf-8 and then detecting the MD5 or SHA-256 hash in the form of HexaString of the whole data block.
JavaScriptSelect and copy to clipboard

var sMd5, sSha256;
var oBuf = Pm.CreatePmBuffer();
oBuf.SetStringVar(-2, "Test string", 1, 1);
sMd5 = oBuf.CalcCheckSum(0, -1, "md5");
sSha256 = oBuf.CalcCheckSum(0, -1, "sha256");

History:
Pm9.00.23: New type of checksum crc4.
Pm9.00.14: New types of checksums (hash) md5, sha256 and sha512.
Pm9.00.10: Created
PROMOTIC 9.0.27 SCADA system documentation MICROSYS, spol. s r.o.

Send page remarkContact responsible person
© MICROSYS, spol. s r.o.