Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.Bits
- import Data.Word
- import Numeric
- fI :: (Integral a, Num b) => a -> b
- fI = fromIntegral
- crc32 :: [Word8] -> Word32
- crc32 xs = foldl k 0xFFFFFFFF xs `xor` 0xFFFFFFFF
- where
- f x = if x .&. 1 /= 0
- then x `shiftR` 1 `xor` 0xEDB88320
- else x `shiftR` 1
- g x = iterate f x !! 8
- t = map g [0..255]
- k c x = t !! fI ((c `xor` fI x) .&. 0xFF) `xor` (c `shiftR` 8)
- crc8 :: [Word8] -> Word8
- crc8 xs = foldl k 0xFF xs `xor` 0xFF
- where
- f x = if x .&. 0x80 /= 0
- then x `shiftL` 1 `xor` 0x1D
- else x `shiftL` 1
- g x = iterate f x !! 8
- t = map g [0..255]
- k c x = t !! fI ((c `xor` x) .&. 0xFF) `xor` (c `shiftL` 8)
- main :: IO ()
- main = do
- putStrLn $ showHex (crc8 [0x04,0x53,0x5F,0x0A,0x6C,0x20,0xB1,0xFF,0x00]) ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement