-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0fb277d
commit acebf1a
Showing
3 changed files
with
68 additions
and
36 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import sys | ||
|
||
|
||
class CRC(object): | ||
def __init__(self, polynomial=0x9B, crc_len=8): | ||
self.poly = polynomial & 0xFF | ||
self.crc_len = crc_len | ||
self.table_len = pow(2, crc_len) | ||
self.cs_table = [' ' for x in range(self.table_len)] | ||
|
||
self.generate_table() | ||
|
||
def generate_table(self): | ||
for i in range(len(self.cs_table)): | ||
curr = i | ||
|
||
for j in range(8): | ||
if (curr & 0x80) != 0: | ||
curr = ((curr << 1) & 0xFF) ^ self.poly | ||
else: | ||
curr <<= 1 | ||
|
||
self.cs_table[i] = curr | ||
|
||
def print_table(self): | ||
for i in range(len(self.cs_table)): | ||
sys.stdout.write(hex(self.cs_table[i]).upper().replace('X', 'x')) | ||
|
||
if (i + 1) % 16: | ||
sys.stdout.write(' ') | ||
else: | ||
sys.stdout.write('\n') | ||
|
||
def calculate(self, arr, dist=None): | ||
crc = 0 | ||
|
||
try: | ||
if dist: | ||
indicies = dist | ||
else: | ||
indicies = len(arr) | ||
|
||
for i in range(indicies): | ||
crc = self.cs_table[crc ^ arr[i]] | ||
|
||
except TypeError: | ||
crc = self.cs_table[arr] | ||
|
||
return crc | ||
|
||
|
||
if __name__ == '__main__': | ||
crc = CRC() | ||
print(crc.print_table()) | ||
print(' ') | ||
print(hex(crc.calculate(0x31)).upper().replace('X', 'x')) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from . import * | ||
from . import * |
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