Skip to content

Commit

Permalink
add CRC8 checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
JK-Rolling authored Oct 10, 2021
1 parent 8e4945a commit ac9508d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions DuinoCoin_RPI_Arduino_Slave/DuinoCoin_Utils.ino
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,27 @@ bool check(String message)
}
return false;
}

// https://stackoverflow.com/questions/51731313/cross-platform-crc8-function-c-and-python-parity-check
uint8_t crc8( uint8_t *addr, uint8_t len) {
uint8_t crc=0;
for (uint8_t i=0; i<len;i++) {
uint8_t inbyte = addr[i];
for (uint8_t j=0;j<8;j++) {
uint8_t mix = (crc ^ inbyte) & 0x01;
crc >>= 1;
if (mix)
crc ^= 0x8C;
inbyte >>= 1;
}
}
return crc;
}

uint8_t str_crc8( String msg )
{
int msg_len = msg.length() + 1;
char char_array[msg_len];
msg.toCharArray(char_array, msg_len);
return crc8(char_array,msg.length());
}

0 comments on commit ac9508d

Please sign in to comment.