From e295e4669be452c3aa855065cf6816c68d42df7a Mon Sep 17 00:00:00 2001 From: Clint Adams Date: Sat, 31 Mar 2012 18:53:45 -0400 Subject: Implement CRC24 for ASCII armor. --- Data/Digest/CRC24.hs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Data/Digest/CRC24.hs diff --git a/Data/Digest/CRC24.hs b/Data/Digest/CRC24.hs new file mode 100644 index 0000000..5c8c27c --- /dev/null +++ b/Data/Digest/CRC24.hs @@ -0,0 +1,25 @@ +-- CRC24.hs: OpenPGP (RFC4880) CRC24 implementation +-- Copyright Ⓒ 2012 Clint Adams +-- This software is released under the terms of the Expat (MIT) license. +-- (See the LICENSE file). + +module Data.Digest.CRC24 ( + crc24 +) where + +import Data.Bits (shiftL, (.&.), xor) +import Data.ByteString (ByteString) +import qualified Data.ByteString as B +import Data.Word (Word8, Word32) + +crc24_init :: Word32 +crc24_init = 0xB704CE + +crc24_poly :: Word32 +crc24_poly = 0x1864CFB + +crc24_update :: Word32 -> Word8 -> Word32 +crc24_update c b = (last . take 9 $ iterate (\x -> if (shiftL x 1) .&. 0x1000000 == 0x1000000 then shiftL x 1 `xor` crc24_poly else shiftL x 1) (c `xor` shiftL (fromIntegral b) 16)) .&. 0xFFFFFF + +crc24 :: ByteString -> Word32 +crc24 bs = B.foldl crc24_update crc24_init bs -- cgit v1.2.3