Last active
June 10, 2022 15:34
-
-
Save stephanedeluca/49820c8a2077b2a8ff485b42f5b0fe90 to your computer and use it in GitHub Desktop.
Reverse what I presume is a CRC16 from data samples, so that I can compte the CRC for whatever the data are.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Reverse the CRC16 from the data sample. | |
@Author Stéphane "Sdl" de Luca on Friday June 10th 2022 4:22 in Tangier, Morocco. | |
@See https://crypto.stackexchange.com/questions/100474/how-can-i-reverse-a-so-called-crc16-from-the-data/100511#100511 for indepth information. | |
*/ | |
int crc16(int value) { | |
const table = [ | |
//0xBB83, //0000 | |
0x7A43, // 0001 | |
0x3A42, // 0002 | |
0xBA40, // 0004 | |
0xBA45, // 0008 | |
0xBA4F, // 0010 | |
0xBA5B, // 0020 | |
0xBA73, // 0040 | |
0xBA23, // 0080 | |
0xBA13, // 0100 | |
0xBAE3, // 0200 | |
0xB943, // 0400 | |
0xBC43, // 0800 | |
0xB643, // 1000 | |
0xA243, // 2000 | |
0x8A43, // 4000 | |
0xDA43, // 8000 | |
]; | |
int crc16 = 0; | |
int bitsCount = 0; | |
for (int i = 0; i < 16; i++) { | |
final mask = 1<<i; | |
final bit = value & mask; | |
final bool bitIsSet = bit != 0; | |
if (!bitIsSet) continue; | |
if (bitsCount++ == 0) { | |
crc16 = table[i]; | |
} else { | |
crc16 ^= table[i]; | |
} | |
print("#${i.toString().padLeft(2)}: ${value.toRadixString(16).padLeft(4,'0')} (${value.toRadixString(2).padLeft(16,'-')}))} | mask:${bit.toRadixString(2).padLeft(16,'-')} | crc: ${crc16.toRadixString(16).padLeft(4,'0')}"); | |
} | |
final bool even = bitsCount & 1 == 0; | |
print("even: $even"); | |
if (even) crc16 ^= 0xBB83; // for 0000 | |
return crc16; | |
} | |
/* | |
For DDDD seconds: d2 06 00 8A DD DD 22 8A | |
For FFFF seconds: d2 06 00 8A FF FF BA 33 | |
For EEEE seconds: d2 06 00 8A EE EE 76 6F | |
For FFFE seconds: d2 06 00 8A FF FE 7B F3 | |
For 0000 seconds: d2 06 00 8A 00 00 BB 83 | |
*/ | |
void main() { | |
const table = [0x0000, 0xDDDD, 0xEEEE, 0xFFFE, 0xFFFF]; | |
for (int i = 0; i < table.length; i++) { | |
final v = table[i]; | |
final crc = crc16(v); | |
print("crc16[${v.toRadixString(16).padLeft(4, '0')}] => ${crc.toRadixString(16).padLeft(4, '0')}"); | |
} | |
} | |
/* | |
The logs: | |
even: true | |
crc16[0000] => bb83 | |
# 0: dddd (1101110111011101))} | mask:---------------1 | crc: 7a43 | |
# 2: dddd (1101110111011101))} | mask:-------------100 | crc: c003 | |
# 3: dddd (1101110111011101))} | mask:------------1000 | crc: 7a46 | |
# 4: dddd (1101110111011101))} | mask:-----------10000 | crc: c009 | |
# 6: dddd (1101110111011101))} | mask:---------1000000 | crc: 7a7a | |
# 7: dddd (1101110111011101))} | mask:--------10000000 | crc: c059 | |
# 8: dddd (1101110111011101))} | mask:-------100000000 | crc: 7a4a | |
#10: dddd (1101110111011101))} | mask:-----10000000000 | crc: c309 | |
#11: dddd (1101110111011101))} | mask:----100000000000 | crc: 7f4a | |
#12: dddd (1101110111011101))} | mask:---1000000000000 | crc: c909 | |
#14: dddd (1101110111011101))} | mask:-100000000000000 | crc: 434a | |
#15: dddd (1101110111011101))} | mask:1000000000000000 | crc: 9909 | |
even: true | |
crc16[dddd] => 228a | |
# 1: eeee (1110111011101110))} | mask:--------------10 | crc: 3a42 | |
# 2: eeee (1110111011101110))} | mask:-------------100 | crc: 8002 | |
# 3: eeee (1110111011101110))} | mask:------------1000 | crc: 3a47 | |
# 5: eeee (1110111011101110))} | mask:----------100000 | crc: 801c | |
# 6: eeee (1110111011101110))} | mask:---------1000000 | crc: 3a6f | |
# 7: eeee (1110111011101110))} | mask:--------10000000 | crc: 804c | |
# 9: eeee (1110111011101110))} | mask:------1000000000 | crc: 3aaf | |
#10: eeee (1110111011101110))} | mask:-----10000000000 | crc: 83ec | |
#11: eeee (1110111011101110))} | mask:----100000000000 | crc: 3faf | |
#13: eeee (1110111011101110))} | mask:--10000000000000 | crc: 9dec | |
#14: eeee (1110111011101110))} | mask:-100000000000000 | crc: 17af | |
#15: eeee (1110111011101110))} | mask:1000000000000000 | crc: cdec | |
even: true | |
crc16[eeee] => 766f | |
# 1: fffe (1111111111111110))} | mask:--------------10 | crc: 3a42 | |
# 2: fffe (1111111111111110))} | mask:-------------100 | crc: 8002 | |
# 3: fffe (1111111111111110))} | mask:------------1000 | crc: 3a47 | |
# 4: fffe (1111111111111110))} | mask:-----------10000 | crc: 8008 | |
# 5: fffe (1111111111111110))} | mask:----------100000 | crc: 3a53 | |
# 6: fffe (1111111111111110))} | mask:---------1000000 | crc: 8020 | |
# 7: fffe (1111111111111110))} | mask:--------10000000 | crc: 3a03 | |
# 8: fffe (1111111111111110))} | mask:-------100000000 | crc: 8010 | |
# 9: fffe (1111111111111110))} | mask:------1000000000 | crc: 3af3 | |
#10: fffe (1111111111111110))} | mask:-----10000000000 | crc: 83b0 | |
#11: fffe (1111111111111110))} | mask:----100000000000 | crc: 3ff3 | |
#12: fffe (1111111111111110))} | mask:---1000000000000 | crc: 89b0 | |
#13: fffe (1111111111111110))} | mask:--10000000000000 | crc: 2bf3 | |
#14: fffe (1111111111111110))} | mask:-100000000000000 | crc: a1b0 | |
#15: fffe (1111111111111110))} | mask:1000000000000000 | crc: 7bf3 | |
even: false | |
crc16[fffe] => 7bf3 | |
# 0: ffff (1111111111111111))} | mask:---------------1 | crc: 7a43 | |
# 1: ffff (1111111111111111))} | mask:--------------10 | crc: 4001 | |
# 2: ffff (1111111111111111))} | mask:-------------100 | crc: fa41 | |
# 3: ffff (1111111111111111))} | mask:------------1000 | crc: 4004 | |
# 4: ffff (1111111111111111))} | mask:-----------10000 | crc: fa4b | |
# 5: ffff (1111111111111111))} | mask:----------100000 | crc: 4010 | |
# 6: ffff (1111111111111111))} | mask:---------1000000 | crc: fa63 | |
# 7: ffff (1111111111111111))} | mask:--------10000000 | crc: 4040 | |
# 8: ffff (1111111111111111))} | mask:-------100000000 | crc: fa53 | |
# 9: ffff (1111111111111111))} | mask:------1000000000 | crc: 40b0 | |
#10: ffff (1111111111111111))} | mask:-----10000000000 | crc: f9f3 | |
#11: ffff (1111111111111111))} | mask:----100000000000 | crc: 45b0 | |
#12: ffff (1111111111111111))} | mask:---1000000000000 | crc: f3f3 | |
#13: ffff (1111111111111111))} | mask:--10000000000000 | crc: 51b0 | |
#14: ffff (1111111111111111))} | mask:-100000000000000 | crc: dbf3 | |
#15: ffff (1111111111111111))} | mask:1000000000000000 | crc: 01b0 | |
even: true | |
crc16[ffff] => ba33 | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment