File: crc.py

package info (click to toggle)
python-biopython 1.42-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,584 kB
  • ctags: 12,272
  • sloc: python: 80,461; xml: 13,834; ansic: 7,902; cpp: 1,855; sql: 1,144; makefile: 203
file content (32 lines) | stat: -rw-r--r-- 641 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# crc32 and crc64
# crc64 is adapted from BioPerl

from binascii import crc32

_table_h = []

def crc64(s):
    crcl = 0
    crch = 0
    for c in s:
        shr = (crch & 0xFF) << 24
        temp1h = crch >> 8
        temp1l = (crcl >> 8) | shr
        idx  = (crcl ^ ord(c)) & 0xFF
        crch = temp1h ^ _table_h[idx]
        crcl = temp1l

    return "CRC-%08X%08X" % (crch, crcl)

# Initialisation
for i in range(256):
    l = i
    part_h = 0
    for j in range(8):
        rflag = l & 1
        l >>= 1
        if part_h & 1: l |= (1L << 31)
        part_h >>= 1L
        if rflag: part_h ^= 0xd8000000L
    _table_h.append(part_h)