sftwnd / crayfish-common-crc

Generic Java language CRC implementation (includes CRC16, CRC32, CRC64, etc). With more than 170 predefined CRC like: CRC-32/ISO-HDLC, CRC-64/ECMA-182 & CRC-8/SMBUS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Quality Gate Status Coverage TravisCI-build License

Crayfish Common CRC

Generic Java language CRC implementation (includes CRC16, CRC32, CRC64, etc). With more than 170 predefined CRC like: CRC-32/ISO-HDLC, CRC-64/ECMA-182 & CRC-8/SMBUS

Create CRC

Full case with description

    CrcDescription crcDescription = new CrcDescription(3, 0x3, 0x0, false, false, 0x7);
    CRC crc = crcDescription.getCRC();
    crc.update("StringA".getBytes());
    long crcValue = crc.getCrc();

Simple case

    CrcDescription crcDescription = new CrcDescription(3, 0x3, 0x0, false, false, 0x7);
    long crc = crcDescription.getCRC().update("StringA".getBytes()).getCrc();

Combine crc

    CrcModel crcModel = CrcModel.lookUp("CRC-16/ARC");
    String str1 = "StringABC";
    String str2 = "StringBCD";
    CRC crc = crcModel.getCRC(str1.getBytes());
    crc.update(str2.getBytes());
    long crcValue = crc.getCrc();

Construct & combine value check examples

    CrcModel crcModel = CrcModel.lookUp("CRC-16/ARC");
    String str1 = "StringABC";
    String str2 = "StringBCD";

    long crc1a = crcModel.getCRC().update(str1.getBytes()).update(str2.getBytes()).getCrc();
    long crc1b = crcModel.getCRC(str1.getBytes()).update(str2.getBytes()).getCrc();
    long crc2 = new CRC(crcModel, crcModel.getCRC(str1.getBytes()).getCrc(), str1.length()).update(str2.getBytes()).getCrc();
    long crc3 = crcModel.getCRC((str1+str2).getBytes()).getCrc();

In the all cases values has to be the same

CrcModel

Crc Model is an object for usage of CrcDescription with name and check code.

Register CrcModel

    CrcDescription crcDescription = new CrcDescription(3, 0x3, 0x0, false, false, 0x7);
    CrcModel crcModel = CrcModel.construct(crcDescription);
    # >>> or:
    CrcModel crcModel = CrcModel.construct("MyModel", crcDescription);
    # >>> or:
    long checkCode = 0x4L;
    CrcModel crcModel = CrcModel.construct("MyModel", crcDescription, checkCode);

P.S.> The checkValue will be calculated if it is not defined.

Use predefined CrcModel

    CrcModel crcModel = CrcModel.CRC64_GO_ISO;
    long crc = crcModel.getCRC().update("String".getBytes()).getCrc();

Use well-known CrcModel by the name

    CrcModel crcModel = CrcModel.lookUp("CRC-16/ARC");
    if (crcModel != null) {
        long crc=crcModel.getCRC().update("String".getBytes()).getCrc();
    }

About

Generic Java language CRC implementation (includes CRC16, CRC32, CRC64, etc). With more than 170 predefined CRC like: CRC-32/ISO-HDLC, CRC-64/ECMA-182 & CRC-8/SMBUS

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Java 100.0%