Nws Yooj Yim Npaum Li Cas Los Xam CRC Checksum (CRC32 - CRC16 - CRC8)

Cov txheej txheem:

Nws Yooj Yim Npaum Li Cas Los Xam CRC Checksum (CRC32 - CRC16 - CRC8)
Nws Yooj Yim Npaum Li Cas Los Xam CRC Checksum (CRC32 - CRC16 - CRC8)

Video: Nws Yooj Yim Npaum Li Cas Los Xam CRC Checksum (CRC32 - CRC16 - CRC8)

Video: Nws Yooj Yim Npaum Li Cas Los Xam CRC Checksum (CRC32 - CRC16 - CRC8)
Video: Cyclic Redundancy Check(CRC) example 2024, Hlis ntuj nqeg
Anonim

Muaj ntau txoj kev xaiv rau xam CRC checksum hauv Is Taws Nem. Tab sis dab tsi xyov yog tus sau nyiaj ib zaug thiab vim li cas nws thiaj xam tau txoj hauv kev no? Cia peb paub tseeb.

Nws yooj yim npaum li cas los xam CRC checksum (CRC32 - CRC16 - CRC8)
Nws yooj yim npaum li cas los xam CRC checksum (CRC32 - CRC16 - CRC8)

Cov Lus Qhia

Kauj ruam 1

Ua ntej, cia peb tau txais me ntsis kev xav. Yog li CRC yog dab tsi tiag? Hauv ntej, qhov no yog ib qho ntawm ntau yam ntawm checksum xam. Checksum yog ib txoj kev los ntawm kev kuaj xyuas kev ncaj ncees ntawm cov ntaub ntawv tau txais ntawm sab txais khoom thaum kis dhau cov xov tooj sib txuas lus. Piv txwv, ib qho ntawm cov tshev yooj yim tshaj plaws yog kev siv me ntsis parity. Qhov no yog thaum txhua qhov me me ntawm cov lus sib kis tau yog summed, thiab yog tias tus lej hloov tawm mus rau txawm tias, tom qab ntawd 0 ntxiv rau qhov kawg ntawm cov lus, yog tias nws yog khib, ces 1. Thaum tau txais, cov lej ntawm xov kab kuj tseem suav thiab piv nrog cov tau txais me ntsis. Yog tias lawv sib txawv, tom qab ntawd yuam kev tau tshwm sim thaum kis thiab cov ntaub ntawv sib kis tau cuam tshuam.

Tab sis txoj kev no ntawm kev kuaj pom qhov muaj qhov tsis raug yog qhov tsis paub thiab tsis tas ua haujlwm, vim hais tias yog tias ob peb qho ntawm cov lus sau tau cuam tshuam, qhov kev hloov ntawm tag nrho yuav tsis hloov. Yog li no, muaj ntau qhov kev paub ntau tshaj "tshaj lij", suav nrog CRC.

Qhov tseeb, CRC tsis yog cov lej, tab sis ntawm kev faib cov nqi ntawm cov ntaub ntawv (xov xwm ntaub ntawv) los ntawm tas mus li, lossis theej, qhov tseem ceeb ntawm kev faib cov lus los ntawm tas mus li. Txawm li cas los xij, CRC tseem muaj keeb kwm hu ua "tshev". Txhua cov lus tsawg zuj zus mus rau CRC tus nqi. Ntawd yog, yog tias tsawg kawg ib qho kev hloov pauv ntawm thawj lub xov xwm thaum lub sijhawm sib kis, daim tshev tseem yuav hloov, thiab tseem ceeb. Qhov no yog qhov loj ntxiv ntawm xws li ib daim tshev, txij li nws tso cai rau koj los txiav txim siab seb cov ntawv qub puas tau cuam tshuam thaum lub sijhawm sib kis lossis tsis.

Kauj ruam 2

Ua ntej peb pib suav qhov CRC, yuav tsum muaj kev tshawb pom me ntsis ntxiv.

Cov lus dab tsi yuav tsum pom tseeb. Nws yog cov kab sib tshooj ntawm cov kav ntev npaum li cas.

Dab tsi yog qhov tsis tu ncua uas peb yuav tsum faib cov lus qub? Tus lej no kuj yog qhov ntev, tab sis feem ntau cov lej ntawm 1 byte siv - 8, 16 thiab 32 me me. Nws yooj yim dua rau suav xwb, vim tias khoos phis tawj ua haujlwm nrog bytes, tsis nrog me me.

Qhov divisor tas mus li feem ntau yog sau raws li ntawv sau npe (polynomial) zoo li no: x ^ 8 + x ^ 2 + x ^ 1 + x ^ 0. Ntawm no, qhov degree ntawm tus naj npawb "x" txhais tau hais tias qhov chaw ntawm ib-ntsis hauv tus lej, pib ntawm xoom, thiab qhov tseem ceeb tshaj plaws qhia tias qhov degree ntawm polynomial thiab raug muab pov tseg thaum txhais tus lej. Ntawd yog, tus lej sau yav dhau los tsis muaj dab tsi ntau tshaj (1) 00000111 hauv binary, lossis 7 hauv tus lej. Nyob rau kev thaiv hom phiaj, Kuv qhia tus lej tseem ceeb ntawm tus lej.

Nov yog piv txwv ntxiv: x ^ 16 + x ^ 15 + x ^ 2 + x ^ 0 = (1) 1000000000000101 = 0x8005 = 32773.

Feem ntau qee cov qauv polynomials siv rau ntau hom CRC.

Kauj ruam 3

Yog li koj yuav ua li cas koj thiaj li sau tau cov nqi tshev? Muaj cov txheej txheem yooj yim - faib cov lus rau hauv polynomial "lub taub hau-rau" - thiab nws cov kev hloov kho hauv kev txiav txim txhawm rau txo cov naj npawb ntawm cov kev suav thiab, raws li, kom cov CRC xam. Peb yuav saib txoj kev pib yooj yim.

Feem ntau, kev faib cov naj npawb los ntawm polynomial yog ua raws li cov hauv qab no algorithm:

1) ib qho kev sau npe (sau npe) yog tsim, puv nrog cov xoom, sib npaug hauv qhov ntev mus rau qhov ntev ntawm polynomial dav;

2) cov lus qub yog ntxiv nrog zeros hauv qhov tsawg tshaj plaws qhov tseem ceeb, hauv ib qhov nqi sib npaug nrog cov lej ntawm cov polynomial;

3) ib qho tseem ceeb tshaj plaws ntawm cov xov tau nkag mus rau hauv qhov tsawg kawg nkaus qhov tseem ceeb ntawm cov ntawv sau npe, thiab ib qho me me tau tsiv los ntawm qhov tseem ceeb tshaj plaws ntawm daim ntawv sau npe;

4) yog hais tias qhov sib txuas ntawm qhov sib npaug sib npaug ntawm "1", tom qab ntawd cov khoom me me yog qhov hloov pauv (XOR haujlwm, tshwj rau LOSSIS) hauv cov ntawv sau npe uas sib haum nrog tus hauv cov polynomial;

5) yog tias tseem muaj cov me nyob hauv cov lus, mus rau theem 3);

6) thaum tag nrho cov khoom me me ntawm cov lus tau nkag rau hauv kev sau npe thiab tau ua tiav los ntawm cov txheej txheem no, cov seem ntawm cov kev faib tawm tseem nyob hauv kev tso npe, uas yog CRC checksum.

Daim duab ua piv txwv qhia qhov sib faib ntawm thawj qhov qub ua ntu zus los ntawm tus xov tooj (1) 00000111, lossis polynomial x ^ 8 + x ^ 2 + x ^ 1 + x ^ 0.

Schematic sawv cev ntawm CRC xam
Schematic sawv cev ntawm CRC xam

Kauj ruam 4

Muaj ob peb ntawm ntxiv kov sab laug. Raws li koj tau pom, cov lus qhia tuaj yeem faib los ntawm tus lej twg. Yuav xaiv nws li cas? Muaj ntau tus lej ntawm cov qib polynomials uas tau siv los laij cov CRC. Piv txwv li, rau CRC32 nws yuav yog 0x04C11DB7, thiab rau CRC16 nws yuav yog 0x8005.

Tsis tas li ntawd, nyob rau hauv kev tso npe thaum pib ntawm kev suav, koj tuaj yeem sau tsis tus lej, tab sis qee tus lej.

Tsis tas li, thaum lub sijhawm suav nyiaj, tam sim ntawd ua ntej muab CRC cov ntawv kuaj zaum kawg, tuaj yeem faib ua qee tus lej.

Thiab kawg li. Bytes ntawm cov lus qhia thaum sau ntawv rau hauv kev sau npe tuaj yeem tso raws li qhov tseem ceeb tshaj plaws "rau pem hauv ntej", thiab hloov mus, tsawg kawg yog qhov tseem ceeb tshaj plaws.

Kauj ruam 5

Raws li txhua yam saum toj no, cia sau sau lub hauv paus. NET txoj haujlwm uas suav CRC checksum los ntawm kev siv qee qhov kev ntsuas uas kuv tau piav qhia saum toj thiab xa rov qab rau CRC tus nqi raws li 32-ntsis tsis tso npe.

Pej Xeem Koom Kev Ua Haujlwm GetCrc (ByVal bytes As Byte (), ByVal poly Raws li UInteger, Yeem ByVal qhov dav li Tus lej = 32, Los ntawm ByVal initReg Raws li UInteger = & HFFFFFFFFUI, Yeem ntawm ByVal kawgXor Raws li UInteger = & HFFFFFFFFUI, Xaiv los ntawm ByVal reverseBytes, reverseCrc Raws li Boolean = Tseeb) Raws li UInteger

Dim qhov davInBytes Raws Li Qhov Sib = Dav = 8

'Ntxiv cov lus dav dav nrog zeros (suav hauv bytes):

ReDim Khaws bytes (bytes. Ntev - 1 + widthInBytes)

'Tsim kab me ntsis los ntawm cov lus:

Dim msgFifo Raws li Tshiab Queue (Ntawm Boolean) (bytes. Count * 8 - 1)

Rau Txhua b Li Byte Hauv bytes

Dim ba Raws li Tshiab BitArray ({b})

Yog hais tias rov qabBytes Tom qab ntawd

Rau Kuv Xws Li = 0 Kom 7

msgFifo. Enqueue (ba (i))

Ntxiv mus

Lwm Tus

Rau Kuv Xws Li = 7 Rau 0 Kauj Ruam -1

msgFifo. Enqueue (ba (i))

Ntxiv mus

Xaus Yog

Ntxiv mus

'Tsim kab los ntawm kev sau npe ntawm qhov sau npe:

Diag initBytes Raws li Byte () = BitConverter. GetBytes (initReg)

Dim initBytesReversed Raws li IEnumerable (Ntawm Byte) = (Los ntawm b Raws li Byte Hauv initBytes Siv davInBytes). Reverse

Dim initFifo Raws Li Tshiab Queue (Ntawm Boolean) (dav - 1)

Rau Txhua b Li Byte Hauv initBytesReversed

Dim ba Raws li Tshiab BitArray ({b})

Yog hais tias Tsis thim rov qabBytes Tom qab ntawd

Rau Kuv Xws Li = 0 Kom 7

initFifo. Enqueue (ba (i))

Ntxiv mus

Lwm Tus

Rau Kuv Xws Li = 7 Rau 0 Kauj Ruam -1

initFifo. Enqueue (ba (i))

Ntxiv mus

Xaus Yog

Ntxiv mus

'Ua haujlwm thiab XOR:

Dim sau npe Raws li UInteger = 0 'sau rau qhov dav-ntsis sau npe nrog tus xoom.

Ua Thaum msgFifo. Count> 0

Dim poppedBit Raws Li Tus lej = CInt (sau npe >> (dav - 1)) Thiab 1 'txhais kom meej ua ntej hloov npe.

Dim hloov pauvBit As Byte = Hloov. ToByte (msgFifo. Dequeue)

Yog initFifo. Count> 0 Ces

Dim b As Byte = Hloov. Kev hloov pauv (initFifo. Dequeue)

nqhivBit = shivBit Xor b

Xaus Yog

sau npe = rau npe << 1

sau npe = rau npe Los yog hloov pauv Bit

Yog tias poppedBit = 1 Tom Qab

zwm = sau npe Xor poly

Xaus Yog

Voj

'Kev hloov pauv zaum kawg:

Dim crc Raws li UInteger = sau npe 'Lub zwm npe muaj qhov seem ntawm kev faib = = checksum.

Yog hais tias reverseCrc Tom qab ntawd

crc = xav txog (crc, dav)

Xaus Yog

crc = crc Xor finalXor

crc = crc Thiab (& HFFFFFFFFUI >> (32 - dav)) 'daim npog qhov tseem ceeb tshaj plaws.

Rov qab crc

Tus Kawg Tas

Pom zoo: