1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#include "Crc.h"
8

9
#include <boost/crc.hpp>  // for boost::crc_32_type
10

11
#include <string>
12

13
using namespace TW;
14

15 1
uint16_t Crc::crc16(uint8_t* bytes, uint32_t length) {
16
    // Calculate checksum for existing bytes
17 1
    uint16_t crc = 0x0000;
18 1
    const uint16_t polynomial = 0x1021;
19

20 1
    for (auto i = 0; i < length; i++) {
21 1
        const auto byte = bytes[i];
22 1
        for (auto bitidx = 0; bitidx < 8; bitidx++) {
23 1
            const auto bit = ((byte >> (7 - bitidx) & 1) == 1);
24 1
            const auto c15 = ((crc >> 15 & 1) == 1);
25 1
            crc <<= 1;
26 1
            if (c15 ^ bit) {
27 1
                crc ^= polynomial;
28
            }
29
        }
30
    }
31

32 1
    return crc & 0xffff;
33
}
34

35 1
uint32_t Crc::crc32(const Data& data)
36
{
37 1
    boost::crc_32_type result;
38 1
    result.process_bytes((const void*)data.data(), data.size());
39 1
    return (uint32_t)result.checksum();
40
}
41

42 0
uint32_t Crc::crc32C(const Data& data)
43
{
44
    using crc_32c_type = boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true>;
45 0
    crc_32c_type result;
46 0
    result.process_bytes((const void*)data.data(), data.size());
47 0
    return (uint32_t)result.checksum();
48
}

Read our documentation on viewing source code .

Loading