Example #1
0
 def __init__(self, name, poly, endian='!'):
     if type(poly) == str:
         self.crc = PredefinedCrc(poly)
     elif type(poly) == int:
         self.poly = poly
         self.crc = Crc(poly)
     Checksum.__init__(self, name, self.crc.digest_size, endian)
Example #2
0
    def test_simple_crc32_class(self):
        """Verify the CRC class when not using xorOut"""
        crc = Crc(g32)

        str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc  = 0xFFFFFFFF
xorOut   = 0x00000000
crcValue = 0xFFFFFFFF'''
        self.assertEqual(str(crc), str_rep)
        self.assertEqual(crc.digest(), '\xff\xff\xff\xff')
        self.assertEqual(crc.hexdigest(), 'FFFFFFFF')

        crc.update(self.msg)
        self.assertEqual(crc.crcValue, 0xF7B400A7L)
        self.assertEqual(crc.digest(), '\xf7\xb4\x00\xa7')
        self.assertEqual(crc.hexdigest(), 'F7B400A7')

        # Verify the .copy() method
        x = crc.copy()
        self.assertTrue(x is not crc)
        str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc  = 0xFFFFFFFF
xorOut   = 0x00000000
crcValue = 0xF7B400A7'''
        self.assertEqual(str(crc), str_rep)
        self.assertEqual(str(x), str_rep)
Example #3
0
    def test_simple_crc32_class(self):
        """Verify the CRC class when not using xorOut"""
        crc = Crc(g32)

        str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc  = 0xFFFFFFFF
xorOut   = 0x00000000
crcValue = 0xFFFFFFFF'''
        self.assertEqual(str(crc), str_rep)
        self.assertEqual(crc.digest(), '\xff\xff\xff\xff')
        self.assertEqual(crc.hexdigest(), 'FFFFFFFF')

        crc.update(self.msg)
        self.assertEqual(crc.crcValue, 0xF7B400A7L)
        self.assertEqual(crc.digest(), '\xf7\xb4\x00\xa7')
        self.assertEqual(crc.hexdigest(), 'F7B400A7')

        # Verify the .copy() method
        x = crc.copy()
        self.assertTrue(x is not crc)
        str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc  = 0xFFFFFFFF
xorOut   = 0x00000000
crcValue = 0xF7B400A7'''
        self.assertEqual(str(crc), str_rep)
        self.assertEqual(str(x), str_rep)
Example #4
0
    def test_full_crc32_class(self):
        """Verify the CRC class when using xorOut"""

        crc = Crc(g32, initCrc=0, xorOut=~0L)

        str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc  = 0x00000000
xorOut   = 0xFFFFFFFF
crcValue = 0x00000000'''
        self.assertEqual(str(crc), str_rep)
        self.assertEqual(crc.digest(), '\x00\x00\x00\x00')
        self.assertEqual(crc.hexdigest(), '00000000')

        crc.update(self.msg)
        self.assertEqual(crc.crcValue, 0x84BFF58L)
        self.assertEqual(crc.digest(), '\x08\x4b\xff\x58')
        self.assertEqual(crc.hexdigest(), '084BFF58')

        # Verify the .copy() method
        x = crc.copy()
        self.assertTrue(x is not crc)
        str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc  = 0x00000000
xorOut   = 0xFFFFFFFF
crcValue = 0x084BFF58'''
        self.assertEqual(str(crc), str_rep)
        self.assertEqual(str(x), str_rep)

        # Verify the .new() method
        y = crc.new()
        self.assertTrue(y is not crc)
        self.assertTrue(y is not x)
        str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc  = 0x00000000
xorOut   = 0xFFFFFFFF
crcValue = 0x00000000'''
        self.assertEqual(str(y), str_rep)
Example #5
0
def dot11crc(pkt):
    #crc_fun = crcmod.Crc(0b100000100110000010001110110110111, rev=True, initCrc=0x0, xorOut=0xFFFFFFFF) # this was valid for python2
    #crc_fun.update(str(pkt))   # this was valid for python2

    # FIXME: the next 2 lines do not work for python3
    crc_fun = crcmod.mkCrcFun(0b100000100110000010001110110110111,
                              initCrc=0,
                              xorOut=0xFFFFFFFF)
    crc_fun = Crc(crc_fun, 0)

    crc = struct.pack('<I', crc_fun.crcValue)
    return crc
Example #6
0
    def is_exist(self, storage, file_path, category, now_date):
        file_name = file_path.split(sep)[-1]
        for curdir, dirs, files in walk(self.current_dir + storage + sep):
            if curdir.split(
                    sep)[-1] == category and curdir.split(sep)[-2] != now_date:
                for file in files:
                    if file[:-8] == file_name:
                        checksumm = Crc(0x104c11db7)
                        checksumm.update(file_path.encode('utf-8'))
                        checksumm.update(str(stat(file_path)[-2]))
                        with open(file_path, 'rb') as reader:
                            chunk = reader.read(1024 * 1024 * 8)
                            while chunk:
                                checksumm.update(chunk)
                                chunk = reader.read(1024 * 1024 * 8)

                        if checksumm.hexdigest() == file[-8:]:
                            return True
        return False
Example #7
0
    def test_full_crc32_class(self):
        """Verify the CRC class when using xorOut"""

        crc = Crc(g32, initCrc=0, xorOut= ~0L)

        str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc  = 0x00000000
xorOut   = 0xFFFFFFFF
crcValue = 0x00000000'''
        self.assertEqual(str(crc), str_rep)
        self.assertEqual(crc.digest(), '\x00\x00\x00\x00')
        self.assertEqual(crc.hexdigest(), '00000000')

        crc.update(self.msg)
        self.assertEqual(crc.crcValue, 0x84BFF58L)
        self.assertEqual(crc.digest(), '\x08\x4b\xff\x58')
        self.assertEqual(crc.hexdigest(), '084BFF58')

        # Verify the .copy() method
        x = crc.copy()
        self.assertTrue(x is not crc)
        str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc  = 0x00000000
xorOut   = 0xFFFFFFFF
crcValue = 0x084BFF58'''
        self.assertEqual(str(crc), str_rep)
        self.assertEqual(str(x), str_rep)

        # Verify the .new() method
        y = crc.new()
        self.assertTrue(y is not crc)
        self.assertTrue(y is not x)
        str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc  = 0x00000000
xorOut   = 0xFFFFFFFF
crcValue = 0x00000000'''
        self.assertEqual(str(y), str_rep)
Example #8
0
class CRC(Checksum):
    """
    Uses the `crcmod.Crc` class for calculating proper CRC's. Expects a
    generator polynom `poly` either as an `int` or as a `str`. If `poly` is a
    `str` it is interpreted as a name of a well-known crc-function and looked
    up from `crcmod.predefined` using the `crcmod.predefined.PredefinedCRC`
    module.
    """

    def __init__(self, name, poly, endian='!'):
        if type(poly) == str:
            self.crc = PredefinedCrc(poly)
        elif type(poly) == int:
            self.poly = poly
            self.crc = Crc(poly)
        Checksum.__init__(self, name, self.crc.digest_size, endian)

    def checksum(self, bytes):
        crc = self.crc.new(bytes)
        return int(crc.hexdigest(), 16)
Example #9
0
#!/usr/bin/env python

import sys, math, binascii, struct
from bitarray import bitarray
from crcmod import Crc
from collections import defaultdict
from hamm import hamming

# poly: x16+x13+x12+x11+x10+x8+x6+x5+x2+1
poly = (1 << 16) + (1 << 13) + (1 << 12) + (1 << 11) + (1 << 10) + (1 << 8) + (
    1 << 6) + (1 << 5) + (1 << 2) + 1
CRC16 = Crc(poly, initCrc=0xffff, rev=False, xorOut=0xffff)


def check(data, crc):
    crc16 = CRC16.copy()
    crc16.update(data)
    return crc == crc16.digest()


def todate(d):
    d = [ord(c) for c in d]
    return '%04d-%02d-%02d' % ((d[1] >> 5) + 2014, ((d[1] >> 1) & 0xf),
                               ((d[1] & 1) << 4) | (d[0] >> 4))


def dump(decoded):
    manuf = []
    vendor = struct.unpack("<H", decoded[2:4])[0]
    for i in xrange(3):
        manuf.append(chr(0x40 + (vendor & 0x1f)))
Example #10
0
# Magnetic Tape Cartridges -DLT1 Format-", December 1992.

g64 = polyFromBits([
    64, 62, 57, 55, 54, 53, 52, 47, 46, 45, 40, 39, 38, 37, 35, 33, 32, 31, 29,
    27, 24, 23, 22, 21, 19, 17, 13, 12, 10, 9, 7, 4, 1, 0
])

print('Generating examples.c')
out = open('examples.c', 'w')
out.write('''// Define the required data types
typedef unsigned char      UINT8;
typedef unsigned short     UINT16;
typedef unsigned int       UINT32;
typedef unsigned long long UINT64;
''')
Crc(g8, rev=False).generateCode('crc8', out)
Crc(g8, rev=True).generateCode('crc8r', out)
Crc(g16, rev=False).generateCode('crc16', out)
Crc(g16, rev=True).generateCode('crc16r', out)
Crc(g24, rev=False).generateCode('crc24', out)
Crc(g24, rev=True).generateCode('crc24r', out)
Crc(g32, rev=False).generateCode('crc32', out)
Crc(g32, rev=True).generateCode('crc32r', out)
Crc(g64, rev=False).generateCode('crc64', out)
Crc(g64, rev=True).generateCode('crc64r', out)

# Check out the XOR-out feature.
Crc(g16, initCrc=0, rev=True, xorOut=~0).generateCode('crc16x', out)
Crc(g24, initCrc=0, rev=True, xorOut=~0).generateCode('crc24x', out)
Crc(g32, initCrc=0, rev=True, xorOut=~0).generateCode('crc32x', out)
Crc(g64, initCrc=0, rev=True, xorOut=~0).generateCode('crc64x', out)
Example #11
0
    def encrypt_and_save_to(self, warehouse):
        files_count = sum(len(f) for f in self.files_found.values())
        self.progressBarBackup.setMaximum(files_count)
        self.progressBarBackup.setValue(0)
        now_date = datetime.datetime.now().strftime('Backup_%m-%d-%y_%H-%M')
        mkdir(self.current_dir + warehouse + sep + now_date + sep)
        for category, files in self.files_found.items():
            current_dir = self.current_dir + warehouse + sep + now_date + sep + category + sep
            if not path.exists(current_dir):
                mkdir(current_dir)
            print(files)
            for file_path in files:
                if file_path == 'deleted':
                    qApp.processEvents()
                    self.progressBarBackup.setValue(
                        self.progressBarBackup.value() + 1)
                    continue
                file_name = file_path.split(sep)[-1]
                if not self.is_exist(warehouse, file_path, category, now_date):
                    checksumm = Crc(0x104c11db7)
                    checksumm.update(file_path.encode('utf-8'))
                    checksumm.update(str(
                        stat(file_path)[-2]))  # modification time
                    with open(file_path,
                              'rb') as reader, open(current_dir + file_name,
                                                    'wb') as writer:
                        chunksize = 64 * 1024 * 16
                        iv = ''.join(
                            chr(random.randint(0, 0xFF)) for _ in range(16))
                        cipher = AES.new(
                            self.password + self.padding[len(self.password):],
                            AES.MODE_CBC, iv)
                        filesize = path.getsize(file_path)

                        writer.write(pack('<Q', filesize))
                        writer.write(iv)
                        writer.write(pack('<B',
                                          len(file_path.encode('utf-8'))))
                        writer.write(file_path.encode('utf-8'))

                        part = reader.read(chunksize)
                        checksumm.update(part)
                        while part:
                            if len(part) % 16 != 0:
                                part += ' ' * (16 - len(part) % 16)
                            ciphertext = cipher.encrypt(part)
                            writer.write(ciphertext)
                            part = reader.read(chunksize)
                            checksumm.update(part)
                    rename(current_dir + file_name,
                           current_dir + file_name + checksumm.hexdigest())

                qApp.processEvents()
                self.progressBarBackup.setValue(
                    self.progressBarBackup.value() + 1)
        self.progressBarBackup.setValue(self.progressBarBackup.maximum())
Example #12
0
"""
#   Name           Identifier-name,         Poly            Reverse         Init-value      XOR-out     Check
[   'crc-8',            'Crc8',             0x107,          NON_REVERSE,    0x00,           0x00,       0xF4,       ],
"""
from io import StringIO
from crcmod import Crc
c8 = 0x107
code = StringIO()
Crc(c8, rev=False).generateCode('crc8', code)

out = open('opsis_eeprom_crc.c', 'w')
out.write(code.getvalue().replace('UINT8', '__u8'))
out.close()
Example #13
0
    64, 62, 57, 55, 54, 53, 52, 47, 46, 45, 40, 39, 38, 37, 35, 33, 32, 31, 29,
    27, 24, 23, 22, 21, 19, 17, 13, 12, 10, 9, 7, 4, 1, 0
])

print('Generating crc.c')
out = open('crc.c', 'w')
out.write('''// Define the required data types
typedef unsigned char      UINT8;
typedef unsigned short     UINT16;
typedef unsigned int       UINT32;
typedef unsigned long long UINT64;
''')
#Crc(g8, rev=False).generateCode('crc8',out)
#Crc(g8, rev=True).generateCode('crc8r',out)
#Crc(g16, rev=False).generateCode('crc16',out)
Crc(g16, rev=True).generateCode('crc16r', out)
#Crc(g24, rev=False).generateCode('crc24',out)
#Crc(g24, rev=True).generateCode('crc24r',out)
#Crc(g32, rev=False).generateCode('crc32',out)
Crc(g32, rev=True).generateCode('crc32r', out)
#Crc(g64, rev=False).generateCode('crc64',out)
#Crc(g64, rev=True).generateCode('crc64r',out)

# Check out the XOR-out feature.
#Crc(g16, initCrc=0, rev=True, xorOut=~0).generateCode('crc16x',out)
#Crc(g24, initCrc=0, rev=True, xorOut=~0).generateCode('crc24x',out)
#Crc(g32, initCrc=0, rev=True, xorOut=~0).generateCode('crc32x',out)
#Crc(g64, initCrc=0, rev=True, xorOut=~0).generateCode('crc64x',out)

out.close()
print('Done')
Example #14
0
from __future__ import division, print_function

from crcmod import Crc
from base64 import b64encode, b64decode
from struct import unpack, pack

crc24pgp = Crc(0x1864CFB, initCrc=0xB704CE, rev=False, xorOut=0)

TEMPLATE = (b'-----BEGIN PGP {what}-----\x0a\x0a'
            b'{b64}\x0a'
            b'={crc}\x0a\x0a'
            b'-----END PGP {what}-----\x0a')


def armor(s, what="PUBLIC KEY BLOCK"):
    crc = b64encode(crc24pgp.new(s).digest())
    return TEMPLATE.format(what=what, crc=crc, b64=b64encode(s))