Contents

edgeengineer/cyclic-redundancy-check

- Support for iOS, tvOS, watchOS, macOS, Linux, Windows and Android.

Supported CRC Algorithms

The library includes pre-configured implementations of common CRC algorithms:

  • CRC-8: Standard CRC-8, CRC-8-CDMA2000, CRC-8-WCDMA
  • CRC-16: CRC-16-IBM, CRC-16-CCITT, CRC-16-XMODEM, CRC-16-MODBUS
  • CRC-32: CRC-32 (Ethernet), CRC-32-BZIP2, CRC-32-MPEG2, CRC-32-POSIX, CRC-32C (Castagnoli)
  • CRC-64: CRC-64-ISO, CRC-64-ECMA

Basic Usage

import CyclicRedundancyCheck

// One-shot CRC-32 calculation
let crc32 = CyclicRedundancyCheck.crc32(string: "123456789")
print("CRC-32: \(String(format: "0x%08X", crc32))") // Should output: 0xCBF43926

// Using a predefined standard
let crc16 = CyclicRedundancyCheck.crc16(string: "Hello, world!")
print("CRC-16: \(String(format: "0x%04X", crc16))")

// CRC-32C (Castagnoli) - commonly used for data integrity
let crc32c = CyclicRedundancyCheck.crc32c(string: "123456789")
print("CRC-32C: \(String(format: "0x%08X", crc32c))") // Should output: 0xE3069283

// Incremental calculation
var calculator = CyclicRedundancyCheck(algorithm: .crc32)
calculator.update(with: "Hello, ")
calculator.update(with: "world!")
let result = calculator.checksum

Custom CRC Configuration

// Creating a custom CRC algorithm
let customConfig = CyclicRedundancyCheckConfiguration<UInt16>(
    polynomial: 0x8005,
    initialValue: 0xFFFF,
    finalXORValue: 0x0000,
    reflectInput: true,
    reflectOutput: true
)

var customCRC = CyclicRedundancyCheck(configuration: customConfig)
let result = customCRC.compute(string: "Test data")

Verification

let expectedCRC: UInt32 = 0xCBF43926
var calculator = CyclicRedundancyCheck(algorithm: .crc32)
let isValid = calculator.verify(string: "123456789", against: expectedCRC)
print("CRC verification: \(isValid ? "Valid" : "Invalid")")

For more details on how to use the library, see the tests in the Tests directory.

License

Apache License 2.0

Package Metadata

Repository: edgeengineer/cyclic-redundancy-check

Default branch: main

README: README.md