forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCrc.cpp
More file actions
48 lines (39 loc) · 1.35 KB
/
Crc.cpp
File metadata and controls
48 lines (39 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#include "Crc.h"
#include <boost/crc.hpp> // for boost::crc_32_type
#include <string>
using namespace TW;
uint16_t Crc::crc16(uint8_t* bytes, uint32_t length) {
// Calculate checksum for existing bytes
uint16_t crc = 0x0000;
const uint16_t polynomial = 0x1021;
for (auto i = 0; i < length; i++) {
const auto byte = bytes[i];
for (auto bitidx = 0; bitidx < 8; bitidx++) {
const auto bit = ((byte >> (7 - bitidx) & 1) == 1);
const auto c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit) {
crc ^= polynomial;
}
}
}
return crc & 0xffff;
}
uint32_t Crc::crc32(const Data& data)
{
boost::crc_32_type result;
result.process_bytes((const void*)data.data(), data.size());
return (uint32_t)result.checksum();
}
uint32_t Crc::crc32C(const Data& data)
{
using crc_32c_type = boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true>;
crc_32c_type result;
result.process_bytes((const void*)data.data(), data.size());
return (uint32_t)result.checksum();
}