Xojo Developer Conference
25/27th April 2018 in Denver.
MBS Xojo Conference
6/7th September 2018 in Munich, Germany.

Platforms to show: All Mac Windows Linux Cross-Platform

CRC16MBS(data as string) as UInt16
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 12.1 Yes Yes Yes Yes No
Function: Calculates a 16bit Checksum about the provided string.
Example:
dim n1 as Integer = CRC16MBS("Hello World")
dim n2 as Integer = CRC16MBS("Hallo World")

MsgBox str(n1)+" "+str(n2)
Notes: Please note: Different CRC functions give different result. This is one specific 16 bit CRC function which may be or not be the one you need for your application.

Feedback, Comments & Corrections

CRC_32InMemContMBS(address as Ptr, length as Integer, prevCRC as UInt32) as UInt32
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 3.1 Yes Yes Yes Yes No
Function: Calculates a 32bit Checksum about the provided memory address.

Feedback, Comments & Corrections

CRC_32InMemMBS(address as Ptr, length as Integer) as UInt32
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 3.1 Yes Yes Yes Yes No
Function: Calculates a 32bit Checksum about the provided memory address.

Feedback, Comments & Corrections

CRC_32OfStrContMBS(s as String, prevCRC as UInt32) as UInt32
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 3.1 Yes Yes Yes Yes No
Function: Calculates a 32bit Checksum about the provided block of data.

Some examples using this method:

Feedback, Comments & Corrections

CRC_32OfStrMBS(s as String) as UInt32
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 3.1 Yes Yes Yes Yes No
Function: Calculates a 32bit Checksum about the provided string.
Example:
dim n as Integer
n=CRC_32OfStrMBS("Hello World")
' n is now 1243066710
Notes:
See the text "About-CRC" for details about this Checksum things.
This function is also available as part of the memoryblock class.

About Checksums:

These functions calculate CRCs over a range of bytes in a MemoryBlock or in a String.

There are three versions of CRC calculation: One for 16 Bit, one for 32 Bit and one for 16 to 64 Bit wide checksums. If you are free to choose, I suggest that you use the 32 Bit version because of its accuracy and performance.

Some background on using checksums/CRCs:
Checksums, such as CRCs, are used to quickly verify that a chunk of data has not been modified somehow without your control. To use it, you'd calculate the checksum (CRC) of your data, then store that checksum value (which only needs 2 to 8 bytes of storage) somewhere. Later, when retrieving your data, you calculate its checksum again and compare its value with the previously stored value. If it does not match, the data got somehow corrupted. If it matches, that it is quite likely, although not 100% sure, that the data is still in its original state.

There are 3 different CRC algorithms available:

  • CRC_CCITT...() as Integer
  • CRC_32...() as Integer
  • CRC_Dillon...(bitWidth as Integer, ...) as String

The CCITT version calculates a rather classic 16 bit CRC. Unless you need that CRC for legacy data, I recomment not to use it, but rather use the CRC_32 version.

CRC_32 is the most common used algorithm for 32 bit wide CRCs. Be aware there are theoretically many other ways to calculate a CRC 32, however.

The Dillon algorithm is a smart routine to calculate CRCs in any width between 16 and 64 bit. This one is only useful if you find that a 32 bit CRC is not sufficient for your needs. If you can live with a plain 32 bit CRC, you should prefer the CRC_32 routines, because they are faster than the Dillon code.

The result, since it can be up to 64 bit in size, cannot be returned in an Integer (they can only hold 32 bit). Instead, the result is returned as a 8 byte value, stored in a 8 byte long string. To get the value of that string, you can copy the string into a MemoryBlock, and then access the parts opf the 8 byte long value there. The demo project shows how to accomplish this.

The CRC code was developed and published by Matthew Dillon. Here's his web page with more information about it:
http://www.backplane.com/diablo/crc64.html

Some examples using this method:

Feedback, Comments & Corrections

CRC_CCITTInMemContMBS(address as Ptr, length as Integer, prevCRC as UInt32) as UInt32
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 3.1 Yes Yes Yes Yes No
Function: Calculates a 16bit Checksum about the provided memory address.

Feedback, Comments & Corrections

CRC_CCITTInMemMBS(address as Ptr, length as Integer) as UInt32
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 3.1 Yes Yes Yes Yes No
Function: Calculates a 16bit Checksum about the provided memory address.

Feedback, Comments & Corrections

CRC_CCITTOfStrContMBS(s as String, prevCRC as UInt32) as UInt32
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 3.1 Yes Yes Yes Yes No
Function: Calculates a 16bit Checksum about the provided string.

Feedback, Comments & Corrections

CRC_CCITTOfStrMBS(s as String) as UInt32
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 3.1 Yes Yes Yes Yes No
Function: Calculates a 16bit Checksum about the provided string.
Example:
dim n as Integer
n=CRC_CCITTOfStrMBS("Hello World")
' n is now 39210

Feedback, Comments & Corrections

CRC_DillonInMemMBS(bitWidth as Integer, address as Ptr, length as Integer) as String
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 3.1 Yes Yes Yes Yes No
Function: Calculates a 16 to 64bit Checksum about the provided memory address.

Feedback, Comments & Corrections

CRC_DillonOfStrMBS(bitWidth as Integer, s as String) as String
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 3.2 Yes Yes Yes Yes No
Function: Calculates a 16 to 64bit Checksum about the provided string.
Example:
dim s as string
s=CRC_DillonOfStrMBS(64,"Hello World")
' s has now the 64bit checksum inside an 8 byte binary string

Feedback, Comments & Corrections

CRC_DillonUInt64InMemMBS(bitWidth as Integer, address as Ptr, length as Integer) as UInt64
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 6.4 Yes Yes Yes Yes No
Function: Calculates a 16 to 64bit Checksum about the provided memory address.

Feedback, Comments & Corrections

CRC_DillonUInt64OfStrMBS(bitWidth as Integer, s as String) as UInt64
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 6.4 Yes Yes Yes Yes No
Function: Calculates a 16 to 64bit Checksum about the provided string.

Feedback, Comments & Corrections

GetHash32MBS(s as string) as UInt32
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 3.1 Yes Yes Yes Yes No
Function: Calculates some special Hash value.
Example:
MsgBox hex(GetHash32MBS("xskin-b013fafit_01-PELVIS-BODY.skn")) // should show 67A53A4D
Notes:
The 255 char limit was in the original C function, but should be resolved for this plugin, so string longer than 255 chars will work.
The original C code looks like this:

static UInt32 GetHash32 (StringPtr inString)
{
intlength = inString[0];
UInt32hash = length + 1;
inti;

for (i = 1; i <= length; i++)
{
hash = __rlwinm(hash, 3, 0, 31) ^ tolower(inString[i]);
}

return hash;
}

Originally this was added for Cherie Benoit, which describes it like this:

"GetHash32MBS" duplicates the hashing function of Westlake
Interactive's "Namer" application which creates LFN-style short
filenames for use with the Macintosh version of "The Sims."

Feedback, Comments & Corrections

ModBusCalculateRTUMessageCRCMBS(data as string) as UInt16
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method Encryption and Hash MBS Util Plugin 16.2 Yes Yes Yes Yes No
Function: Calculates the RTU Message CRC string.
Example:
dim m as new MemoryBlock(9)

m.UInt8Value(0) = 1 // start of package
m.UInt8Value(1) = 6 // command code
m.UInt8Value(2) = 0 // some data...
m.UInt8Value(3) = 0
m.UInt8Value(4) = 0
m.UInt8Value(5) = &h21
m.UInt8Value(6) = 0 // checksum comes here
m.UInt8Value(7) = 0
m.UInt8Value(8) = 4 // end of package

dim Data as string = m.StringValue(0,6)
dim CheckSum as Int16 = ModBusCalculateRTUMessageCRCMBS(data)

m.UInt16Value(6) = checksum

MsgBox EncodeHex(m)

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS Util Plugin.




Links
MBS Xojo PDF Plugins