Use python to implement CRC32 for string Verification Code

Source: Internet
Author: User
Tags crc32

The binascii module of python is used to calculate the character string verification code, which is a negative value.ProgramThe obtained verification code is different, so I studied it. I found that the CRC32 check code calculated by the binascii module of python3.0 is what I want. I can't implement the CRC32 using python.Algorithm. The discovery algorithm is very simple, that is, some implementations are complicated with several functions. In fact, it is easy to understand and apply the algorithms based on the Core algorithms.

Because it was directly transferred from the C program, we found that python2.5 (my version activepython 2.5.2.2 (ActiveState Software Inc.) based on
Python 2.5.2 (r252: 60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit (Intel)] On win32) does not return bitwise inversion, I found a lot on the InternetArticleAll about ~ In python, It is bitwise inversion. I tested it myself:

>>> ~ 3 ,~ 2 ,~ 1 ,~ 0 ,~ -1 ,~ -2 ,~ 3
(-4,-3,-2,-1, 0, 1,-4)

It was an error.

Let's look at it again:

>>> Import binascii
>>> Binascii. CRC32 ("we ")
-603163415

If the followingCodeThe penultimate line

Dwcrc32 = dwcrc32 ^ 0 xffffffffl

Change

Dwcrc32 = ~ Dwcrc32

Run mycrc32 ("we") and the result is-603163415.

So I suspect that python2.5 is also in this trap.

The code below is the same as the CRC32 Verification Code calculated by the C code. However, Python supports a greater positive number than C, so bitwise inversion only ensures that the same verification code as C code is obtained in 32-bit cases.

# Coding = GBK
Def mycrc32 (szstring ):
M_pdwcrc32table = [0 for X in range (0,256)]
Dwpolynomial = 0xedb88320;
Dwcrc = 0
For I in range (0,255 ):
Dwcrc = I
For J in [8, 7, 6, 5, 4, 3, 2, 1]:
If dwcrc & 1:
Dwcrc = (dwcrc> 1) ^ dwpolynomial
Else:
Dwcrc> = 1
M_pdwcrc32table [I] = dwcrc
Dwcrc32 = 0 xffffffffl
For I in szstring:
B = ord (I)
Dwcrc32 = (dwcrc32)> 8) ^ m_pdwcrc32table [(B) ^ (dwcrc32) & 0x000000ff)]
Dwcrc32 = dwcrc32 ^ 0 xffffffffl
Return dwcrc32

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.