- 精华
- 0
- 帖子
- 343
- 威望
- 0 点
- 积分
- 386 点
- 种子
- 12 点
- 注册时间
- 2009-2-9
- 最后登录
- 2024-5-6
|
发表于 2011-9-29 21:56 · 云南
|
显示全部楼层
本帖最后由 頭文字D 于 2011-9-29 21:57 编辑
class CRC32:
def __init__(self):
self.gentable()
def crc32(self, input, crc=0xffffffffl):
count = len(input)
i = 0
while count != 0:
count -= 1
temp1 = (crc >> 8) & 0xFFFFFF
temp2 = self.table[(crc ^ ord(input)) & 0xFF]
crc = temp1 ^ temp2
i += 1
return crc
def gentable(self):
self.table = []
for i in range(256):
crc = i
for j in range(8):
if crc & 1:
crc = (crc >> 1) ^ 0xEDB88320l
else:
crc >>= 1
self.table.append(crc)
def error(s):
return '<div class="error">%s</div>'%s
def process():
try:
int(form["number"][0]) #validate
if len(form["number"][0]) != 8 or not all([x in "0123456789" for x in form["number"][0]]):
raise ValueError()
except:
return error("Please provide a valid 8-digit confirmation number")
try:
int(form["date"][0]) #validate
if len(form["date"][0]) != 4 or not all([x in "0123456789" for x in form["date"][0]]):
raise ValueError()
except:
return error("Invalid date")
fullnum = form["date"][0] + form["number"][0][4:8]
crc = CRC32().crc32(fullnum)
code = ((crc ^ 0xaaaa) + 0x14c1) % 100000
这套算法有点意思
话说Python语言看起来满简洁的 和Pascal挺像 |
|