CRC 8 and CRC 16 - crc

CRC 16
http://www.lammertbies.nl/comm/info/crc-calculation.html
For input of hex 0x43 the CRC-16 (poly 0x8005) is 0xF141 as per above website...
How do I input this in below calculator to get same result?
https://ghsi.de/CRC/index.php?Polynom=1000000000000101&Message=43
CRC 8
http://www.codeproject.com/Articles/19059/C-CCITT-CRC-Algorithm
In above link with byte array {0xee, 0x01, 0x13, 0x00, 0x06, 0x1c, 0x00, 0x20, 0x1d, 0x00, 0x00} using CRC8 (poly 0xd5) it return 226 as a result.
How do I go about and insert the value into below website?
How about input 0x43? How do I input this using CRC8 (poly 0xd5) into the same website?
https://ghsi.de/CRC/
Sorry but I've been stucked on figuring out this... been studying CRC using below link until pg 11, and I have a rough idea on CRC calculation. But I have found difficulties in figuring how to get same result from the sources I mentioned above...
http://www.ross.net/crc/download/crc_v3.txt

For the CRC-16, use this link with the polynomial corrected to have the x16 term and the input byte reversed, and read the CRC result (828f) reversed (f141). The input and output need to be reversed since that is a reflected CRC. See the definition of that CRC here.
For the CRC-8, defining just the polynomial is not sufficient. You also need to define whether the CRC is reflected or not, what the initial value of the CRC register is, and what to exclusive-or the result with. There is one in the catalogue with that polynomial, which is not reflected and has zero for the initial value and final xor. You can use this online version for that specific CRC to get 0x37 as the CRC of 0x43. That version gives 226 decimal for your example sequence.

Related

What flavor of CRC-CCITT?

Can anyone tell what flavor CRC-CCITT is implemented in this code?
When I use it to compute a CRC over "123456789", I get 0x6E90. But it doesn't match any of the CRCs here: https://www.lammertbies.nl/comm/info/crc-calculation.html
But it does exhibit the property of a CRC where including the CRC in the data that is CRC'd results in a known constant value.
The code shows a right shifting CRC16 based on 0x11021, which when bit reversed and shifted right 1 bit becomes 0x8408. This is a variation of CRC-CCITT (Kermit), but unlike Kermit, it starts with CRC = 0xFFFF (Kermit starts with CRC = 0x0000), and ones complement the CRC at the end, so it won't match any of the CRCs shown at the lammertbies web site. You can verify that it's similar to Kermit by going to the lammertbies web site, switching to hex mode and entering a single byte 80, which will result in 0x0884, which is a byte swapped 0x8408 due to little endian.
If it wasn't for the ones complement at the end, then appending a CRC to data and recalculating a CRC for the data + CRC would return a CRC of 0x0000, for any of the normal CRC methods. With the post ones complement, you get a constant depending on which CRC implementation is used, in this case 0x470F (byte swapped again due to little endian).
To test this, go to the lammertbies web site, prefix the string with hex 21 F3, which will cause the Kermit CRC to be 0xFFFF after processing the 21 F3. Switch to hex input and enter the string:
21F3313233343536373839
this will result in the Kermit CRC returning
0x916F
which when one's complemented is
0x6e90
You can change the Dr Dobb's example code to match Kermit with these changes:
crc = 0; /* instead of crc = 0xffff; */
/* ... */
/* crc = ~crc; comment out this line */

Default CRC input to reproduce check value

I found a site where a lot of CRC values are listed, with configurations/setup. There is also a "check" value, which I believe is generated with some kind of default input (some dummy string or such).
Does anybody know what this is and/or how I can reproduce it?
Sample config: width=3 poly=0x3 init=0x7 refin=true refout=true xorout=0x0 check=0x6 name="CRC-3/ROHC"
Site: http://reveng.sourceforge.net/crc-catalogue/all.htm#crc.cat.arc
Thanks a lot.
sorry, found the solution.
HEX: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39
String: 123456789

How to send messages as websocket client in c++ to a server?

I've a little problem in sendine my messages to the server.
It probably send them and the server recieve them, but i have no real idea how to mask them correctly.
I know this is actually the way:
https://www.rfc-editor.org/rfc/rfc6455#section-5.2
But I'm not understanding a lot of it.
It would be nice if someone could share a whole pseudocode what actually need to be done from the plain message until the writing into the buffer.
I also searched here a lot for information but mostly they advice libs or directly the link. The problem is that libs are nice, but i've actually done already all i need. Only the sending mask is missing.
Regards
Can you send the message with mask bit off?
When mask bit is set, the server expect 4 bytes mask to present, and the payload is XOR'd with
for ( size_t i=0; i<packet_len; i++ ) {
packet[i] = payload[i] ^ mask_bits[i&3];
}
EDIT: Adding more detail.
I assume you have done the Upgrade handshake, the actual bytes to send to network for communication are:
[flag][mask_bit, len][extended length][mask][payload]
Where:
flag: one byte, read the manual, note that I have not tried fragment.
mask bit, one bit located at highest bit of second byte.
len: 7 bits of second byte. If len is 126, real len is 16 bit field of extended length. if it is 127, extended length is 64-bit.
mask: present only if mask bit is set, it's 4 bytes using to XOR the payload.
payload: actual bytes to send
For example, to send "Hello" to server in binary mode, you do:
flag: 0x82, "Final packet in frame" and "Binary mode"
mask bit: 1
len: 6
mask: 0x11, 0x22, 0x33, 0x44
payload: "Hello", 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x00
The masked payload is: 0x48^0x11, 0x65^0x22, 0x6C^0x33, 0x6C^0x44, 0x6F^0x11, 0x00^0x22
=> 0x59, 0x47, 0x5F, 0x28, 0x7E, 0x22
The whole stream is: 0x82, 0x86, 0x11, 0x22, 0x33, 0x44, 0x59, 0x47, 0x5F, 0x28, 0x7E, 0x22

Sending Info Hash to Bittorrent Tracker Over UDP

So I'm writing a Bittorrent client in C++ and I can communicate with the tracker, however when I get the announce response my IP address is the only one I get in return. After looking at my traffic over wireshark and comparing it to the traffic to the same tracker in transmission I've concluded that I must be sending the info-hash of the torrent incorrectly.
So, I'm sending this as my info-hash of my test torrent (TPB AFK (totally legal to torrent!)):
99FEAE0A05C6A5DD9AF939FFCE5CA9B0D16F31B0
From reading a few other posts around the net I got the idea that I have to encode this somehow, and also that it needs to be 20 bytes (which I stupidly didn't realize it wasn't before...didn't count).
So there must be some sort of encoding that I must feed this hash into, but what is it?
Okay, so That string of 40 characters, is just 20 bytes...so the correct way to send the info_hash is:
0x99 0xFE 0xAE 0x0A 0x05 0xC6 0xA5 0xDD 0x9A 0xF9 0x39 0xFF 0xCE 0x5C 0xA9 0xB0 0xD1 0x6F 0x31 0xB0

CoreBluetooth read NSData

i try to read and write something in CoreBluetooth
So my device(peripheral) send me the lenght of a array
and the array as one.
I read the value with this method
(void)readValueForCharacteristic:(CBCharacteristic *)characteristic;
when i get the answer from the peripheral the
(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;
is calling
When i read one value it is not a problem
char arrayLength;
charakteristik.value getBytes:&arrayLength lenght:SERVICE_ARRAYLENGTH_LEN]
self.MyArrayLenght=(float)arrayLength;-
But when i get the array from the Peripheral i dont know how to read them
charakteristik.value is NSData
when i trying to
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:charakteristik.value]
i got error
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive (0x4e, 0x49, 0x42, 0x41, 0x72, 0x63, 0x68, 0x69)'
The hex (0x4e, 0x49, 0x42, 0x41, 0x72, 0x63, 0x68, 0x69.......) are the data what i want to read and later show
How can i read the NSData? Or How i can convert the NSData to NSArray?
Edit:
Problem solved
I am afraid that the unarchived NSData object from the peripheral is incomplete. CoreBluetooth limits maximum size of a characteristic could update each time, which is set by the central device. The exceeded part would be truncated while transferring. Probably the size of your array exceeds the size limitation, thus the central device did not receive the compete one, which results in exception.
The maximum length limitation varies from device to device, you can get the size limitation from the maximumUpdateValueLength property of the CBCentral object given by the CBPeipheralManagerDelegate methods:
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests
or any other methods that offer a CBCentral object.
If you want to send over-sized data, you should segment the data into pieces and send them through a couple of times. On the central device, you could rebuild the original data object by combining the pieces together. Apple provides a sample code to illustrate how to send over-sized data. BTLE Central Peripheral Transfer
Probably would have answered your question by now.
Just in case: you can read the NSData content with the method:
char *data = [NSData data];
The length of the data is retrieved with
[NSData length];
To check if it contains your data you can simply do something like:
strncmp(blah, [NSDataObject data], [NSDataObject length]);
Having said that, you can define which service you want to discover and only that one. You can then restrict even further to only listen to the particular characteristic you're interested with in
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
call
[peripheral discoverCharacteristics:[NSArray arrayWithObject:[CBUUID UUIDWithString:#"ABCD"]] forService:service];
Hope this help