Understanding results of CRC8 SAE J1850 (normal) vs. "Zero"
<p>I need a verification of CRC8-SAE-J1850 messages and therefore wrote a script, that reads logs and needs to calculate CRC8 (non ZERO) from there to match them with the CRC8 values in the log and then check which step in the tool chain caused the hassle.</p>

<p>Anyway, I spent some time looking into documentation, SO posts and other peoples sourcecode, but I want to stick with python for easier textprocessing and interfaces to my other tools. </p>

<p>I found some code on sourceforge <a href="https://gist.github.com/evansneath/4650991" rel="nofollow">evansneath's Python CRC Implementation</a> which is nicely straight forward and wanted to give it a go, but I figured it does not work as expected (maybe I understood something completely wrong, but I am stuck here):</p>

<pre class="lang-py prettyprint-override"><code>def crc(msg, div, code='11111111'):
    """Cyclic Redundancy Check
        Generates an error detecting code based on an inputted message and divisor in the form of a polynomial representation.
        Arguments:
        msg: The input message of which to generate the output code.
        div: The divisor in polynomial form. For example, if the polynomial of x^3 + x + 1 is given, this should be represented as '1011' in the div argument.
        code: This is an option argument where a previously generated code may be passed in. This can be used to check validity. If the inputted code produces an outputted code of all zeros, then the message has        no errors.
    Returns:
        An error-detecting code generated by the message and the given divisor.
    """

    msg = msg + code
    msg = list (msg)
    div = list (div)

    for i in range (len (msg) - len (code)):
        if msg[i] == '1':
            for j in range (len (div)):
               msg[i+j] = str ((int (msg[i+j])+int (div[j]))%2)

    return ''.join (msg[-len (code):])

#Testing:
# Use a divisor that simulates: CRC8 SAE J1850 x^8+x^4+x^3+x^2+x^0
div = '100011101'#0x1D with leading 1 as given by polynomial
msg = '10101001' # 0xA9, just for a Test

print('Input message:', hex(int(msg,2)))
print('Polynomial:', hex(int(div,2)))

o = '11111111'
z = '00000000'

code = crc(msg, div, o)

print('CRC8 code:', hex(int(code,2)))

# Test for output code of '00000000' respectively '11111111' proving that the function worked correctly
print('Success:', crc(msg, div, code) == o)
</code></pre>

<p>I checked the results using this generator:
<a href="http://www.sunshine2k.de/coding/javascript/crc/crc_js.html" rel="nofollow">CRC Generator</a> which seems to be the only one featuring CRC8 SAE J1850 ZERO and non-ZERO. </p>

<p>Now the fun part: for ZERO the above code works perfectly fine. </p>

<p>Unfortunately the CRC codes I get from the software I want to check are initialized and checked against 0xFF ('11111111') where both tools deliver completely different results.
So far I wasn't even able to find some off-by-one issue (which I would have rated the most likely case) or a mathematical link between the solution calculated by the script above and the one by the website. 
The website complies with the result from the software however the python part above doesn't.</p>

<p>Can anyone point me to a documentation I might have missed or is it another issue? 
I already checked for MSB/LSB issues when entering messages on the website and tried initializing with 0xFE as some other code suggested. But no success... Most examples however are zero based and there I have no issues.</p>

<p><strong>Edit</strong>:</p>

<p>I checked the calculation and went through an example by hand as well as printing every single step and achieved the same. So mathematically it seems correct, but what is the SAE doing other than appending a line of '11111111' and then XOR-ing bitwise, then shifting until there is a leading one again, XOR-ing etc... and spitting out the remainder? It must be an issue of understanding on my side. </p>

<pre><code>Test 1 ---------------------------
Input message: 0xa9
Polynome: 0x11d
['1', '0', '1', '0', '1', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', 1']
 /
['1', '0', '0', '0', '1', '1', '1', '0', '1']
 =
current message: ['1', '0', '1', '0', '1', '0', '0', '1', '1', '1', '1', '1', '1 ', '1', '1', '1']
shift 0 Bits
1 XOR 1 = 0
0 XOR 0 = 0
1 XOR 0 = 1
0 XOR 0 = 0
1 XOR 1 = 0
0 XOR 1 = 1
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0
current message: ['0', '0', '1', '0', '0', '1', '1', '1', '0', '1', '1', '1', '1 ', '1', '1', '1']
shift 2 Bits
1 XOR 1 = 0
0 XOR 0 = 0
0 XOR 0 = 0
1 XOR 0 = 1
1 XOR 1 = 0
1 XOR 1 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0
current message: ['0', '0', '0', '0', '0', '1', '0', '0', '1', '1', '0', '1', '1 ', '1', '1', '1']
shift 5 Bits
1 XOR 1 = 0
0 XOR 0 = 0
0 XOR 0 = 0
1 XOR 0 = 1
1 XOR 1 = 0
0 XOR 1 = 1
1 XOR 1 = 0
1 XOR 0 = 1
1 XOR 1 = 0
CRC8 code: 0xab

Reverse Calculation: Check
['1', '0', '1', '0', '1', '0', '0', '1', '1', '0', '1', '0', '1', '0', '1', '1']
 /
['1', '0', '0', '0', '1', '1', '1', '0', '1']
 =
current message: ['1', '0', '1', '0', '1', '0', '0', '1', '1', '0', '1', '0', '1', '0', '1', '1']
shift 0 Bits
1 XOR 1 = 0
0 XOR 0 = 0
1 XOR 0 = 1
0 XOR 0 = 0
1 XOR 1 = 0
0 XOR 1 = 1
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0
current message: ['0', '0', '1', '0', '0', '1', '1', '1', '0', '0', '1', '0', '1', '0', '1', '1']
shift 2 Bits
1 XOR 1 = 0
0 XOR 0 = 0
0 XOR 0 = 0
1 XOR 0 = 1
1 XOR 1 = 0
1 XOR 1 = 0
0 XOR 1 = 1
0 XOR 0 = 0
1 XOR 1 = 0
current message: ['0', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '1', '0', '1', '1']
shift 5 Bits
1 XOR 1 = 0
0 XOR 0 = 0
0 XOR 0 = 0
1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 1 = 1
0 XOR 1 = 1
1 XOR 0 = 1
0 XOR 1 = 1
CRC correct: True
</code></pre>